Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
epicsoft-llc committed Sep 23, 2021
1 parent c9b7461 commit d067b42
Show file tree
Hide file tree
Showing 9 changed files with 464 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
@RequiredArgsConstructor
public class RejectController {

private static final String PATH = "/reject";
static final String PATH = "/reject";

private final AccessTokenService accessTokenService;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
@RequiredArgsConstructor
public class ValidationStatusController {

private static final String PATH = "/status";
static final String PATH = "/status";

private final ValidationStatusService validationStatusService;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public ValidationServiceIdentityResponse identity(final ServiceProperties servic
.toUriString();

log.debug("REST Call to '{}' starting", url);
final ResponseEntity<ValidationServiceIdentityResponse> response = restTpl
final ResponseEntity<ValidationServiceIdentityResponse> response = this.restTpl
.getForEntity(url, ValidationServiceIdentityResponse.class);
return response.getBody();
}
Expand Down Expand Up @@ -101,12 +101,12 @@ public ValidationServiceInitializeResponse initialize(

final HttpHeaders headers = new HttpHeaders();
headers.add("X-Version", "1.0");
headers.add("Authorization", accessTokenService.buildHeaderToken(subject));
headers.add("Authorization", this.accessTokenService.buildHeaderToken(subject));

final HttpEntity<ValidationServiceInitializeRequest> entity = new HttpEntity<>(body, headers);

log.debug("REST Call to '{}' starting", url);
final ResponseEntity<ValidationServiceInitializeResponse> response = restTpl
final ResponseEntity<ValidationServiceInitializeResponse> response = this.restTpl
.exchange(url, HttpMethod.PUT, entity, ValidationServiceInitializeResponse.class);
return response.getBody();
}
Expand All @@ -124,12 +124,12 @@ public ValidationServiceStatusResponse status(final ServiceProperties service, f

final HttpHeaders headers = new HttpHeaders();
headers.add("X-Version", "1.0");
headers.add("Authorization", accessTokenService.buildHeaderToken(subject));
headers.add("Authorization", this.accessTokenService.buildHeaderToken(subject));

final HttpEntity<String> entity = new HttpEntity<>(headers);

log.debug("REST Call to '{}' starting", url);
final ResponseEntity<String> response = restTpl.exchange(url, HttpMethod.GET, entity, String.class);
final ResponseEntity<String> response = this.restTpl.exchange(url, HttpMethod.GET, entity, String.class);
switch (response.getStatusCode()) {
case OK:
return new ValidationServiceStatusResponse(response.getStatusCodeValue(), response.getBody());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,13 @@
import eu.europa.ec.dgc.validation.decorator.dto.DccTokenRequest;
import eu.europa.ec.dgc.validation.decorator.dto.IdentityResponse.ServiceIdentityResponse;
import eu.europa.ec.dgc.validation.decorator.entity.ServiceTokenContentResponse;
import eu.europa.ec.dgc.validation.decorator.entity.ServiceTokenContentResponse.OccurrenceInfoResponse;
import eu.europa.ec.dgc.validation.decorator.entity.ServiceTokenContentResponse.SubjectResponse;
import eu.europa.ec.dgc.validation.decorator.entity.ValidationServiceInitializeResponse;
import eu.europa.ec.dgc.validation.decorator.repository.BackendRepository;
import eu.europa.ec.dgc.validation.decorator.repository.ValidationServiceRepository;
import eu.europa.ec.dgc.validation.decorator.service.AccessTokenService;
import eu.europa.ec.dgc.validation.decorator.service.IdentityService;
import eu.europa.ec.dgc.validation.decorator.util.TestHelper;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.util.Arrays;
import java.util.Base64;
import java.util.Map;
import java.util.UUID;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -85,10 +81,11 @@ public void before() {
this.subject = UUID.randomUUID().toString();
this.service = this.identityService.getIdentity("service", "ValidationService").getService().get(0);

final ValidationServiceInitializeResponse initialize = this.buildValidationServiceInitializeMock();
final ValidationServiceInitializeResponse initialize = this.buildValidationServiceInitialize();
when(this.validationServiceRepositoryMock.initialize(any(), any(), any(), any())).thenReturn(initialize);

final ServiceTokenContentResponse tokenContent = this.buildServiceTokenContentMock();
final ServiceTokenContentResponse tokenContent = TestHelper.buildServiceTokenContent(
this.subject, this.service);
when(this.backendRepository.tokenContent(any())).thenReturn(tokenContent);
when(this.backendRepository.tokenContent(any(), any())).thenReturn(tokenContent);
}
Expand All @@ -115,7 +112,11 @@ void token_withValidTokenAndService_successResponse() {
assertThat(result).isNotNull();
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(result.getBody()).isNotBlank();
assertThat(accessTokenService.isValid(result.getBody())).isTrue();
assertThat(this.accessTokenService.isValid(result.getBody())).isTrue();
// AND header
assertThat(result.getHeaders()).containsKeys("X-Nonce", "Cache-Control");
assertThat(result.getHeaders().get("X-Nonce")).isNotNull().isNotEmpty();
assertThat(result.getHeaders().get("Cache-Control")).contains("no-cache");
// AND tokenContent
final Map<String, Object> tokenContent = accessTokenService.parseAccessToken(result.getBody());
assertThat(tokenContent)
Expand All @@ -128,45 +129,11 @@ void token_withValidTokenAndService_successResponse() {
"validationClock", "validFrom", "validTo");
}

private ValidationServiceInitializeResponse buildValidationServiceInitializeMock() {
private ValidationServiceInitializeResponse buildValidationServiceInitialize() {
final ValidationServiceInitializeResponse initialize = new ValidationServiceInitializeResponse();
initialize.setSubject(subject);
initialize.setSubject(this.subject);
initialize.setExp(Instant.now().plusSeconds(60).toEpochMilli());
initialize.setAud("ValidationServiceInitializeResponse");
return initialize;
}

private ServiceTokenContentResponse buildServiceTokenContentMock() {
final String serviceId = Base64.getUrlEncoder().withoutPadding().encodeToString(service.getId().getBytes());
final SubjectResponse subjectResponse = new SubjectResponse();
subjectResponse.setId(UUID.fromString(subject));
subjectResponse.setForename("Lionel");
subjectResponse.setLastname("Kuhic");
subjectResponse.setBirthDate("1994-05-25");
subjectResponse.setServiceIdUsed(serviceId);
subjectResponse.setJti(UUID.randomUUID().toString());

final OffsetDateTime departureTime = OffsetDateTime.now().plusDays(1);
final OccurrenceInfoResponse occurrenceInfo = new OccurrenceInfoResponse();
occurrenceInfo.setFrom("East Kizzieshire");
occurrenceInfo.setTo("South Duncanhaven");
occurrenceInfo.setTime(departureTime);
occurrenceInfo.setType(2);
occurrenceInfo.setCategories(Arrays.asList("Standard"));
occurrenceInfo.setConditionTypes(Arrays.asList("r", "v", "t"));
occurrenceInfo.setCountryOfArrival("TT");
occurrenceInfo.setRegionOfArrival("TT");
occurrenceInfo.setCountryOfDeparture("TD");
occurrenceInfo.setRegionOfDeparture("TD");
occurrenceInfo.setDepartureTime(departureTime);
occurrenceInfo.setArrivalTime(departureTime.plusHours(8).plusMinutes(24));
occurrenceInfo.setLanguage("en-en");

final ServiceTokenContentResponse tokenContent = new ServiceTokenContentResponse();
tokenContent.setReference("TestBookingReference");
tokenContent.setTime(OffsetDateTime.now());
tokenContent.getSubjects().add(subjectResponse);
tokenContent.setFlightInfo(occurrenceInfo);
return tokenContent;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.util.UriComponentsBuilder;

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
Expand All @@ -45,36 +48,51 @@ class IdentityControllerTest {
void identityAll_withoutVariales_successWithAllIdentities() {
// GIVEN
final String url = UriComponentsBuilder.fromUriString("http://localhost")
.port(port)
.port(this.port)
.path(IdentityController.PATH_ALL)
.toUriString();
// WHEN
final IdentityResponse result = restTpl.getForObject(url, IdentityResponse.class);
final ResponseEntity<IdentityResponse> result = this.restTpl.exchange(
url, HttpMethod.GET, null, IdentityResponse.class);
// THEN
assertThat(result).isNotNull();
assertThat(result.getVerificationMethod()).hasSizeGreaterThanOrEqualTo(4);
assertThat(result.getService()).hasSizeGreaterThanOrEqualTo(4);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
// AND header
assertThat(result.getHeaders()).containsKeys("Cache-Control");
assertThat(result.getHeaders().get("Cache-Control")).contains("no-cache");
// AND body / identity
final IdentityResponse identity = result.getBody();
assertThat(identity).isNotNull();
assertThat(identity.getVerificationMethod()).hasSizeGreaterThanOrEqualTo(4);
assertThat(identity.getService()).hasSizeGreaterThanOrEqualTo(4);
}

@ParameterizedTest
@ValueSource(strings = { "verificationMethod", "service" })
void identityElement_withElement_successWithElement(final String element) {
// GIVEN
final String url = UriComponentsBuilder.fromUriString("http://localhost")
.port(port)
.port(this.port)
.path(IdentityController.PATH_ELEMENT.replace("{element}", element))
.toUriString();
// WHEN
final IdentityResponse result = restTpl.getForObject(url, IdentityResponse.class);
final ResponseEntity<IdentityResponse> result = this.restTpl.exchange(
url, HttpMethod.GET, null, IdentityResponse.class);
// THEN
assertThat(result).isNotNull();
// AND
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
// AND header
assertThat(result.getHeaders()).containsKeys("Cache-Control");
assertThat(result.getHeaders().get("Cache-Control")).contains("no-cache");
// AND body / identity
final IdentityResponse identity = result.getBody();
assertThat(identity).isNotNull();
if ("verificationMethod".equals(element)) {
assertThat(result.getVerificationMethod()).hasSizeGreaterThanOrEqualTo(4);
assertThat(result.getService()).hasSize(0);
assertThat(identity.getVerificationMethod()).hasSizeGreaterThanOrEqualTo(4);
assertThat(identity.getService()).hasSize(0);
} else {
assertThat(result.getVerificationMethod()).hasSize(0);
assertThat(result.getService()).hasSizeGreaterThanOrEqualTo(4);
assertThat(identity.getVerificationMethod()).hasSize(0);
assertThat(identity.getService()).hasSizeGreaterThanOrEqualTo(4);
}
}

Expand All @@ -85,14 +103,22 @@ void identityType_withServiceAndType_successWithServiceAndType(final String type
// GIVEN
String element = "service";
final String url = UriComponentsBuilder.fromUriString("http://localhost")
.port(port)
.port(this.port)
.path(IdentityController.PATH_ELEMENT_TYPE.replace("{element}", element).replace("{type}", type))
.toUriString();
// WHEN
final IdentityResponse result = restTpl.getForObject(url, IdentityResponse.class);
final ResponseEntity<IdentityResponse> result = restTpl.exchange(url, HttpMethod.GET, null,
IdentityResponse.class);
// THEN
assertThat(result).isNotNull();
assertThat(result.getVerificationMethod()).hasSize(0);
assertThat(result.getService()).hasSize(1);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
// AND header
assertThat(result.getHeaders()).containsKeys("Cache-Control");
assertThat(result.getHeaders().get("Cache-Control")).contains("no-cache");
// AND body / identity
final IdentityResponse identity = result.getBody();
assertThat(identity).isNotNull();
assertThat(identity.getVerificationMethod()).hasSize(0);
assertThat(identity.getService()).hasSize(1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.util.UriComponentsBuilder;

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
Expand All @@ -41,34 +44,42 @@ class InitializeControllerTest {

@Autowired
private TestRestTemplate restTpl;

@Autowired
private IdentityProperties properties;

@Autowired
private AccessTokenService accessTokenService;

@Test
void initialize_withRandomSubject_successQrCode() {
// GIVEN
final String subject = UUID.randomUUID().toString();
final String url = UriComponentsBuilder.fromUriString("http://localhost")
.port(port)
.port(this.port)
.path(InitializeController.PATH.replace("{subject}", subject))
.toUriString();
// WHEN
final QrCodeDto result = restTpl.getForObject(url, QrCodeDto.class);
// WHEN
final ResponseEntity<QrCodeDto> result = this.restTpl.exchange(
url, HttpMethod.GET, null, QrCodeDto.class);
// THEN
assertThat(result).isNotNull();
assertThat(result.getProtocol()).isEqualTo(this.properties.getProtocol());
assertThat(result.getProtocolVersion()).isEqualTo(this.properties.getProtocolVersion());
assertThat(result.getServiceIdentity()).isEqualTo(this.properties.getServiceIdentityUrl());
assertThat(result.getPrivacyUrl()).isEqualTo(this.properties.getPrivacyUrl());
assertThat(result.getConsent()).isEqualTo(this.properties.getConsent());
assertThat(result.getSubject()).isEqualTo(subject);
assertThat(result.getServiceProvider()).isEqualTo(this.properties.getServiceProvider());
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
// AND header
assertThat(result.getHeaders()).containsKeys("Cache-Control");
assertThat(result.getHeaders().get("Cache-Control")).contains("no-cache");
// AND body
final QrCodeDto qrCode = result.getBody();
assertThat(qrCode).isNotNull();
assertThat(qrCode.getProtocol()).isEqualTo(this.properties.getProtocol());
assertThat(qrCode.getProtocolVersion()).isEqualTo(this.properties.getProtocolVersion());
assertThat(qrCode.getServiceIdentity()).isEqualTo(this.properties.getServiceIdentityUrl());
assertThat(qrCode.getPrivacyUrl()).isEqualTo(this.properties.getPrivacyUrl());
assertThat(qrCode.getConsent()).isEqualTo(this.properties.getConsent());
assertThat(qrCode.getSubject()).isEqualTo(subject);
assertThat(qrCode.getServiceProvider()).isEqualTo(this.properties.getServiceProvider());
// AND token
assertThat(result.getToken()).isNotBlank();
assertThat(accessTokenService.isValid(result.getToken())).isTrue();
assertThat(qrCode.getToken()).isNotBlank();
assertThat(this.accessTokenService.isValid(qrCode.getToken())).isTrue();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*-
* ---license-start
* European Digital COVID Certificate Validation Decorator Service / dgca-validation-decorator
* ---
* Copyright (C) 2021 T-Systems International GmbH and all other contributors
* ---
* 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.
* ---license-end
*/

package eu.europa.ec.dgc.validation.decorator.controller;

import static org.assertj.core.api.Assertions.assertThat;
import eu.europa.ec.dgc.validation.decorator.dto.DccTokenRequest;
import eu.europa.ec.dgc.validation.decorator.service.AccessTokenService;
import java.util.UUID;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.util.UriComponentsBuilder;

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class RejectControllerTest {

@LocalServerPort
private int port;

@Autowired
private TestRestTemplate restTpl;

@Autowired
private AccessTokenService accessTokenService;

@Test
void reject_withValidToken_successResponse() {
// GIVEN
final String subject = UUID.randomUUID().toString();
final String token = this.accessTokenService.buildHeaderToken(subject);
// AND
final String url = UriComponentsBuilder.fromUriString("http://localhost")
.port(this.port)
.path(RejectController.PATH)
.toUriString();
final HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", token);
final HttpEntity<DccTokenRequest> entity = new HttpEntity<>(headers);
// WHEN
final ResponseEntity<Void> result = this.restTpl.exchange(url, HttpMethod.GET, entity, Void.class);
// THEN
assertThat(result).isNotNull();
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(result.getBody()).isNull();
// AND header
assertThat(result.getHeaders()).containsKeys("Cache-Control");
assertThat(result.getHeaders().get("Cache-Control")).contains("no-cache");
}
}
Loading

0 comments on commit d067b42

Please sign in to comment.