Skip to content
This repository has been archived by the owner on Sep 15, 2023. It is now read-only.

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
martinalig committed Apr 7, 2022
1 parent cf189f4 commit 943318f
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import org.bouncycastle.cms.CMSException;
import org.bouncycastle.operator.OperatorCreationException;
import org.slf4j.Logger;
Expand All @@ -48,53 +47,64 @@ public int sync() {
List<String> countries = dgcRulesClient.getCountries();
countries.remove("CH");
int successful = 0;
for(var country: countries){
for (var country : countries) {
var rules = new ArrayList<ForeignRule>();
var downloaded = dgcRulesClient.download(country);
downloaded.forEach( (id, ruleVersions) -> {
ruleVersions.forEach( version ->{
try {
rules.add(
decodeRule(
(ObjectNode) version, country, id));
} catch (Exception e) {
logger.error(
"Failed to decode rule {}",
id,
e);
}
}
);
});
downloaded.forEach(
(id, ruleVersions) -> {
ruleVersions.forEach(
version -> {
try {
rules.add(decodeRule((ObjectNode) version, country, id));
} catch (Exception e) {
logger.error("Failed to decode rule {}", id, e);
}
});
});

if(rules.isEmpty()){
if (rules.isEmpty()) {
logger.error("No rules were downloaded or decoded for {}", country);
}else{
} else {
foreignRulesDataService.removeRuleSet(country);
rules.forEach(foreignRulesDataService::insertRule);
successful += rules.size();
}
}
var end = Instant.now();
logger.info(
"Successfully downloaded {} foreign rules {} ms", successful,
"Successfully downloaded {} foreign rules {} ms",
successful,
end.toEpochMilli() - start.toEpochMilli());
return successful;
}

private ForeignRule decodeRule(ObjectNode rule, String country, String id)
throws CertificateException, IOException, OperatorCreationException, CMSException {
var foreignRule = new ForeignRule();
String content = new String((byte[]) CmsUtil.decodeCms(rule.get("cms").asText()), StandardCharsets.UTF_8);
String content =
new String(
(byte[]) CmsUtil.decodeCms(rule.get("cms").asText()),
StandardCharsets.UTF_8);
ObjectNode convertedNode = mapper.createObjectNode();
mapper.readTree(content).fields().forEachRemaining(field -> {
String fieldName = field.getKey().substring(0,1).toLowerCase().concat(field.getKey().substring(1));
convertedNode.set(fieldName, field.getValue());
});
mapper.readTree(content)
.fields()
.forEachRemaining(
field -> {
String fieldName =
field.getKey()
.substring(0, 1)
.toLowerCase()
.concat(field.getKey().substring(1));
convertedNode.set(fieldName, field.getValue());
});
foreignRule.setContent(convertedNode.toString());
var validUntil = LocalDateTime.parse(rule.get("validTo").asText(), DateTimeFormatter.ISO_OFFSET_DATE_TIME);
var validUntil =
LocalDateTime.parse(
rule.get("validTo").asText(), DateTimeFormatter.ISO_OFFSET_DATE_TIME);
foreignRule.setValidUntil(validUntil);
var validFrom = LocalDateTime.parse(rule.get("validTo").asText(), DateTimeFormatter.ISO_OFFSET_DATE_TIME);
var validFrom =
LocalDateTime.parse(
rule.get("validTo").asText(), DateTimeFormatter.ISO_OFFSET_DATE_TIME);
foreignRule.setValidFrom(validFrom);
foreignRule.setVersion(rule.get("version").asText());
foreignRule.setCountry(country);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,10 @@
package ch.admin.bag.covidcertificate.backend.verifier.sync.syncer;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus;

import ch.admin.bag.covidcertificate.backend.verifier.model.sync.CertificateType;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import java.security.NoSuchAlgorithmException;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsProperties.Web;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import ch.admin.bag.covidcertificate.backend.verifier.ws.util.TestHelper;
import ch.admin.bag.covidcertificate.backend.verifier.ws.utils.EtagUtil;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.util.Map;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class MockForeignRuleDataService implements ForeignRulesDataService {
private final ForeignRule at1 = new ForeignRule();
private final ForeignRule at2 = new ForeignRule();

public MockForeignRuleDataService(){
public MockForeignRuleDataService() {
at1.setId("GR-AT-0039");
at1.setVersion("0.0.1");
at1.setCountry("AT");
Expand All @@ -38,22 +38,18 @@ public List<String> getCountries() {

@Override
public List<ForeignRule> getRulesForCountry(String country) {
if(country.equals("AT")){
if (country.equals("AT")) {
return Arrays.asList(at1, at2);
}else if(country.equals("DE")){
} else if (country.equals("DE")) {
return new ArrayList<>();
}else{
} else {
return new ArrayList<>();
}
}

@Override
public void insertRule(ForeignRule rule) {

}
public void insertRule(ForeignRule rule) {}

@Override
public void removeRuleSet(String country) {

}
public void removeRuleSet(String country) {}
}

0 comments on commit 943318f

Please sign in to comment.