Skip to content

Commit

Permalink
* changed revocation handling to work with the DTOs
Browse files Browse the repository at this point in the history
  • Loading branch information
Morphyum committed Mar 14, 2022
1 parent 2da5118 commit 5ff9fc4
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import eu.europa.ec.dgc.gateway.connector.client.DgcGatewayConnectorRestClient;
import eu.europa.ec.dgc.gateway.connector.config.DgcGatewayConnectorConfigProperties;
import eu.europa.ec.dgc.gateway.connector.dto.ProblemReportDto;
import eu.europa.ec.dgc.gateway.connector.dto.RevocationBatchDeleteRequestDto;
import eu.europa.ec.dgc.gateway.connector.dto.RevocationBatchDto;
import eu.europa.ec.dgc.signing.SignedStringMessageBuilder;
import eu.europa.ec.dgc.utils.CertificateUtils;
import feign.FeignException;
Expand Down Expand Up @@ -102,18 +105,23 @@ void init() throws KeyStoreException, CertificateEncodingException, IOException
/**
* Uploads a JSON-File as RevocationBatch to DGC Gateway.
*
* @param json the JSON containing the ValidationRUle to upload.
* @param revocationBatchDto the RevocationBatchDto to upload.
* @throws DgcRevocationBatchUploadException with detailed information why the upload has failed.
*/
public void uploadRevocationBatch(String json) throws DgcRevocationBatchUploadException {
public String uploadRevocationBatch(RevocationBatchDto revocationBatchDto)
throws DgcRevocationBatchUploadException, JsonProcessingException {

String payload = new SignedStringMessageBuilder().withPayload(json)
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
String jsonString = mapper.writeValueAsString(revocationBatchDto);
String payload = new SignedStringMessageBuilder().withPayload(jsonString)
.withSigningCertificate(uploadCertificateHolder, uploadCertificatePrivateKey).buildAsString();

try {
ResponseEntity<Void> response = dgcGatewayConnectorRestClient.uploadBatch(payload);
if (response.getStatusCode() == HttpStatus.CREATED) {
log.info("Successfully uploaded RevocationBatch");
return response.getHeaders().getETag();
}
} catch (FeignException e) {
if (e.status() == HttpStatus.BAD_REQUEST.value()) {
Expand All @@ -126,17 +134,24 @@ public void uploadRevocationBatch(String json) throws DgcRevocationBatchUploadEx
DgcRevocationBatchUploadException.Reason.INVALID_AUTHORIZATION);
}
}
return null;
}

