From 3511856f87670bbfe9d42bcb5e82e3f826f9ea1c Mon Sep 17 00:00:00 2001 From: slaurenz <82034561+slaurenz@users.noreply.github.com> Date: Thu, 24 Mar 2022 15:21:02 +0100 Subject: [PATCH] Added unit tests for trusted issuer --- .../TrustedIssuerDownloadConnectorTest.java | 94 +++++++++++++++++++ .../dgc/testdata/TrustedIssuerTestHelper.java | 66 +++++++++++++ 2 files changed, 160 insertions(+) create mode 100644 src/test/java/eu/europa/ec/dgc/gateway/connector/TrustedIssuerDownloadConnectorTest.java create mode 100644 src/test/java/eu/europa/ec/dgc/testdata/TrustedIssuerTestHelper.java diff --git a/src/test/java/eu/europa/ec/dgc/gateway/connector/TrustedIssuerDownloadConnectorTest.java b/src/test/java/eu/europa/ec/dgc/gateway/connector/TrustedIssuerDownloadConnectorTest.java new file mode 100644 index 0000000..1f93ce7 --- /dev/null +++ b/src/test/java/eu/europa/ec/dgc/gateway/connector/TrustedIssuerDownloadConnectorTest.java @@ -0,0 +1,94 @@ +/*- + * ---license-start + * EU Digital Green Certificate Gateway Service / dgc-lib + * --- + * 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.gateway.connector; + +import eu.europa.ec.dgc.gateway.connector.client.DgcGatewayConnectorRestClient; +import eu.europa.ec.dgc.gateway.connector.model.TrustedIssuer; +import eu.europa.ec.dgc.testdata.TrustedIssuerTestHelper; +import feign.FeignException; +import feign.Request; +import feign.RequestTemplate; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.Assertions; +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.mock.mockito.MockBean; +import org.springframework.http.ResponseEntity; + +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.when; + +@SpringBootTest +@Slf4j +class TrustedIssuerDownloadConnectorTest { + + @MockBean + DgcGatewayConnectorRestClient restClientMock; + + @Autowired + DgcGatewayTrustedIssuerDownloadConnector connector; + + @Autowired + TrustedIssuerTestHelper trustedIssuerTestHelper; + + @Test + void testDownloadOfTrustedIssuerList() throws Exception { + Map param = new HashMap<>(); + + when(restClientMock.downloadTrustedIssuers(param)) + .thenReturn(ResponseEntity.ok( + List.of(trustedIssuerTestHelper.createTrustedIssuer("DE"), + trustedIssuerTestHelper.createTrustedIssuer("EU")))); + + List result = connector.getTrustedIssuers(); + Assertions.assertEquals(2, result.size()); + Assertions.assertEquals("DE", result.get(0).getCountry()); + Assertions.assertEquals("EU", result.get(1).getCountry()); + + } + + @Test + void shouldReturnEmptyListWhenDownloadFails() { + Map param = new HashMap<>(); + + when(restClientMock.downloadTrustedIssuers(param)) + .thenReturn(ResponseEntity.status(500).build()); + + Assertions.assertEquals(0, connector.getTrustedIssuers().size()); + + doThrow(new FeignException.InternalServerError("", dummyRequest(), null, null)) + .when(restClientMock).downloadCountryList(); + + Assertions.assertEquals(0, connector.getTrustedIssuers().size()); + } + + /** + * Method to create dummy request which is required to throw FeignExceptions. + */ + private Request dummyRequest() { + return Request.create(Request.HttpMethod.GET, "url", new HashMap<>(), null, new RequestTemplate()); + } + +} diff --git a/src/test/java/eu/europa/ec/dgc/testdata/TrustedIssuerTestHelper.java b/src/test/java/eu/europa/ec/dgc/testdata/TrustedIssuerTestHelper.java new file mode 100644 index 0000000..1b1ff39 --- /dev/null +++ b/src/test/java/eu/europa/ec/dgc/testdata/TrustedIssuerTestHelper.java @@ -0,0 +1,66 @@ +/*- + * ---license-start + * EU Digital Green Certificate Gateway Service / dgc-gateway + * --- + * Copyright (C) 2021 - 2022 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.testdata; + + +import eu.europa.ec.dgc.gateway.connector.dto.TrustedIssuerDto; +import eu.europa.ec.dgc.signing.SignedStringMessageBuilder; +import lombok.RequiredArgsConstructor; +import org.bouncycastle.cert.X509CertificateHolder; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +public class TrustedIssuerTestHelper { + + private final DgcTestKeyStore testKeyStore; + + public TrustedIssuerDto createTrustedIssuer(final String country) throws Exception { + TrustedIssuerDto trustedIssuer = new TrustedIssuerDto(); + trustedIssuer.setUrl("https://trusted.issuer"); + trustedIssuer.setName("tiName"); + trustedIssuer.setCountry(country); + trustedIssuer.setType(TrustedIssuerDto.UrlTypeDto.HTTP); + trustedIssuer.setSslPublicKey("pubKey"); + trustedIssuer.setThumbprint("thumbprint"); + trustedIssuer.setKeyStorageType("JWKS"); + final String signature = signString(getHashData(trustedIssuer)); + trustedIssuer.setSignature(signature); + + return trustedIssuer; + } + + private String getHashData(TrustedIssuerDto entity) { + return entity.getUuid()+ ";" + + entity.getCountry() + ";" + + entity.getName() + ";" + + entity.getUrl() + ";" + + entity.getType().name() + ";"; + } + + public String signString(final String hashdata) throws Exception { + return new SignedStringMessageBuilder() + .withPayload(hashdata) + .withSigningCertificate(new X509CertificateHolder(testKeyStore.getTrustAnchor().getEncoded()), testKeyStore.getTrustAnchorPrivateKey()) + .buildAsString(); + } + +}