Skip to content

Commit

Permalink
Feat: Add Validation for JRC Response (#206)
Browse files Browse the repository at this point in the history
* Add Validation for JRC Response

* Add Unit Test
  • Loading branch information
f11h authored Sep 27, 2022
1 parent b4447c4 commit ec4175b
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/main/java/eu/europa/ec/dgc/gateway/client/JrcClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,25 @@
package eu.europa.ec.dgc.gateway.client;

import eu.europa.ec.dgc.gateway.model.JrcRatValuesetResponse;
import javax.validation.Valid;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;

@FeignClient(
name = "jrcClient",
url = "${dgc.jrc.url}",
configuration = JrcClientConfig.class)
@Validated
public interface JrcClient {

/**
* This method gets a the RAT values from JRC API.
*
* @return List of RAT values.
*/
@GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE
)
@GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
@Valid
JrcRatValuesetResponse downloadRatValues();
}
13 changes: 13 additions & 0 deletions src/main/java/eu/europa/ec/dgc/gateway/model/JrcRatValueset.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,30 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import java.time.ZonedDateTime;
import java.util.List;
import javax.validation.constraints.NotNull;
import lombok.Data;

@Data
public class JrcRatValueset {

@JsonProperty("id_device")
@NotNull
String idDevice;

@JsonProperty("commercial_name")
@NotNull
String commercialName;

@JsonProperty("manufacturer")
@NotNull
Manufacturer manufacturer;

@JsonProperty("hsc_common_list")
@NotNull
Boolean hscCommonList;

@JsonProperty("hsc_mutual_recognition")
@NotNull
Boolean hscMutualRecognition;

@JsonProperty("last_updated")
Expand All @@ -56,28 +62,35 @@ public static class HscListHistory {

@JsonProperty("list_date")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss z")
@NotNull
ZonedDateTime listDate;

@JsonProperty("in_common_list")
@NotNull
Boolean inCommonList;

@JsonProperty("in_mutual_recognition")
@NotNull
Boolean inMutualRecognition;
}

@Data
public static class Manufacturer {

@JsonProperty("id_manufacturer")
@NotNull
String id;

@JsonProperty("name")
@NotNull
String name;

@JsonProperty("country")
@NotNull
String country;

@JsonProperty("website")
@NotNull
String website;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,18 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import java.time.ZonedDateTime;
import java.util.List;
import javax.validation.constraints.NotNull;
import lombok.Data;

@Data
public class JrcRatValuesetResponse {

@JsonProperty("extracted_on")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss z")
@NotNull
ZonedDateTime extractedOn;

@JsonProperty("deviceList")
@NotNull
List<JrcRatValueset> deviceList;
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.HashMap;
import java.util.Optional;
import javax.annotation.PostConstruct;
import javax.validation.ConstraintViolationException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
Expand Down Expand Up @@ -93,6 +94,9 @@ public void update() {
} catch (FeignException e) {
log.error("Failed to download RatValueset from JRC", e);
return;
} catch (ConstraintViolationException e) {
log.error("Failed to parse RatValueset from JRC", e);
return;
}

for (JrcRatValueset device : jrcResponse.getDeviceList()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.validation.ConstraintViolationException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -379,6 +380,24 @@ void testRatValuesetUpdateShouldNotUpdateWhenRequestFails() throws JsonProcessin
assertEquals(rat2, updatedValueset.getValue().get(RAT2_ID));
}

@Test
void testRatValuesetUpdateShouldNotUpdateWhenJsonIsInvalid() throws JsonProcessingException {

doThrow(new ConstraintViolationException("Invalid JSON", Collections.emptySet()))
.when(jrcClientMock).downloadRatValues();

ratValuesetUpdateService.update();

String updatedValuesetJson = valuesetService.getValueSetById(RAT_VALUESET_ID).orElseThrow();
Valueset<String, RatValueset> updatedValueset = objectMapper.readValue(updatedValuesetJson, typeReference);

Assertions.assertEquals(LocalDate.now().minus(1, ChronoUnit.DAYS), updatedValueset.getDate(),
"Valueset Date has been updated.");
Assertions.assertEquals(2, updatedValueset.getValue().size(), "Valueset List size has been changed");
assertEquals(rat1, updatedValueset.getValue().get(RAT1_ID));
assertEquals(rat2, updatedValueset.getValue().get(RAT2_ID));
}

@Test
void testRatValuesetUpdateLatestAllHistoryEntriesAreInFuture() throws JsonProcessingException {
JrcRatValueset.Manufacturer manufacturer = new JrcRatValueset.Manufacturer();
Expand Down

0 comments on commit ec4175b

Please sign in to comment.