Skip to content

Commit

Permalink
Updated Unit Tests (#37)
Browse files Browse the repository at this point in the history
* Fix code smells

* Add ContextController unit test

* Added Unit test for SignerCertificateDownloadServiceImpl

* Code smell
  • Loading branch information
slaurenz authored May 20, 2021
1 parent a81d581 commit 5f38ea5
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
package eu.europa.ec.dgc.verifier;

import eu.europa.ec.dgc.verifier.config.DgcConfigProperties;
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.info.Info;
import io.swagger.v3.oas.annotations.info.License;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import io.swagger.v3.oas.models.info.License;
import lombok.Generated;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.info.BuildProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/eu/europa/ec/dgc/verifier/OpenApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
},
webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT
)
public class OpenApiTest {
class OpenApiTest {

@MockBean
private DgcGatewayDownloadConnector dgcGatewayDownloadConnector;

@Test
public void apiDocs() {
void apiDocs() {
try (BufferedInputStream in = new BufferedInputStream(new URL("http://localhost:8080/openapi").openStream());
FileOutputStream out = new FileOutputStream("target/openapi.json")) {
byte[] buffer = new byte[1024];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package eu.europa.ec.dgc.verifier.restapi.controller;

import eu.europa.ec.dgc.gateway.connector.DgcGatewayDownloadConnector;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@SpringBootTest
@AutoConfigureMockMvc
class ContextControllerIntegrationTest {

@MockBean
DgcGatewayDownloadConnector dgcGatewayDownloadConnector;

@Autowired
private MockMvc mockMvc;

@Test
void requestContext() throws Exception {
mockMvc.perform(get("/context"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(result -> assertContextStrEqualFile(result));
}

private void assertContextStrEqualFile(MvcResult result) throws UnsupportedEncodingException {
String resultContext = result.getResponse().getContentAsString();
Resource resource = new ClassPathResource("/static/context.json");
String fileContext = null;
try {
fileContext = IOUtils.toString(resource.getInputStream(), StandardCharsets.UTF_8);
} catch (IOException e) {
Assertions.fail(e);
}
Assertions.assertEquals(resultContext, fileContext);
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package eu.europa.ec.dgc.verifier.service;

import eu.europa.ec.dgc.gateway.connector.DgcGatewayDownloadConnector;
import eu.europa.ec.dgc.gateway.connector.model.TrustListItem;
import eu.europa.ec.dgc.verifier.entity.SignerInformationEntity;
import eu.europa.ec.dgc.verifier.repository.SignerInformationRepository;
import eu.europa.ec.dgc.verifier.testdata.SignerInformationTestHelper;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;


@SpringBootTest
class SignerCertificateDownloadServiceImplTest {

@MockBean
DgcGatewayDownloadConnector dgcGatewayDownloadConnector;

@Autowired
SignerCertificateDownloadServiceImpl signerCertificateDownloadService;

@Autowired
SignerInformationRepository signerInformationRepository;

@Autowired
SignerInformationTestHelper signerInformationTestHelper;

@Test
void downloadEmptyCertificatesList() {
ArrayList<TrustListItem> trustList = new ArrayList<>();
Mockito.when(dgcGatewayDownloadConnector.getTrustedCertificates()).thenReturn(trustList);

signerCertificateDownloadService.downloadCertificates();

List<SignerInformationEntity> repositoryItems = signerInformationRepository.findAll();
Assertions.assertEquals(0, repositoryItems.size());
}

@Test
void downloadCertificates() {
ArrayList<TrustListItem> trustList = new ArrayList<>();
trustList.add(signerInformationTestHelper.createTrustListItem(SignerInformationTestHelper.TEST_CERT_1_STR));
Mockito.when(dgcGatewayDownloadConnector.getTrustedCertificates()).thenReturn(trustList);

signerCertificateDownloadService.downloadCertificates();

List<SignerInformationEntity> repositoryItems = signerInformationRepository.findAll();
Assertions.assertEquals(1, repositoryItems.size());

SignerInformationEntity repositoryItem = repositoryItems.get(0);
Assertions.assertEquals(SignerInformationTestHelper.TEST_CERT_1_KID, repositoryItem.getKid());
Assertions.assertEquals(SignerInformationTestHelper.TEST_CERT_1_STR, repositoryItem.getRawData());
}
}

0 comments on commit 5f38ea5

Please sign in to comment.