private Response postCompetitionGroupSet() throws Exception { org.coner.core.api.request.AddCompetitionGroupSetRequest requestAddCompetitionGroupSet = objectMapper.readValue( FixtureHelpers.fixture("fixtures/api/request/competition_group_set_add-request.json"), org.coner.core.api.request.AddCompetitionGroupSetRequest.class ); Entity<AddCompetitionGroupSetRequest> requestEntity = Entity.json(requestAddCompetitionGroupSet); CompetitionGroupSetAddPayload addPayload = mock(CompetitionGroupSetAddPayload.class); when(competitionGroupSetMapper.toDomainAddPayload(requestAddCompetitionGroupSet)).thenReturn(addPayload); CompetitionGroupSet domainEntity = mock(CompetitionGroupSet.class); when(competitionGroupSetService.add(addPayload)).thenReturn(domainEntity); CompetitionGroupSetApiEntity apiEntity = ApiEntityTestUtils.fullCompetitionGroupSet(); when(competitionGroupSetMapper.toApiEntity(domainEntity)).thenReturn(apiEntity); Response response = resources.client() .target("/competitionGroups/sets") .request(MediaType.APPLICATION_JSON_TYPE) .post(requestEntity); verify(competitionGroupSetService).add(addPayload); verifyNoMoreInteractions(competitionGroupSetService); return response; }
@Test public void whenAddValidEventItShouldAddEvent() throws Exception { AddEventRequest requestEvent = objectMapper.readValue( FixtureHelpers.fixture("fixtures/api/entity/event_add-request.json"), AddEventRequest.class ); Entity<AddEventRequest> requestEntity = Entity.json(requestEvent); EventAddPayload addPayload = mock(EventAddPayload.class); when(eventMapper.toDomainAddPayload(requestEvent)).thenReturn(addPayload); Event domainEntity = mock(Event.class); when(eventEntityService.add(addPayload)).thenReturn(domainEntity); EventApiEntity apiEntity = ApiEntityTestUtils.fullEvent(); when(eventMapper.toApiEntity(domainEntity)).thenReturn(apiEntity); Response response = resources.client() .target("/events") .request(MediaType.APPLICATION_JSON_TYPE) .post(requestEntity); assertThat(response.getStatus()).isEqualTo(HttpStatus.CREATED_201); verify(eventEntityService).add(addPayload); verifyNoMoreInteractions(eventEntityService); assertThat(response).isNotNull(); assertThat(response.getHeaderString(HttpHeader.LOCATION.asString())) .containsSequence("/events/", TestConstants.EVENT_ID); }
@Test public void whenAddEventWithoutNameItShouldFailValidation() throws Exception { AddEventRequest requestEvent = objectMapper.readValue( FixtureHelpers.fixture("fixtures/api/entity/event_add-request-without-name.json"), AddEventRequest.class ); Entity<AddEventRequest> requestEntity = Entity.json(requestEvent); Response response = resources.client() .target("/events") .request(MediaType.APPLICATION_JSON_TYPE) .post(requestEntity); assertThat(response.getStatus()).isEqualTo(HttpStatus.UNPROCESSABLE_ENTITY_422); ValidationErrorMessage validationErrorMessage = response.readEntity(ValidationErrorMessage.class); assertThat(validationErrorMessage.getErrors()) .isNotEmpty() .contains("name may not be empty"); verifyZeroInteractions(eventEntityService); }
@Test public void whenAddEventWithoutDateItShouldFailValidation() throws Exception { AddEventRequest requestEvent = objectMapper.readValue( FixtureHelpers.fixture("fixtures/api/entity/event_add-request-without-date.json"), AddEventRequest.class ); Entity<AddEventRequest> requestEntity = Entity.json(requestEvent); Response response = resources.client() .target("/events") .request(MediaType.APPLICATION_JSON_TYPE) .post(requestEntity); assertThat(response.getStatus()).isEqualTo(HttpStatus.UNPROCESSABLE_ENTITY_422); ValidationErrorMessage validationErrorMessage = response.readEntity(ValidationErrorMessage.class); assertThat(validationErrorMessage.getErrors()) .isNotEmpty() .contains("date may not be null"); verifyZeroInteractions(eventEntityService); }
@Test public void whenAddEventWithoutHandicapGroupSetIdItShouldFailValidation() throws Exception { AddEventRequest request = objectMapper.readValue( FixtureHelpers.fixture("fixtures/api/entity/event_add-request-without-handicap-group-set-id.json"), AddEventRequest.class ); Entity<AddEventRequest> requestEntity = Entity.json(request); Response response = resources.client() .target("/events") .request(MediaType.APPLICATION_JSON_TYPE) .post(requestEntity); assertThat(response.getStatus()).isEqualTo(HttpStatus.UNPROCESSABLE_ENTITY_422); ValidationErrorMessage validationErrorMessage = response.readEntity(ValidationErrorMessage.class); assertThat(validationErrorMessage.getErrors()) .isNotEmpty() .contains("handicapGroupSetId may not be empty"); verifyZeroInteractions(eventEntityService); }
@Test public void whenAddEventWithoutCompetitionGroupSetIdItShouldFailValidation() throws Exception { AddEventRequest request = objectMapper.readValue( FixtureHelpers.fixture("fixtures/api/entity/event_add-request-without-competition-group-set-id.json"), AddEventRequest.class ); Entity<AddEventRequest> requestEntity = Entity.json(request); Response response = resources.client() .target("/events") .request(MediaType.APPLICATION_JSON_TYPE) .post(requestEntity); assertThat(response.getStatus()).isEqualTo(HttpStatus.UNPROCESSABLE_ENTITY_422); ValidationErrorMessage validationErrorMessage = response.readEntity(ValidationErrorMessage.class); assertThat(validationErrorMessage.getErrors()) .isNotEmpty() .contains("competitionGroupSetId may not be empty"); verifyZeroInteractions(eventEntityService); }
private Response postHandicapGroupSet() throws Exception { AddHandicapGroupSetRequest request = objectMapper.readValue( FixtureHelpers.fixture("fixtures/api/request/handicap_group_set_add-request.json"), AddHandicapGroupSetRequest.class ); Entity<AddHandicapGroupSetRequest> requestEntity = Entity.json(request); HandicapGroupSetAddPayload addPayload = mock(HandicapGroupSetAddPayload.class); when(handicapGroupSetMapper.toDomainAddPayload(request)).thenReturn(addPayload); HandicapGroupSet domainEntity = mock(HandicapGroupSet.class); when(handicapGroupSetService.add(addPayload)).thenReturn(domainEntity); HandicapGroupSetApiEntity apiEntity = mock(HandicapGroupSetApiEntity.class); when(handicapGroupSetMapper.toApiEntity(domainEntity)).thenReturn(apiEntity); when(apiEntity.getId()).thenReturn(HANDICAP_GROUP_SET_ID); Response response = resources.client() .target("/handicapGroups/sets") .request(MediaType.APPLICATION_JSON_TYPE) .post(requestEntity); verify(handicapGroupSetService).add(addPayload); return response; }
@Test public void itShouldAddRunWithFullDetails() throws Exception { AddRunRequest apiRequest = objectMapper.readValue( FixtureHelpers.fixture("fixtures/api/entity/run_add-request_full.json"), AddRunRequest.class ); Entity<AddRunRequest> requestEntity = Entity.json(apiRequest); RunAddPayload addPayload = mock(RunAddPayload.class); when(runMapper.toDomainAddPayload(apiRequest, EVENT_ID)).thenReturn(addPayload); Run domainEntity = DomainEntityTestUtils.fullRun(); when(runEntityService.add(addPayload)).thenReturn(domainEntity); RunApiEntity apiEntity = ApiEntityTestUtils.fullRun(); when(runMapper.toApiEntity(domainEntity)).thenReturn(apiEntity); Response response = resources.client() .target(UriBuilder.fromPath("/events/{eventId}/runs").build(EVENT_ID)) .request(MediaType.APPLICATION_JSON_TYPE) .post(requestEntity); verify(runEntityService).add(addPayload); verifyNoMoreInteractions(runEntityService); assertThat(response).isNotNull(); assertThat(response.getStatus()).isEqualTo(HttpStatus.CREATED_201); assertThat(response.getHeaderString(HttpHeader.LOCATION.asString())) .containsSequence("/events/", EVENT_ID, "/runs/", RUN_ID); }
@Test public void whenAddHandicapGroupWithLargeFactorItShouldFailValidation() throws Exception { AddHandicapGroupRequest requestHandicapGroup = objectMapper.readValue( FixtureHelpers.fixture("fixtures/api/entity/handicap_group_add-request-wrong-factor.json"), AddHandicapGroupRequest.class ); Entity<AddHandicapGroupRequest> requestEntity = Entity.json(requestHandicapGroup); HandicapGroupAddPayload addPayload = mock(HandicapGroupAddPayload.class); when(handicapGroupMapper.toDomainAddPayload(requestHandicapGroup)).thenReturn(addPayload); Response response = resources.client() .target("/handicapGroups") .request(MediaType.APPLICATION_JSON_TYPE) .post(requestEntity); assertThat(response.getStatus()).isEqualTo(HttpStatus.UNPROCESSABLE_ENTITY_422); ValidationErrorMessage validationErrorMessage = response.readEntity(ValidationErrorMessage.class); assertThat(validationErrorMessage.getErrors()) .isNotEmpty() .contains("factor must be less than or equal to 1.000"); verifyZeroInteractions(handicapGroupEntityService); }
private Response postHandicapGroup() throws Exception { AddHandicapGroupRequest request = objectMapper.readValue( FixtureHelpers.fixture("fixtures/api/entity/handicap_group_add-request.json"), AddHandicapGroupRequest.class ); Entity<AddHandicapGroupRequest> requestEntity = Entity.json(request); HandicapGroupAddPayload addPayload = mock(HandicapGroupAddPayload.class); when(handicapGroupMapper.toDomainAddPayload(request)).thenReturn(addPayload); HandicapGroup domainEntity = mock(HandicapGroup.class); when(handicapGroupEntityService.add(addPayload)).thenReturn(domainEntity); HandicapGroupApiEntity apiEntity = mock(HandicapGroupApiEntity.class); when(handicapGroupMapper.toApiEntity(domainEntity)).thenReturn(apiEntity); when(apiEntity.getId()).thenReturn(HANDICAP_GROUP_ID); Response response = resources.client() .target("/handicapGroups") .request(MediaType.APPLICATION_JSON_TYPE) .post(requestEntity); verify(handicapGroupEntityService).add(addPayload); verifyNoMoreInteractions(handicapGroupEntityService); return response; }
@Test public void testH2() throws Exception { JettyClientConfiguration h2conf = new JettyClientConfiguration(); Http2ClientTransportFactory h2transport = new Http2ClientTransportFactory(); h2transport.setTrustStorePath(ResourceHelpers.resourceFilePath("stores/h2_client.jts")); h2transport.setTrustStorePassword("h2_client"); h2transport.setValidatePeers(false); h2conf.setConnectionFactoryBuilder(h2transport); HttpClient client = new JettyClientBuilder(environment) .using(h2conf) .build(); String response = client.GET(String.format("https://127.0.0.1:%d/application/greet", h2.getLocalPort())) .getContentAsString(); assertThat(objectMapper.readTree(response)) .isEqualTo(objectMapper.readTree(FixtureHelpers.fixture("server_response.json"))); }
@Test public void testProcessSQS() throws Exception { final HttpRequestBase request = mock(HttpRequestBase.class); final S3ObjectInputStream stream = new S3ObjectInputStream(Resources .asByteSource( Resources.getResource("fixtures/s3_object.txt.gz")) .openStream(), request); final ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentEncoding("gzip"); final S3Object object = new S3Object(); object.setObjectMetadata(metadata); object.setObjectContent(stream); when(broadcaster.isEmpty()).thenReturn(false); when(s3.fetch(any(S3EventNotificationRecord.class))).thenReturn(object); message.setBody(FixtureHelpers.fixture("fixtures/sqs_records.json")); final boolean actual = processor.test(message); verify(broadcaster, times(2)).isEmpty(); verify(broadcaster, times(10)).test(anyString()); verify(s3).fetch(any(S3EventNotificationRecord.class)); verify(request, never()).abort(); assertThat(actual).isTrue(); }
@Test public void deserializesFromJSON() throws Exception { final Credential credential = Credential.builder() .key("abc") .symmetricKey("sym") .primary("user") .secondary("password") .updatedAt(DateTime.parse("2017-02-26T04:10:00").toDate()) .lastAccess(DateTime.parse("2017-02-26T04:10:00").toDate()) .createdAt(DateTime.parse("2017-02-23T18:15:20").toDate()) .build(); assertThat( MAPPER.readValue(FixtureHelpers.fixture("fixtures/credential.json"), Credential.class)) .isEqualToIgnoringGivenFields(credential, "key"); }
@Parameterized.Parameters(name = "{0}") public static Collection<Object[]> data() { List<Object[]> datas = new ArrayList<Object[]>(); ObjectMapper mapper = new ObjectMapper(); JsonTestCase[] testCases; try { testCases = mapper .readValue(FixtureHelpers.fixture("fixtures/jsonpatch/tests.json"), JsonTestCase[].class); for (JsonTestCase testCase : testCases) { datas.add(new Object[]{testCase}); } return datas; } catch (IOException e) { throw new RuntimeException(e); } }
@Test public void deserializesFromJSON() throws Exception { final Credential credential = Credential.builder() .symmetricKey("sym") .primary("user") .secondary("password").build(); assertThat(MAPPER.readValue(FixtureHelpers.fixture("fixtures/credential.json"), Credential.class)).isEqualTo(credential); }
@Test public void serializesToJSON() throws Exception { final Credential credential = Credential.builder() .symmetricKey("sym") .primary("user") .secondary("password").build(); assertThat(MAPPER.writeValueAsString(credential)) .isEqualTo(FixtureHelpers.fixture("fixtures/credential.json")); }
@Before public void setUp() throws Exception { final PetiteConfiguration configuration = MAPPER.readValue( FixtureHelpers.fixture("config_short_name_automagic.yml"), PetiteConfiguration.class); this.petiteManaged = new PetiteManaged(configuration, new MetricRegistry()); this.petiteManaged.start(); }
@Before public void setUp() throws Exception { this.metricRegistry = new MetricRegistry(); this.expectedConfiguration = new PetiteConfiguration(); this.expectedConfiguration.setAutomagicConfigurator(true); this.expectedConfiguration.setRegisterSelf(true); this.expectedConfiguration.setUseFullTypeNames(false); this.configuration = MAPPER.readValue( FixtureHelpers.fixture("config_short_name_automagic.yml"), PetiteConfiguration.class); }
@Test public void itShouldCreateRegistrationForEvent() throws Exception { final String eventId = EVENT_ID; AddRegistrationRequest apiRequest = objectMapper.readValue( FixtureHelpers.fixture("fixtures/api/entity/registration_add-request.json"), AddRegistrationRequest.class ); Entity<AddRegistrationRequest> requestEntity = Entity.json(apiRequest); RegistrationAddPayload addPayload = mock(RegistrationAddPayload.class); when(registrationMapper.toDomainAddPayload(apiRequest, TestConstants.EVENT_ID)).thenReturn(addPayload); Registration domainEntity = DomainEntityTestUtils.fullRegistration(); when(eventRegistrationService.add(addPayload)).thenReturn(domainEntity); RegistrationApiEntity apiEntity = ApiEntityTestUtils.fullRegistration(); when(registrationMapper.toApiEntity(domainEntity)).thenReturn(apiEntity); Response response = resources.client() .target("/events/" + eventId + "/registrations") .request(MediaType.APPLICATION_JSON_TYPE) .post(requestEntity); assertThat(response) .isNotNull(); assertThat(response.getStatus()).isEqualTo(HttpStatus.CREATED_201); assertThat(response.getHeaderString(HttpHeader.LOCATION.asString())) .containsSequence("/events/", eventId, "/registrations/", TestConstants.REGISTRATION_ID); verify(eventRegistrationService).add(addPayload); verifyNoMoreInteractions(eventRegistrationService); }
@Test public void itShouldAddRunWithoutRegistration() throws Exception { AddRunRequest apiRequest = objectMapper.readValue( FixtureHelpers.fixture("fixtures/api/entity/run_add-request_without-registration.json"), AddRunRequest.class ); Entity<AddRunRequest> requestEntity = Entity.json(apiRequest); RunAddPayload addPayload = mock(RunAddPayload.class); when(runMapper.toDomainAddPayload(apiRequest, EVENT_ID)).thenReturn(addPayload); Run domainEntity = DomainEntityTestUtils.fullRun(); domainEntity.setRegistration(null); when(runEntityService.add(addPayload)).thenReturn(domainEntity); RunApiEntity apiEntity = ApiEntityTestUtils.fullRun(); apiEntity.setRegistrationId(null); when(runMapper.toApiEntity(domainEntity)).thenReturn(apiEntity); Response response = resources.client() .target(UriBuilder.fromPath("/events/{eventId}/runs").build(EVENT_ID)) .request(MediaType.APPLICATION_JSON_TYPE) .post(requestEntity); verify(runEntityService).add(addPayload); verifyNoMoreInteractions(runEntityService); assertThat(response).isNotNull(); assertThat(response.getStatus()).isEqualTo(HttpStatus.CREATED_201); assertThat(response.getHeaderString(HttpHeader.LOCATION.asString())) .containsSequence("/events/", EVENT_ID, "/runs/", RUN_ID); }
@Test public void itShouldAddRunWithoutRawTime() throws Exception { AddRunRequest apiRequest = objectMapper.readValue( FixtureHelpers.fixture("fixtures/api/entity/run_add-request_without-raw-time.json"), AddRunRequest.class ); Entity<AddRunRequest> requestEntity = Entity.json(apiRequest); RunAddPayload addPayload = mock(RunAddPayload.class); when(runMapper.toDomainAddPayload(apiRequest, EVENT_ID)).thenReturn(addPayload); Run domainEntity = DomainEntityTestUtils.fullRun(); domainEntity.setRawTime(null); when(runEntityService.add(addPayload)).thenReturn(domainEntity); RunApiEntity apiEntity = ApiEntityTestUtils.fullRun(); apiEntity.setRawTime(null); when(runMapper.toApiEntity(domainEntity)).thenReturn(apiEntity); Response response = resources.client() .target(UriBuilder.fromPath("/events/{eventId}/runs").build(EVENT_ID)) .request(MediaType.APPLICATION_JSON_TYPE) .post(requestEntity); verify(runEntityService).add(addPayload); verifyNoMoreInteractions(runEntityService); assertThat(response).isNotNull(); assertThat(response.getStatus()).isEqualTo(HttpStatus.CREATED_201); assertThat(response.getHeaderString(HttpHeader.LOCATION.asString())) .containsSequence("/events/", EVENT_ID, "/runs/", RUN_ID); }
@Test public void itShouldAddRunWithoutPenalty() throws Exception { AddRunRequest apiRequest = objectMapper.readValue( FixtureHelpers.fixture("fixtures/api/entity/run_add-request_without-penalty.json"), AddRunRequest.class ); Entity<AddRunRequest> requestEntity = Entity.json(apiRequest); RunAddPayload addPayload = mock(RunAddPayload.class); when(runMapper.toDomainAddPayload(apiRequest, EVENT_ID)).thenReturn(addPayload); Run domainEntity = DomainEntityTestUtils.fullRun(); domainEntity.setDidNotFinish(false); when(runEntityService.add(addPayload)).thenReturn(domainEntity); RunApiEntity apiEntity = ApiEntityTestUtils.fullRun(); apiEntity.setDidNotFinish(false); when(runMapper.toApiEntity(domainEntity)).thenReturn(apiEntity); Response response = resources.client() .target(UriBuilder.fromPath("/events/{eventId}/runs").build(EVENT_ID)) .request(MediaType.APPLICATION_JSON_TYPE) .post(requestEntity); verify(runEntityService).add(addPayload); verifyNoMoreInteractions(runEntityService); assertThat(response).isNotNull(); assertThat(response.getStatus()).isEqualTo(HttpStatus.CREATED_201); assertThat(response.getHeaderString(HttpHeader.LOCATION.asString())) .containsSequence("/events/", EVENT_ID, "/runs/", RUN_ID); }
@Test public void deserializesFromJson() throws Exception { CompetitionGroupApiEntity actual = objectMapper.readValue( FixtureHelpers.fixture(fixturePath), CompetitionGroupApiEntity.class ); Assertions.assertThat(actual).isEqualTo(competitionGroupApiEntity); }
@Test public void testH2c() throws Exception { JettyClientConfiguration configuration = new JettyClientConfiguration(); configuration.setConnectionFactoryBuilder(new Http2ClearClientTransportFactory()); final HttpClient client = new JettyClientBuilder(environment) .using(configuration) .build(); String response = client.GET(String.format("http://127.0.0.1:%d/application/greet", h2c.getLocalPort())) .getContentAsString(); assertThat(objectMapper.readTree(response)) .isEqualTo(objectMapper.readTree(FixtureHelpers.fixture("server_response.json"))); }
@Test public void testConverter() throws Exception { final S3EventNotificationRecord record = MAPPER.readValue( FixtureHelpers.fixture("fixtures/amazon_event_record.json"), S3EventNotificationRecord.class); final AmazonS3Object actual = converter.convert(record); assertThat(actual.getRegion()).isEqualTo("us-east-1"); assertThat(actual.getBucketName()).isEqualTo("bucket-name"); assertThat(actual.getKey()).isEqualTo("object-key"); assertThat(actual.getSize()).isEqualTo(100); assertThat(actual.getETag().get()).isEqualTo("object eTag"); }
@Test public void testProcessS3EventFetchFailure() throws Exception { when(broadcaster.isEmpty()).thenReturn(false); when(s3.fetch(any(S3EventNotificationRecord.class))) .thenThrow(new AmazonServiceException("error")); message.setBody( FixtureHelpers.fixture("fixtures/sns_notification.json")); final boolean actual = processor.test(message); verify(broadcaster, times(2)).isEmpty(); verify(broadcaster, never()).test(anyString()); verify(s3).fetch(any(S3EventNotificationRecord.class)); assertThat(actual).isFalse(); }
@Test public void testProcessNoConnectionsAfterParse() throws Exception { when(broadcaster.isEmpty()).thenReturn(false, true); message.setBody( FixtureHelpers.fixture("fixtures/sns_notification.json")); final boolean actual = processor.test(message); verify(broadcaster, times(2)).isEmpty(); verify(broadcaster, never()).test(anyString()); verify(s3, never()).fetch(any(S3EventNotificationRecord.class)); assertThat(actual).isFalse(); }
@Test public void testProcessS3ZeroSizeFailure() throws Exception { when(broadcaster.isEmpty()).thenReturn(false); when(s3.fetch(any(S3EventNotificationRecord.class))) .thenThrow(new AmazonS3ZeroSizeException()); message.setBody( FixtureHelpers.fixture("fixtures/sns_notification.json")); final boolean actual = processor.test(message); verify(broadcaster, times(2)).isEmpty(); verify(broadcaster, never()).test(anyString()); verify(s3).fetch(any(S3EventNotificationRecord.class)); assertThat(actual).isTrue(); }
@Test public void testProcessS3ConstraintFailure() throws Exception { when(broadcaster.isEmpty()).thenReturn(false); when(s3.fetch(any(S3EventNotificationRecord.class))) .thenThrow(new AmazonS3ConstraintException()); message.setBody( FixtureHelpers.fixture("fixtures/sns_notification.json")); final boolean actual = processor.test(message); verify(broadcaster, times(2)).isEmpty(); verify(broadcaster, never()).test(anyString()); verify(s3).fetch(any(S3EventNotificationRecord.class)); assertThat(actual).isTrue(); }
@Test public void testProcessSNS() throws Exception { final HttpRequestBase request = mock(HttpRequestBase.class); final S3ObjectInputStream stream = new S3ObjectInputStream(Resources .asByteSource( Resources.getResource("fixtures/s3_object.txt.gz")) .openStream(), request); final ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentEncoding("gzip"); final S3Object object = new S3Object(); object.setObjectMetadata(metadata); object.setObjectContent(stream); when(broadcaster.isEmpty()).thenReturn(false); when(s3.fetch(any(S3EventNotificationRecord.class))).thenReturn(object); message.setBody( FixtureHelpers.fixture("fixtures/sns_notification.json")); final boolean actual = processor.test(message); verify(broadcaster, times(2)).isEmpty(); verify(broadcaster, times(10)).test(anyString()); verify(s3).fetch(any(S3EventNotificationRecord.class)); verify(request, never()).abort(); assertThat(actual).isTrue(); }