/**
* Deletes a RevocationBatch with given ID from DGC Gateway.
*
* @param ruleId The ID of the Rules to be deleted.
* @param batchId The ID of the batch to be deleted.
* @throws DgcRevocationBatchUploadException with detailed information why the delete has failed.
*/
public void deleteRevocationBatch(String ruleId) throws DgcRevocationBatchUploadException {
public void deleteRevocationBatch(String batchId) throws DgcRevocationBatchUploadException,
JsonProcessingException {

String payload = new SignedStringMessageBuilder().withPayload(ruleId)
RevocationBatchDeleteRequestDto deleteRequest = new RevocationBatchDeleteRequestDto();
deleteRequest.setBatchId(batchId);
ObjectMapper mapper = new ObjectMapper();
String jsonString = mapper.writeValueAsString(deleteRequest);

String payload = new SignedStringMessageBuilder().withPayload(jsonString)
.withSigningCertificate(uploadCertificateHolder, uploadCertificatePrivateKey).buildAsString();

try {
Expand All @@ -155,7 +170,7 @@ public void deleteRevocationBatch(String ruleId) throws DgcRevocationBatchUpload
DgcRevocationBatchUploadException.Reason.INVALID_AUTHORIZATION);

} else if (e.status() == HttpStatus.NOT_FOUND.value()) {
log.info("ValidationRules with ID {} does not exists on DGCG", ruleId);
log.info("ValidationRules with ID {} does not exists on DGCG", batchId);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import eu.europa.ec.dgc.gateway.connector.client.DgcGatewayConnectorRestClient;
import eu.europa.ec.dgc.gateway.connector.dto.RevocationBatchDeleteRequestDto;
import eu.europa.ec.dgc.gateway.connector.dto.RevocationBatchDto;
import eu.europa.ec.dgc.gateway.connector.dto.RevocationHashTypeDto;
import eu.europa.ec.dgc.signing.SignedStringMessageParser;
import eu.europa.ec.dgc.testdata.DgcTestKeyStore;
import eu.europa.ec.dgc.utils.CertificateUtils;
Expand All @@ -36,7 +42,10 @@
import java.security.KeyStoreException;
import java.security.PrivateKey;
import java.security.cert.X509Certificate;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -66,20 +75,18 @@ class RevocationListUploadConnectorTest {

@Test
void testUploadOfRevocationList() throws Exception {
String dummyRevocationList = "dummyRevocationList";

ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class);

when(restClientMock.uploadBatch(argumentCaptor.capture()))
.thenReturn(ResponseEntity.status(HttpStatus.CREATED).build());

connector.uploadRevocationBatch(dummyRevocationList);
connector.uploadRevocationBatch(getRevocation());

verify(restClientMock).uploadBatch(any());

SignedStringMessageParser parser = new SignedStringMessageParser(argumentCaptor.getValue());

Assertions.assertEquals(dummyRevocationList, parser.getPayload());
Assertions.assertEquals(getRevocationJSON(), parser.getPayload());
Assertions.assertEquals(certificateUtils.convertCertificate(testKeyStore.getUpload()),
parser.getSigningCertificate());
}
Expand All @@ -99,7 +106,7 @@ void testDeleteCertificates() throws Exception {

SignedStringMessageParser parser = new SignedStringMessageParser(argumentCaptor.getValue());

Assertions.assertEquals(dummyRevocationListId, parser.getPayload());
Assertions.assertEquals(getDeleteJSON(dummyRevocationListId), parser.getPayload());
Assertions.assertEquals(certificateUtils.convertCertificate(testKeyStore.getUpload()),
parser.getSigningCertificate());
}
Expand Down Expand Up @@ -140,10 +147,11 @@ void shouldThrowAnExceptionWhenUploadRequestFailed() {

DgcGatewayRevocationListUploadConnector.DgcRevocationBatchUploadException e =
Assertions.assertThrows(DgcGatewayRevocationListUploadConnector.DgcRevocationBatchUploadException.class,
() -> connector.uploadRevocationBatch(dummyRevocationList));
() -> connector.uploadRevocationBatch(getRevocation()));

Assertions.assertEquals(
DgcGatewayRevocationListUploadConnector.DgcRevocationBatchUploadException.Reason.INVALID_BATCH, e.getReason());
DgcGatewayRevocationListUploadConnector.DgcRevocationBatchUploadException.Reason.INVALID_BATCH,
e.getReason());
}

@Test
Expand All @@ -163,10 +171,11 @@ void shouldThrowAnExceptionWhenUploadRequestFailedWithBadJson() {

DgcGatewayRevocationListUploadConnector.DgcRevocationBatchUploadException e =
Assertions.assertThrows(DgcGatewayRevocationListUploadConnector.DgcRevocationBatchUploadException.class,
() -> connector.uploadRevocationBatch(dummyRevocationList));
() -> connector.uploadRevocationBatch(getRevocation()));

Assertions.assertEquals(
DgcGatewayRevocationListUploadConnector.DgcRevocationBatchUploadException.Reason.UNKNOWN_ERROR, e.getReason());
DgcGatewayRevocationListUploadConnector.DgcRevocationBatchUploadException.Reason.UNKNOWN_ERROR,
e.getReason());
}

