google-nomulus/javatests/google/registry/tools/UpdateApplicationStatusCommandTest.java
mcilwain b825a2b5a8 Get rid of custom ExceptionRule methods
The only remaining methods on ExceptionRule after this are methods that
also exist on ExpectedException, which will allow us to, in the next CL,
swap out the one for the other and then run the automated refactoring to
turn it all into assertThrows/expectThrows.

Note that there were some assertions about root causes that couldn't
easily be turned into ExpectedException invocations, so I simply
converted them directly to usages of assertThrows/expectThrows.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=178623431
2017-12-13 12:43:45 -05:00

285 lines
12 KiB
Java

// Copyright 2017 The Nomulus Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package google.registry.tools;
import static com.google.common.collect.Iterables.getLast;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.model.domain.launch.ApplicationStatus.ALLOCATED;
import static google.registry.model.domain.launch.ApplicationStatus.PENDING_ALLOCATION;
import static google.registry.model.domain.launch.ApplicationStatus.REJECTED;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.testing.DatastoreHelper.createTld;
import static google.registry.testing.DatastoreHelper.loadRegistrar;
import static google.registry.testing.DatastoreHelper.newContactResourceWithRoid;
import static google.registry.testing.DatastoreHelper.newDomainApplication;
import static google.registry.testing.DatastoreHelper.persistResource;
import static google.registry.testing.DomainApplicationSubject.assertAboutApplications;
import static google.registry.testing.HistoryEntrySubject.assertAboutHistoryEntries;
import static google.registry.testing.JUnitBackports.expectThrows;
import static org.joda.time.DateTimeZone.UTC;
import google.registry.model.domain.DomainApplication;
import google.registry.model.eppcommon.StatusValue;
import google.registry.model.eppcommon.Trid;
import google.registry.model.poll.PendingActionNotificationResponse.DomainPendingActionNotificationResponse;
import google.registry.model.poll.PollMessage;
import google.registry.model.reporting.HistoryEntry;
import org.joda.time.DateTime;
import org.junit.Before;
import org.junit.Test;
/** Unit tests for {@link UpdateApplicationStatusCommand}. */
public class UpdateApplicationStatusCommandTest
extends CommandTestCase<UpdateApplicationStatusCommand> {
private DomainApplication domainApplication;
private DateTime creationTime;
@Before
public void init() {
// Since the command's history client ID defaults to CharlestonRoad, resave TheRegistrar as
// CharlestonRoad so we don't have to pass in --history_client_id everywhere below.
persistResource(
loadRegistrar("TheRegistrar").asBuilder().setClientId("CharlestonRoad").build());
createTld("xn--q9jyb4c");
domainApplication = persistResource(newDomainApplication(
"label.xn--q9jyb4c", persistResource(newContactResourceWithRoid("contact1", "C1-ROID")))
.asBuilder()
.setPersistedCurrentSponsorClientId("TheRegistrar")
.build());
this.creationTime = domainApplication.getCreationTime();
// Add a history entry under this application that corresponds to its creation.
persistResource(
new HistoryEntry.Builder()
.setParent(domainApplication)
.setModificationTime(creationTime)
.setTrid(Trid.create("ABC123", "server-trid"))
.setType(HistoryEntry.Type.DOMAIN_APPLICATION_CREATE)
.build());
// Add a second history entry with a different Trid just to make sure we are always retrieving
// the right one.
persistResource(
new HistoryEntry.Builder()
.setParent(domainApplication)
.setModificationTime(creationTime)
.setTrid(Trid.create("ABC124", "server-trid"))
.setType(HistoryEntry.Type.DOMAIN_APPLICATION_CREATE)
.build());
}
private HistoryEntry loadLastHistoryEntry() {
// Loading everything and using getLast() is inefficient, but to do it right would require a new
// descending index on modification time, and this is fine for testing.
return getLast(ofy().load()
.type(HistoryEntry.class)
.ancestor(domainApplication)
.order("modificationTime")
.list());
}
@Test
public void testSuccess_rejected() throws Exception {
DateTime before = new DateTime(UTC);
assertAboutApplications()
.that(domainApplication)
.hasStatusValue(StatusValue.PENDING_CREATE).and()
.doesNotHaveApplicationStatus(REJECTED);
assertThat(getPollMessageCount()).isEqualTo(0);
Trid creationTrid = Trid.create("DEF456", "server-trid");
persistResource(reloadResource(domainApplication).asBuilder()
.setCreationTrid(creationTrid)
.build());
runCommandForced("--ids=2-Q9JYB4C", "--msg=\"Application rejected\"", "--status=REJECTED");
domainApplication = ofy().load().entity(domainApplication).now();
assertAboutApplications().that(domainApplication)
.doesNotHaveStatusValue(StatusValue.PENDING_CREATE).and()
.hasApplicationStatus(REJECTED).and()
.hasLastEppUpdateTimeAtLeast(before).and()
.hasLastEppUpdateClientId("TheRegistrar");
assertAboutHistoryEntries().that(loadLastHistoryEntry())
.hasType(HistoryEntry.Type.DOMAIN_APPLICATION_STATUS_UPDATE).and()
.hasClientId("CharlestonRoad");
assertThat(getPollMessageCount()).isEqualTo(1);
PollMessage pollMessage = getFirstPollMessage();
assertThat(pollMessage.getMsg()).isEqualTo("Application rejected");
DomainPendingActionNotificationResponse response =
(DomainPendingActionNotificationResponse)
pollMessage.getResponseData().stream().findFirst().get();
assertThat(response.getTrid()).isEqualTo(creationTrid);
assertThat(response.getActionResult()).isFalse();
}
private PollMessage getFirstPollMessage() {
return ofy().load().type(PollMessage.class).first().safe();
}
@Test
public void testSuccess_allocated() throws Exception {
DateTime before = new DateTime(UTC);
assertAboutApplications().that(domainApplication)
.hasStatusValue(StatusValue.PENDING_CREATE).and()
.doesNotHaveApplicationStatus(ALLOCATED);
assertThat(getPollMessageCount()).isEqualTo(0);
Trid creationTrid = Trid.create("DEF456", "server-trid");
persistResource(reloadResource(domainApplication).asBuilder()
.setCreationTrid(creationTrid)
.build());
runCommandForced("--ids=2-Q9JYB4C", "--msg=\"Application allocated\"", "--status=ALLOCATED");
domainApplication = ofy().load().entity(domainApplication).now();
assertAboutApplications().that(domainApplication)
.doesNotHaveStatusValue(StatusValue.PENDING_CREATE).and()
.hasApplicationStatus(ALLOCATED).and()
.hasLastEppUpdateTimeAtLeast(before).and()
.hasLastEppUpdateClientId("TheRegistrar");
assertAboutHistoryEntries().that(loadLastHistoryEntry())
.hasType(HistoryEntry.Type.DOMAIN_APPLICATION_STATUS_UPDATE).and()
.hasClientId("CharlestonRoad");
assertThat(getPollMessageCount()).isEqualTo(1);
PollMessage pollMessage = getFirstPollMessage();
assertThat(pollMessage.getMsg()).isEqualTo("Application allocated");
DomainPendingActionNotificationResponse response =
(DomainPendingActionNotificationResponse)
pollMessage.getResponseData().stream().findFirst().get();
assertThat(response.getTrid()).isEqualTo(creationTrid);
assertThat(response.getActionResult()).isTrue();
}
@Test
public void testSuccess_pendingAllocation() throws Exception {
DateTime before = new DateTime(UTC);
assertAboutApplications().that(domainApplication)
.doesNotHaveApplicationStatus(PENDING_ALLOCATION).and()
.hasStatusValue(StatusValue.PENDING_CREATE);
assertThat(getPollMessageCount()).isEqualTo(0);
Trid creationTrid = Trid.create("DEF456", "server-trid");
persistResource(reloadResource(domainApplication).asBuilder()
.setCreationTrid(creationTrid)
.build());
runCommandForced(
"--ids=2-Q9JYB4C",
"--msg=\"Application pending allocation\"",
"--status=PENDING_ALLOCATION");
domainApplication = ofy().load().entity(domainApplication).now();
assertAboutApplications().that(domainApplication)
.hasStatusValue(StatusValue.PENDING_CREATE).and()
.hasApplicationStatus(PENDING_ALLOCATION).and()
.hasLastEppUpdateTimeAtLeast(before).and()
.hasLastEppUpdateClientId("TheRegistrar");
assertAboutHistoryEntries().that(loadLastHistoryEntry())
.hasType(HistoryEntry.Type.DOMAIN_APPLICATION_STATUS_UPDATE).and()
.hasClientId("CharlestonRoad");
assertThat(getPollMessageCount()).isEqualTo(1);
PollMessage pollMessage = getFirstPollMessage();
assertThat(pollMessage.getMsg()).isEqualTo("Application pending allocation");
assertThat(pollMessage.getResponseData()).isEmpty();
assertThat(pollMessage.getResponseExtensions()).isNotEmpty();
}
@Test
public void testSuccess_rejectedTridFromHistoryEntry() throws Exception {
DateTime before = new DateTime(UTC);
assertAboutApplications().that(domainApplication)
.hasStatusValue(StatusValue.PENDING_CREATE).and()
.doesNotHaveApplicationStatus(REJECTED);
assertThat(getPollMessageCount()).isEqualTo(0);
runCommandForced("--ids=2-Q9JYB4C", "--msg=\"Application rejected\"", "--status=REJECTED");
domainApplication = ofy().load().entity(domainApplication).now();
assertAboutApplications().that(domainApplication)
.doesNotHaveStatusValue(StatusValue.PENDING_CREATE).and()
.hasApplicationStatus(REJECTED).and()
.hasLastEppUpdateTimeAtLeast(before).and()
.hasLastEppUpdateClientId("TheRegistrar");
assertAboutHistoryEntries().that(loadLastHistoryEntry())
.hasType(HistoryEntry.Type.DOMAIN_APPLICATION_STATUS_UPDATE).and()
.hasClientId("CharlestonRoad");
assertThat(getPollMessageCount()).isEqualTo(1);
PollMessage pollMessage = getFirstPollMessage();
DomainPendingActionNotificationResponse response =
(DomainPendingActionNotificationResponse)
pollMessage.getResponseData().stream().findFirst().get();
assertThat(response.getTrid()).isEqualTo(Trid.create("ABC123", "server-trid"));
}
@Test
public void testFailure_applicationAlreadyRejected() throws Exception {
assertThat(getPollMessageCount()).isEqualTo(0);
persistResource(reloadResource(domainApplication).asBuilder()
.setApplicationStatus(REJECTED)
.build());
runCommandForced("--ids=2-Q9JYB4C", "--msg=\"Application rejected\"", "--status=REJECTED");
assertAboutApplications().that(ofy().load().entity(domainApplication).now())
.hasApplicationStatus(REJECTED);
assertThat(getPollMessageCount()).isEqualTo(0);
assertAboutHistoryEntries().that(loadLastHistoryEntry())
.hasType(HistoryEntry.Type.DOMAIN_APPLICATION_CREATE);
}
@Test
public void testFailure_applicationAlreadyAllocated() throws Exception {
persistResource(reloadResource(domainApplication).asBuilder()
.setApplicationStatus(ALLOCATED)
.build());
IllegalStateException e =
expectThrows(
IllegalStateException.class,
() ->
runCommandForced(
"--ids=2-Q9JYB4C", "--msg=\"Application rejected\"", "--status=REJECTED"));
assertThat(e).hasMessageThat().contains("Domain application has final status ALLOCATED");
assertAboutApplications().that(ofy().load().entity(domainApplication).now())
.hasApplicationStatus(ALLOCATED);
assertThat(getPollMessageCount()).isEqualTo(0);
assertAboutHistoryEntries().that(loadLastHistoryEntry())
.hasType(HistoryEntry.Type.DOMAIN_APPLICATION_CREATE);
}
@Test
public void testFailure_applicationDoesNotExist() throws Exception {
thrown.expect(IllegalArgumentException.class);
runCommandForced("--ids=555-Q9JYB4C", "--msg=\"Application rejected\"", "--status=REJECTED");
}
@Test
public void testFailure_historyClientIdDoesNotExist() throws Exception {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("fakeclient");
runCommandForced(
"--history_client_id=fakeclient", "--ids=2-Q9JYB4C", "--msg=Ignored", "--status=REJECTED");
}
}