Skip to content

Commit

Permalink
Return Batch ID on Revocation List Upload (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
f11h authored Jan 11, 2022
1 parent 8b15d91 commit f35dab5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package eu.europa.ec.dgc.gateway.restapi.controller;

import eu.europa.ec.dgc.gateway.config.OpenApiConfig;
import eu.europa.ec.dgc.gateway.entity.RevocationBatchEntity;
import eu.europa.ec.dgc.gateway.exception.DgcgResponseException;
import eu.europa.ec.dgc.gateway.model.RevocationBatchDownload;
import eu.europa.ec.dgc.gateway.restapi.converter.CmsStringMessageConverter;
Expand Down Expand Up @@ -209,7 +210,8 @@ public ResponseEntity<String> downloadBatch(
responses = {
@ApiResponse(
responseCode = "201",
description = "Batch created."),
description = "Batch created.",
headers = @Header(name = HttpHeaders.ETAG, description = "Batch ID of created Batch")),
@ApiResponse(
responseCode = "409",
description = "Batch already exists.")
Expand All @@ -224,13 +226,17 @@ public ResponseEntity<Void> uploadBatch(
"Submitted string needs to be signed by a valid upload certificate");
}

String batchId;

try {
revocationListService.addRevocationBatch(
RevocationBatchEntity entity = revocationListService.addRevocationBatch(
batch.getPayloadString(),
batch.getSignerCertificate(),
batch.getRawMessage(),
countryCode
);

batchId = entity.getBatchId();
} catch (RevocationListService.RevocationBatchServiceException e) {
log.error("Upload of Revocation Batch failed: {}, {}", e.getReason(), e.getMessage());

Expand All @@ -254,7 +260,10 @@ public ResponseEntity<Void> uploadBatch(
}
}

return ResponseEntity.status(HttpStatus.CREATED).build();
return ResponseEntity
.status(HttpStatus.CREATED)
.header(HttpHeaders.ETAG, batchId)
.build();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

Expand Down Expand Up @@ -63,8 +64,10 @@
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;

@SpringBootTest
@AutoConfigureMockMvc
Expand Down Expand Up @@ -139,13 +142,14 @@ void testSuccessfulUpload() throws Exception {

String authCertHash = trustedPartyTestHelper.getHash(TrustedPartyEntity.CertificateType.AUTHENTICATION, countryCode);

mockMvc.perform(post("/revocation-list")
.content(payload)
.contentType("application/cms")
.header(dgcConfigProperties.getCertAuth().getHeaderFields().getThumbprint(), authCertHash)
.header(dgcConfigProperties.getCertAuth().getHeaderFields().getDistinguishedName(), authCertSubject)
)
.andExpect(status().isCreated());
MvcResult mvcResult = mockMvc.perform(post("/revocation-list")
.content(payload)
.contentType("application/cms")
.header(dgcConfigProperties.getCertAuth().getHeaderFields().getThumbprint(), authCertHash)
.header(dgcConfigProperties.getCertAuth().getHeaderFields().getDistinguishedName(), authCertSubject)
).andReturn();

Assertions.assertEquals(HttpStatus.CREATED.value(), mvcResult.getResponse().getStatus());

Assertions.assertEquals(revocationBatchesInDb + 1, revocationBatchRepository.count());
Optional<RevocationBatchEntity> createdRevocationBatch =
Expand All @@ -161,6 +165,7 @@ void testSuccessfulUpload() throws Exception {
Assertions.assertEquals(countryCode, createdRevocationBatch.get().getCountry());
Assertions.assertEquals(revocationBatchDto.getHashType().name(), createdRevocationBatch.get().getType().name());
Assertions.assertEquals(revocationBatchDto.getKid(), createdRevocationBatch.get().getKid());
Assertions.assertEquals(createdRevocationBatch.get().getBatchId(), mvcResult.getResponse().getHeader(HttpHeaders.ETAG));
Assertions.assertEquals(36, createdRevocationBatch.get().getBatchId().length());

SignedStringMessageParser parser = new SignedStringMessageParser(createdRevocationBatch.get().getSignedBatch());
Expand Down Expand Up @@ -192,6 +197,7 @@ void testUploadFailedInvalidJson() throws Exception {
)
.andExpect(status().isBadRequest());


Assertions.assertEquals(revocationBatchesInDb, revocationBatchRepository.count());
Assertions.assertEquals(auditEventEntitiesInDb, auditEventRepository.count());
}
Expand Down Expand Up @@ -230,7 +236,8 @@ void testUploadFailedInvalidJsonValues() throws Exception {
.header(dgcConfigProperties.getCertAuth().getHeaderFields().getThumbprint(), authCertHash)
.header(dgcConfigProperties.getCertAuth().getHeaderFields().getDistinguishedName(), authCertSubject)
)
.andExpect(status().isBadRequest());
.andExpect(status().isBadRequest())
.andExpect(header().doesNotExist(HttpHeaders.ETAG));

Assertions.assertEquals(revocationBatchesInDb, revocationBatchRepository.count());
Assertions.assertEquals(auditEventEntitiesInDb, auditEventRepository.count());
Expand Down Expand Up @@ -270,7 +277,8 @@ void testUploadFailedInvalidJsonValuesInHashEntries() throws Exception {
.header(dgcConfigProperties.getCertAuth().getHeaderFields().getThumbprint(), authCertHash)
.header(dgcConfigProperties.getCertAuth().getHeaderFields().getDistinguishedName(), authCertSubject)
)
.andExpect(status().isBadRequest());
.andExpect(status().isBadRequest())
.andExpect(header().doesNotExist(HttpHeaders.ETAG));

Assertions.assertEquals(revocationBatchesInDb, revocationBatchRepository.count());
Assertions.assertEquals(auditEventEntitiesInDb, auditEventRepository.count());
Expand Down Expand Up @@ -310,7 +318,8 @@ void testUploadFailedInvalidCountry() throws Exception {
.header(dgcConfigProperties.getCertAuth().getHeaderFields().getThumbprint(), authCertHash)
.header(dgcConfigProperties.getCertAuth().getHeaderFields().getDistinguishedName(), authCertSubject)
)
.andExpect(status().isForbidden());
.andExpect(status().isForbidden())
.andExpect(header().doesNotExist(HttpHeaders.ETAG));

Assertions.assertEquals(revocationBatchesInDb, revocationBatchRepository.count());
Assertions.assertEquals(auditEventEntitiesInDb, auditEventRepository.count());
Expand Down

0 comments on commit f35dab5

Please sign in to comment.