@Test
Expand All @@ -178,7 +187,7 @@ void shouldThrowAnExceptionWhenUploadRequestGetsInternalServerError() {

DgcGatewayRevocationListUploadConnector.DgcRevocationBatchUploadException e =
Assertions.assertThrows(DgcGatewayRevocationListUploadConnector.DgcRevocationBatchUploadException.class,
() -> connector.uploadRevocationBatch(dummyRevocationList));
() -> connector.uploadRevocationBatch(getRevocation()));
Assertions.assertEquals(
DgcGatewayRevocationListUploadConnector.DgcRevocationBatchUploadException.Reason.SERVER_ERROR, e.getReason());
}
Expand All @@ -192,7 +201,7 @@ void shouldThrowAnExceptionWhenUploadRequestGetsUnauthorizedError() {

DgcGatewayRevocationListUploadConnector.DgcRevocationBatchUploadException e =
Assertions.assertThrows(DgcGatewayRevocationListUploadConnector.DgcRevocationBatchUploadException.class,
() -> connector.uploadRevocationBatch(dummyRevocationList));
() -> connector.uploadRevocationBatch(getRevocation()));
Assertions.assertEquals(
DgcGatewayRevocationListUploadConnector.DgcRevocationBatchUploadException.Reason.INVALID_AUTHORIZATION,
e.getReason());
Expand All @@ -207,7 +216,7 @@ void shouldThrowAnExceptionWhenUploadRequestGetsForbiddenError() {

DgcGatewayRevocationListUploadConnector.DgcRevocationBatchUploadException e =
Assertions.assertThrows(DgcGatewayRevocationListUploadConnector.DgcRevocationBatchUploadException.class,
() -> connector.uploadRevocationBatch(dummyRevocationList));
() -> connector.uploadRevocationBatch(getRevocation()));
Assertions.assertEquals(
DgcGatewayRevocationListUploadConnector.DgcRevocationBatchUploadException.Reason.INVALID_AUTHORIZATION,
e.getReason());
Expand All @@ -233,7 +242,8 @@ void shouldThrowAnExceptionWhenDeleteRequestFailed() {
() -> connector.deleteRevocationBatch(dummyRevocationList));

Assertions.assertEquals(
DgcGatewayRevocationListUploadConnector.DgcRevocationBatchUploadException.Reason.INVALID_BATCH, e.getReason());
DgcGatewayRevocationListUploadConnector.DgcRevocationBatchUploadException.Reason.INVALID_BATCH,
e.getReason());
}

@Test
Expand All @@ -256,7 +266,8 @@ void shouldThrowAnExceptionWhenDeleteRequestFailedWithBadJson() {
() -> connector.deleteRevocationBatch(dummyRevocationList));

Assertions.assertEquals(
DgcGatewayRevocationListUploadConnector.DgcRevocationBatchUploadException.Reason.UNKNOWN_ERROR, e.getReason());
DgcGatewayRevocationListUploadConnector.DgcRevocationBatchUploadException.Reason.UNKNOWN_ERROR,
e.getReason());
}

@Test
Expand Down Expand Up @@ -319,4 +330,31 @@ void shouldNotThrowAnExceptionWhenDeleteRequestGetsNotFoundError() {
private Request dummyRequest() {
return Request.create(Request.HttpMethod.GET, "url", new HashMap<>(), null, new RequestTemplate());
}

private RevocationBatchDto getRevocation() {
RevocationBatchDto revocation = new RevocationBatchDto();
revocation.setCountry("SE");
revocation.setExpires(ZonedDateTime.parse("2022-03-14T10:43:08.828Z"));
revocation.setKid("123456789012");
revocation.setHashType(RevocationHashTypeDto.UCI);
RevocationBatchDto.BatchEntryDto entry = new RevocationBatchDto.BatchEntryDto("123456789123456789123456");
List<RevocationBatchDto.BatchEntryDto> list = new ArrayList<>();
list.add(entry);
revocation.setEntries(list);
return revocation;
}

private String getRevocationJSON() throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
return mapper.writeValueAsString(getRevocation());
}

private String getDeleteJSON(String batchId) throws JsonProcessingException {
RevocationBatchDeleteRequestDto deleteRequest = new RevocationBatchDeleteRequestDto();
deleteRequest.setBatchId(batchId);
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
return mapper.writeValueAsString(deleteRequest);
}
}

0 comments on commit 5ff9fc4

Please sign in to comment.