Commit 1d592a6b authored by Rostislav Vatolin [SLB]'s avatar Rostislav Vatolin [SLB] Committed by Rostislav Vatolin [SLB]
Browse files

Merged PR 1682: Set default expirationDate if empty provided

Set default expirationDate if empty provided. When legalTag is created expiration date may be passed or not, need to handle a case, when it is "".
Fix absence of https://dev.azure.com/slb-swt/data-at-rest/_git/dps-legal?path=%2Flegal%2Fsrc%2Fmain%2Fjava%2Fcom%2Fslb%2Flegal%2Futil%2FTimestampAdapter.java

Related work items: #2105
parent f5c4185e
Pipeline #2548 failed with stage
in 2 minutes and 47 seconds
......@@ -6,7 +6,7 @@
<groupId>org.opengroup.osdu</groupId>
<artifactId>os-core-common</artifactId>
<version>0.0.19</version>
<version>0.0.20</version>
<packaging>jar</packaging>
<properties>
......
package org.opengroup.osdu.core.common.model.legal;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.google.common.base.Strings;
import org.opengroup.osdu.core.common.model.legal.serialization.ExpirationDateDeserializer;
import org.opengroup.osdu.core.common.model.legal.validation.ValidDataType;
import org.opengroup.osdu.core.common.model.legal.validation.ValidExportClassification;
import org.opengroup.osdu.core.common.model.legal.validation.ValidLegalTagProperties;
......@@ -38,6 +41,8 @@ public class Properties {
private String contractId;
@JsonDeserialize(using = ExpirationDateDeserializer.class)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
private Date expirationDate;
@Getter(AccessLevel.NONE)
......
package org.opengroup.osdu.core.common.model.legal.serialization;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import org.apache.commons.lang3.StringUtils;
import java.io.IOException;
import java.sql.Date;
import java.time.LocalDate;
import org.opengroup.osdu.core.common.model.legal.Properties;
public class ExpirationDateDeserializer extends StdDeserializer<Date> {
public ExpirationDateDeserializer() {
super(Date.class);
}
@Override
public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
final JsonNode expirationDate = jsonParser.getCodec().readTree(jsonParser);
if (expirationDate == null || StringUtils.isBlank(expirationDate.asText())) {
return Properties.DEFAULT_EXPIRATIONDATE;
}
return Date.valueOf(LocalDate.parse(expirationDate.asText()));
}
}
package org.opengroup.osdu.core.common.model.legal;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Assert;
import org.junit.Test;
import java.io.IOException;
import java.sql.Date;
import java.util.Arrays;
public class LegalTagSerializationTest {
private final ObjectMapper objectMapper = new ObjectMapper();
@Test
public void shouldSuccessfullySerializeLegalTag() throws JsonProcessingException {
final LegalTag legalTag = new LegalTag();
legalTag.setId(123L);
legalTag.setName("name");
legalTag.setDescription("desc");
final Properties properties = new Properties();
properties.setContractId("contrId");
properties.setDataType("dataType");
properties.setCountryOfOrigin(Arrays.asList("US", "BY"));
properties.setExpirationDate(Date.valueOf("2020-12-20"));
properties.setOriginator("company");
properties.setSecurityClassification("securityClassification");
properties.setPersonalData("data");
properties.setExportClassification("exportClassification");
legalTag.setProperties(properties);
String result = objectMapper.writeValueAsString(legalTag);
Assert.assertEquals("{\"id\":123,\"name\":\"name\",\"description\":\"desc\"," +
"\"properties\":{\"countryOfOrigin\":[\"US\",\"BY\"],\"contractId\":\"contrId\"," +
"\"expirationDate\":\"2020-12-20\",\"originator\":\"company\"," +
"\"dataType\":\"dataType\",\"securityClassification\":\"securityClassification\"," +
"\"personalData\":\"data\",\"exportClassification\":\"exportClassification\"}," +
"\"isValid\":false}", result);
}
@Test
public void shouldSuccessfullyDeserializeLegalTag() throws IOException {
final String input = "{\"id\":123,\"name\":\"name\",\"description\":\"desc\"," +
"\"properties\":{\"countryOfOrigin\":[\"US\",\"BY\"],\"contractId\":\"contrId\"," +
"\"expirationDate\":\"2020-12-20\",\"originator\":\"company\"," +
"\"dataType\":\"dataType\",\"securityClassification\":\"securityClassification\"," +
"\"personalData\":\"data\",\"exportClassification\":\"exportClassification\"}," +
"\"isValid\":false}";
LegalTag legalTag = objectMapper.readValue(input, LegalTag.class);
Assert.assertEquals(new Long(123L), legalTag.getId());
Assert.assertEquals("name", legalTag.getName());
Assert.assertEquals("desc", legalTag.getDescription());
Assert.assertEquals("contrId", legalTag.getProperties().getContractId());
Assert.assertEquals(Arrays.asList("US","BY"), legalTag.getProperties().getCountryOfOrigin());
Assert.assertEquals(Date.valueOf("2020-12-20"), legalTag.getProperties().getExpirationDate());
Assert.assertEquals("company", legalTag.getProperties().getOriginator());
Assert.assertEquals("dataType", legalTag.getProperties().getDataType());
Assert.assertEquals("securityClassification", legalTag.getProperties().getSecurityClassification());
Assert.assertEquals("data", legalTag.getProperties().getPersonalData());
Assert.assertEquals("exportClassification", legalTag.getProperties().getExportClassification());
}
@Test
public void shouldSetDefaultExpirationDateIfNotProvided() throws IOException {
final String input = "{\"id\":123,\"name\":\"name\",\"description\":\"desc\"," +
"\"properties\":{\"countryOfOrigin\":[\"US\",\"BY\"],\"contractId\":\"contrId\"," +
"\"originator\":\"company\"," +
"\"dataType\":\"dataType\",\"securityClassification\":\"securityClassification\"," +
"\"personalData\":\"data\",\"exportClassification\":\"exportClassification\"}," +
"\"isValid\":false}";
LegalTag legalTag = objectMapper.readValue(input, LegalTag.class);
Assert.assertEquals(Date.valueOf("9999-12-31"), legalTag.getProperties().getExpirationDate());
}
@Test
public void shouldSetDefaultExpirationDateIfEmptyProvided() throws IOException {
final String input = "{\"id\":123,\"name\":\"name\",\"description\":\"desc\"," +
"\"properties\":{\"countryOfOrigin\":[\"US\",\"BY\"],\"contractId\":\"contrId\"," +
"\"expirationDate\":\"\",\"originator\":\"company\"," +
"\"dataType\":\"dataType\",\"securityClassification\":\"securityClassification\"," +
"\"personalData\":\"data\",\"exportClassification\":\"exportClassification\"}," +
"\"isValid\":false}";
LegalTag legalTag = objectMapper.readValue(input, LegalTag.class);
Assert.assertEquals(Date.valueOf("9999-12-31"), legalTag.getProperties().getExpirationDate());
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment