Switch to constructor injection in a few actions

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=232552406
This commit is contained in:
mcilwain 2019-02-05 13:57:17 -08:00 committed by jianglai
parent 927e8bbd73
commit 29d3ad8052
5 changed files with 49 additions and 50 deletions

View file

@ -38,11 +38,22 @@ import javax.inject.Inject;
auth = Auth.AUTH_INTERNAL_ONLY) auth = Auth.AUTH_INTERNAL_ONLY)
public final class RefreshDnsAction implements Runnable { public final class RefreshDnsAction implements Runnable {
@Inject Clock clock; private final Clock clock;
@Inject DnsQueue dnsQueue; private final DnsQueue dnsQueue;
@Inject @Parameter("domainOrHostName") String domainOrHostName; private final String domainOrHostName;
@Inject @Parameter("type") TargetType type; private final TargetType type;
@Inject RefreshDnsAction() {}
@Inject
RefreshDnsAction(
@Parameter("domainOrHostName") String domainOrHostName,
@Parameter("type") TargetType type,
Clock clock,
DnsQueue dnsQueue) {
this.domainOrHostName = domainOrHostName;
this.type = type;
this.clock = clock;
this.dnsQueue = dnsQueue;
}
@Override @Override
public void run() { public void run() {

View file

@ -58,9 +58,14 @@ public class DeleteEntityAction implements Runnable {
public static final String PATH = "/_dr/admin/deleteEntity"; public static final String PATH = "/_dr/admin/deleteEntity";
public static final String PARAM_RAW_KEYS = "rawKeys"; public static final String PARAM_RAW_KEYS = "rawKeys";
@Inject @Parameter(PARAM_RAW_KEYS) String rawKeys; private final String rawKeys;
@Inject Response response; private final Response response;
@Inject DeleteEntityAction() {}
@Inject
DeleteEntityAction(@Parameter(PARAM_RAW_KEYS) String rawKeys, Response response) {
this.rawKeys = rawKeys;
this.response = response;
}
@Override @Override
public void run() { public void run() {

View file

@ -41,20 +41,14 @@ import org.junit.runners.JUnit4;
public class RefreshDnsActionTest { public class RefreshDnsActionTest {
@Rule @Rule
public final AppEngineRule appEngine = AppEngineRule.builder() public final AppEngineRule appEngine =
.withDatastore() AppEngineRule.builder().withDatastore().withTaskQueue().build();
.withTaskQueue()
.build();
private final DnsQueue dnsQueue = mock(DnsQueue.class); private final DnsQueue dnsQueue = mock(DnsQueue.class);
private final FakeClock clock = new FakeClock(); private final FakeClock clock = new FakeClock();
private void run(TargetType type, String name) { private void run(TargetType type, String name) {
RefreshDnsAction action = new RefreshDnsAction(); new RefreshDnsAction(name, type, clock, dnsQueue).run();
action.clock = clock;
action.domainOrHostName = name;
action.type = type;
action.dnsQueue = dnsQueue;
action.run();
} }
@Before @Before

View file

@ -60,7 +60,10 @@ public final class FakeResponse implements Response {
@Override @Override
public void setContentType(MediaType contentType) { public void setContentType(MediaType contentType) {
checkArgument(payload.isEmpty(), "setContentType must be called before setPayload"); checkArgument(
payload.isEmpty(),
"setContentType must be called before setPayload; payload is: %s",
payload);
this.contentType = checkNotNull(contentType); this.contentType = checkNotNull(contentType);
} }

View file

@ -26,7 +26,6 @@ import google.registry.model.registry.label.ReservedList;
import google.registry.request.HttpException.BadRequestException; import google.registry.request.HttpException.BadRequestException;
import google.registry.testing.AppEngineRule; import google.registry.testing.AppEngineRule;
import google.registry.testing.FakeResponse; import google.registry.testing.FakeResponse;
import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -36,34 +35,23 @@ import org.junit.runners.JUnit4;
@RunWith(JUnit4.class) @RunWith(JUnit4.class)
public class DeleteEntityActionTest { public class DeleteEntityActionTest {
@Rule @Rule public final AppEngineRule appEngine = AppEngineRule.builder().withDatastore().build();
public final AppEngineRule appEngine = AppEngineRule.builder().withDatastore().build();
FakeResponse response = new FakeResponse(); FakeResponse response = new FakeResponse();
DeleteEntityAction action = new DeleteEntityAction();
@Before
public void init() {
action.response = response;
}
@Test @Test
public void test_deleteSingleRawEntitySuccessfully() { public void test_deleteSingleRawEntitySuccessfully() {
Entity entity = new Entity("single", "raw"); Entity entity = new Entity("single", "raw");
getDatastoreService().put(entity); getDatastoreService().put(entity);
action.rawKeys = KeyFactory.keyToString(entity.getKey()); new DeleteEntityAction(KeyFactory.keyToString(entity.getKey()), response).run();
action.run(); assertThat(response.getPayload()).isEqualTo("Deleted 1 raw entities and 0 registered entities");
assertThat(response.getPayload())
.isEqualTo("Deleted 1 raw entities and 0 registered entities");
} }
@Test @Test
public void test_deleteSingleRegisteredEntitySuccessfully() { public void test_deleteSingleRegisteredEntitySuccessfully() {
ReservedList ofyEntity = new ReservedList.Builder().setName("foo").build(); ReservedList ofyEntity = new ReservedList.Builder().setName("foo").build();
ofy().saveWithoutBackup().entity(ofyEntity).now(); ofy().saveWithoutBackup().entity(ofyEntity).now();
action.rawKeys = KeyFactory.keyToString(create(ofyEntity).getRaw()); new DeleteEntityAction(KeyFactory.keyToString(create(ofyEntity).getRaw()), response).run();
action.run(); assertThat(response.getPayload()).isEqualTo("Deleted 0 raw entities and 1 registered entities");
assertThat(response.getPayload())
.isEqualTo("Deleted 0 raw entities and 1 registered entities");
} }
@Test @Test
@ -71,10 +59,8 @@ public class DeleteEntityActionTest {
Entity entity = new Entity("single", "raw"); Entity entity = new Entity("single", "raw");
entity.setIndexedProperty("^d", "UnregType"); entity.setIndexedProperty("^d", "UnregType");
getDatastoreService().put(entity); getDatastoreService().put(entity);
action.rawKeys = KeyFactory.keyToString(entity.getKey()); new DeleteEntityAction(KeyFactory.keyToString(entity.getKey()), response).run();
action.run(); assertThat(response.getPayload()).isEqualTo("Deleted 1 raw entities and 0 registered entities");
assertThat(response.getPayload())
.isEqualTo("Deleted 1 raw entities and 0 registered entities");
} }
@Test @Test
@ -85,18 +71,17 @@ public class DeleteEntityActionTest {
ReservedList ofyEntity = new ReservedList.Builder().setName("registered").build(); ReservedList ofyEntity = new ReservedList.Builder().setName("registered").build();
ofy().saveWithoutBackup().entity(ofyEntity).now(); ofy().saveWithoutBackup().entity(ofyEntity).now();
String ofyKey = KeyFactory.keyToString(create(ofyEntity).getRaw()); String ofyKey = KeyFactory.keyToString(create(ofyEntity).getRaw());
action.rawKeys = String.format("%s,%s", rawKey, ofyKey); new DeleteEntityAction(String.format("%s,%s", rawKey, ofyKey), response).run();
action.run(); assertThat(response.getPayload()).isEqualTo("Deleted 1 raw entities and 1 registered entities");
assertThat(response.getPayload())
.isEqualTo("Deleted 1 raw entities and 1 registered entities");
} }
@Test @Test
public void test_deleteNonExistentEntityRepliesWithError() { public void test_deleteNonExistentEntityRepliesWithError() {
Entity entity = new Entity("not", "here"); Entity entity = new Entity("not", "here");
String rawKey = KeyFactory.keyToString(entity.getKey()); String rawKey = KeyFactory.keyToString(entity.getKey());
action.rawKeys = rawKey; BadRequestException thrown =
BadRequestException thrown = assertThrows(BadRequestException.class, action::run); assertThrows(
BadRequestException.class, () -> new DeleteEntityAction(rawKey, response).run());
assertThat(thrown).hasMessageThat().contains("Could not find entity with key " + rawKey); assertThat(thrown).hasMessageThat().contains("Could not find entity with key " + rawKey);
} }
@ -105,10 +90,11 @@ public class DeleteEntityActionTest {
ReservedList ofyEntity = new ReservedList.Builder().setName("first_registered").build(); ReservedList ofyEntity = new ReservedList.Builder().setName("first_registered").build();
ofy().saveWithoutBackup().entity(ofyEntity).now(); ofy().saveWithoutBackup().entity(ofyEntity).now();
String ofyKey = KeyFactory.keyToString(create(ofyEntity).getRaw()); String ofyKey = KeyFactory.keyToString(create(ofyEntity).getRaw());
Entity entity = new Entity("non", "existent"); String rawKey = KeyFactory.keyToString(new Entity("non", "existent").getKey());
String rawKey = KeyFactory.keyToString(entity.getKey()); BadRequestException thrown =
action.rawKeys = String.format("%s,%s", ofyKey, rawKey); assertThrows(
BadRequestException thrown = assertThrows(BadRequestException.class, action::run); BadRequestException.class,
() -> new DeleteEntityAction(String.format("%s,%s", ofyKey, rawKey), response).run());
assertThat(thrown).hasMessageThat().contains("Could not find entity with key " + rawKey); assertThat(thrown).hasMessageThat().contains("Could not find entity with key " + rawKey);
} }
} }