Allow partial update of billing account map

When doing update_registrar, it is now possible to only specify the currencies and the account ids that need updating.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=159262119
This commit is contained in:
jianglai 2017-06-16 12:41:19 -07:00 committed by Ben McIlwain
parent 7d2f53a6fe
commit d1ef4b9c37
3 changed files with 27 additions and 4 deletions

View file

@ -698,8 +698,8 @@ public class Registrar extends ImmutableObject implements Buildable, Jsonifiable
ImmutableMap.Builder<CurrencyUnit, BillingAccountEntry> billingAccountMapBuilder =
new ImmutableMap.Builder<>();
for (Map.Entry<CurrencyUnit, String> entry : billingAccountMap.entrySet()) {
CurrencyUnit key = entry.getKey();
billingAccountMapBuilder.put(key, new BillingAccountEntry(key, entry.getValue()));
billingAccountMapBuilder.put(
entry.getKey(), new BillingAccountEntry(entry.getKey(), entry.getValue()));
}
getInstance().billingAccountMap = billingAccountMapBuilder.build();
}

View file

@ -47,6 +47,7 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
@ -182,7 +183,8 @@ abstract class CreateOrUpdateRegistrarCommand extends MutatingCommand {
description =
"Registrar Billing Account key-value pairs (formatted as key=value[,key=value...]), "
+ "where key is a currency unit (USD, JPY, etc) and value is the registrar's billing "
+ "account id for that currency.",
+ "account id for that currency. During update, only the pairs that need updating need "
+ "to be provided.",
converter = CurrencyUnitToStringMap.class,
validateWith = CurrencyUnitToStringMap.class
)
@ -354,7 +356,12 @@ abstract class CreateOrUpdateRegistrarCommand extends MutatingCommand {
builder.setBillingIdentifier(billingId.orNull());
}
if (billingAccountMap != null) {
builder.setBillingAccountMap(billingAccountMap);
LinkedHashMap<CurrencyUnit, String> newBillingAccountMap = new LinkedHashMap<>();
if (oldRegistrar != null && oldRegistrar.getBillingAccountMap() != null) {
newBillingAccountMap.putAll(oldRegistrar.getBillingAccountMap());
}
newBillingAccountMap.putAll(billingAccountMap);
builder.setBillingAccountMap(newBillingAccountMap);
}
if (billingMethod != null) {
if (oldRegistrar != null && !billingMethod.equals(oldRegistrar.getBillingMethod())) {

View file

@ -26,6 +26,7 @@ import static org.joda.time.DateTimeZone.UTC;
import com.beust.jcommander.ParameterException;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import google.registry.model.billing.RegistrarBillingEntry;
import google.registry.model.registrar.Registrar;
@ -225,6 +226,7 @@ public class UpdateRegistrarCommandTest extends CommandTestCase<UpdateRegistrarC
"NewRegistrar");
}
@Test
public void testSuccess_billingAccountMap_onlyAppliesToRealRegistrar() throws Exception {
createTlds("foo");
assertThat(loadByClientId("NewRegistrar").getBillingAccountMap()).isEmpty();
@ -237,6 +239,20 @@ public class UpdateRegistrarCommandTest extends CommandTestCase<UpdateRegistrarC
.containsExactly(CurrencyUnit.JPY, "789xyz");
}
@Test
public void testSuccess_billingAccountMap_partialUpdate() throws Exception {
createTlds("foo");
persistResource(
loadByClientId("NewRegistrar")
.asBuilder()
.setBillingAccountMap(
ImmutableMap.of(CurrencyUnit.USD, "abc123", CurrencyUnit.JPY, "789xyz"))
.build());
runCommand("--billing_account_map=JPY=123xyz", "--allowed_tlds=foo", "--force", "NewRegistrar");
assertThat(loadByClientId("NewRegistrar").getBillingAccountMap())
.containsExactly(CurrencyUnit.JPY, "123xyz", CurrencyUnit.USD, "abc123");
}
@Test
public void testSuccess_changeBillingMethodToBraintreeWhenBalanceIsZero() throws Exception {
createTlds("xn--q9jyb4c");