-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix code smells * Add ContextController unit test * Added Unit test for SignerCertificateDownloadServiceImpl * Code smell
- Loading branch information
Showing
5 changed files
with
116 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...t/java/eu/europa/ec/dgc/verifier/restapi/controller/ContextControllerIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} | ||
|
59 changes: 59 additions & 0 deletions
59
...test/java/eu/europa/ec/dgc/verifier/service/SignerCertificateDownloadServiceImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |