Skip to content

Commit

Permalink
Added unit tests for trusted issuer
Browse files Browse the repository at this point in the history
  • Loading branch information
slaurenz committed Mar 24, 2022
1 parent 4dd0997 commit 3511856
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<String,String> param = new HashMap<>();

when(restClientMock.downloadTrustedIssuers(param))
.thenReturn(ResponseEntity.ok(
List.of(trustedIssuerTestHelper.createTrustedIssuer("DE"),
trustedIssuerTestHelper.createTrustedIssuer("EU"))));

List<TrustedIssuer> 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<String,String> 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());
}

}
Original file line number Diff line number Diff line change
@@ -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();
}

}

0 comments on commit 3511856

Please sign in to comment.