Test that update works for every field in RegistrarSettings

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=192348329
This commit is contained in:
guyben 2018-04-10 14:36:58 -07:00 committed by jianglai
parent ea995cf801
commit eb17851cb3
3 changed files with 150 additions and 0 deletions

View file

@ -94,9 +94,21 @@ public class Address extends ImmutableObject implements Jsonifiable {
.build(); .build();
} }
@VisibleForTesting
public Builder<? extends Address> asBuilder() {
return new Builder<>(clone(this));
}
/** A builder for constructing {@link Address}. */ /** A builder for constructing {@link Address}. */
@VisibleForTesting @VisibleForTesting
public static class Builder<T extends Address> extends Buildable.Builder<T> { public static class Builder<T extends Address> extends Buildable.Builder<T> {
public Builder() {}
protected Builder(T instance) {
super(instance);
}
public Builder<T> setStreet(ImmutableList<String> street) { public Builder<T> setStreet(ImmutableList<String> street) {
checkArgument( checkArgument(
street == null || (!street.isEmpty() && street.size() <= 3), street == null || (!street.isEmpty() && street.size() <= 3),

View file

@ -17,6 +17,7 @@ package google.registry.model.registrar;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import static google.registry.util.CollectionUtils.forceEmptyToNull; import static google.registry.util.CollectionUtils.forceEmptyToNull;
import com.google.common.annotations.VisibleForTesting;
import com.googlecode.objectify.annotation.Embed; import com.googlecode.objectify.annotation.Embed;
import google.registry.model.eppcommon.Address; import google.registry.model.eppcommon.Address;
@ -30,8 +31,20 @@ import google.registry.model.eppcommon.Address;
@Embed @Embed
public class RegistrarAddress extends Address { public class RegistrarAddress extends Address {
@Override
@VisibleForTesting
public Builder asBuilder() {
return new Builder(clone(this));
}
/** Builder for {@link RegistrarAddress}. */ /** Builder for {@link RegistrarAddress}. */
public static class Builder extends Address.Builder<RegistrarAddress> { public static class Builder extends Address.Builder<RegistrarAddress> {
public Builder() {}
private Builder(RegistrarAddress instance) {
super(instance);
}
@Override @Override
public RegistrarAddress build() { public RegistrarAddress build() {
RegistrarAddress instance = getInstance(); RegistrarAddress instance = getInstance();

View file

@ -28,12 +28,18 @@ import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import google.registry.export.sheet.SyncRegistrarsSheetAction; import google.registry.export.sheet.SyncRegistrarsSheetAction;
import google.registry.model.registrar.Registrar;
import google.registry.request.HttpException.ForbiddenException; import google.registry.request.HttpException.ForbiddenException;
import google.registry.request.auth.AuthResult; import google.registry.request.auth.AuthResult;
import google.registry.testing.CertificateSamples;
import google.registry.testing.TaskQueueHelper.TaskMatcher; import google.registry.testing.TaskQueueHelper.TaskMatcher;
import google.registry.util.CidrAddressBlock;
import java.util.Map; import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Function;
import javax.mail.internet.InternetAddress; import javax.mail.internet.InternetAddress;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import org.json.simple.JSONValue; import org.json.simple.JSONValue;
@ -158,6 +164,125 @@ public class RegistrarSettingsActionTest extends RegistrarSettingsActionTestCase
assertNoTasksEnqueued("sheet"); assertNoTasksEnqueued("sheet");
} }
private <T> void doTestUpdate(
Function<Registrar, T> getter,
T newValue,
BiFunction<Registrar.Builder, T, Registrar.Builder> setter) {
Registrar registrar = loadRegistrar(CLIENT_ID);
assertThat(getter.apply(registrar)).isNotEqualTo(newValue);
Map<String, Object> response =
action.handleJsonRequest(
ImmutableMap.of(
"op", "update",
"args", setter.apply(registrar.asBuilder(), newValue).build().toJsonMap()));
registrar = loadRegistrar(CLIENT_ID);
assertThat(response).containsEntry("status", "SUCCESS");
assertThat(response).containsEntry("results", asList(registrar.toJsonMap()));
assertThat(getter.apply(registrar)).isEqualTo(newValue);
}
@Test
public void testUpdate_premiumPriceAck() throws Exception {
doTestUpdate(
Registrar::getPremiumPriceAckRequired, true, Registrar.Builder::setPremiumPriceAckRequired);
}
@Test
public void testUpdate_whoisServer() throws Exception {
doTestUpdate(Registrar::getWhoisServer, "new-whois.example", Registrar.Builder::setWhoisServer);
}
@Test
public void testUpdate_referralUrl() throws Exception {
doTestUpdate(
Registrar::getReferralUrl, "new-reference-url.example", Registrar.Builder::setReferralUrl);
}
@Test
public void testUpdate_phoneNumber() throws Exception {
doTestUpdate(Registrar::getPhoneNumber, "+1.2345678900", Registrar.Builder::setPhoneNumber);
}
@Test
public void testUpdate_faxNumber() throws Exception {
doTestUpdate(Registrar::getFaxNumber, "+1.2345678900", Registrar.Builder::setFaxNumber);
}
@Test
public void testUpdate_url() throws Exception {
doTestUpdate(Registrar::getUrl, "new-url.example", Registrar.Builder::setUrl);
}
@Test
public void testUpdate_ipAddressWhitelist() throws Exception {
doTestUpdate(
Registrar::getIpAddressWhitelist,
ImmutableList.of(CidrAddressBlock.create("1.1.1.0/24")),
Registrar.Builder::setIpAddressWhitelist);
}
@Test
public void testUpdate_clientCertificate() throws Exception {
doTestUpdate(
Registrar::getClientCertificate,
CertificateSamples.SAMPLE_CERT,
(builder, s) -> builder.setClientCertificate(s, clock.nowUtc()));
}
@Test
public void testUpdate_failoverClientCertificate() throws Exception {
doTestUpdate(
Registrar::getFailoverClientCertificate,
CertificateSamples.SAMPLE_CERT,
(builder, s) -> builder.setFailoverClientCertificate(s, clock.nowUtc()));
}
@Test
public void testUpdate_localizedAddress_city() throws Exception {
doTestUpdate(
Registrar::getLocalizedAddress,
loadRegistrar(CLIENT_ID).getLocalizedAddress().asBuilder().setCity("newCity").build(),
Registrar.Builder::setLocalizedAddress);
}
@Test
public void testUpdate_localizedAddress_countryCode() throws Exception {
doTestUpdate(
Registrar::getLocalizedAddress,
loadRegistrar(CLIENT_ID).getLocalizedAddress().asBuilder().setCountryCode("GB").build(),
Registrar.Builder::setLocalizedAddress);
}
@Test
public void testUpdate_localizedAddress_state() throws Exception {
doTestUpdate(
Registrar::getLocalizedAddress,
loadRegistrar(CLIENT_ID).getLocalizedAddress().asBuilder().setState("NJ").build(),
Registrar.Builder::setLocalizedAddress);
}
@Test
public void testUpdate_localizedAddress_street() throws Exception {
doTestUpdate(
Registrar::getLocalizedAddress,
loadRegistrar(CLIENT_ID)
.getLocalizedAddress()
.asBuilder()
.setStreet(ImmutableList.of("new street"))
.build(),
Registrar.Builder::setLocalizedAddress);
}
@Test
public void testUpdate_localizedAddress_zip() throws Exception {
doTestUpdate(
Registrar::getLocalizedAddress,
loadRegistrar(CLIENT_ID).getLocalizedAddress().asBuilder().setZip("new zip").build(),
Registrar.Builder::setLocalizedAddress);
}
private static String getLastUpdateTime() { private static String getLastUpdateTime() {
return loadRegistrar(CLIENT_ID).getLastUpdateTime().toString(); return loadRegistrar(CLIENT_ID).getLastUpdateTime().toString();
} }