Skip to content
Snippets Groups Projects
Commit 6e61016a authored by Ashutosh Rai's avatar Ashutosh Rai
Browse files

Merge branch 'azure/ashutoshrai/expirationdate-issue' into 'master'

Fixed invalid expiration date bug in Create/Update Legal Tag API calls

See merge request !650
parents 3fad3971 9f16510b
No related branches found
No related tags found
1 merge request!650Fixed invalid expiration date bug in Create/Update Legal Tag API calls
Pipeline #314335 failed
......@@ -79,7 +79,7 @@ The following software have components provided under the terms of this license:
- Apache HttpCore (from http://hc.apache.org/httpcomponents-core-ga, http://hc.apache.org/httpcomponents-core-ga/, http://hc.apache.org/httpcomponents-core/)
- Apache Log4j API (from https://repo1.maven.org/maven2/org/apache/logging/log4j/log4j-api)
- Apache Log4j Core (from https://repo1.maven.org/maven2/org/apache/logging/log4j/log4j-core)
- Apache Log4j JUL Handler (from https://logging.apache.org/log4j/3.x/)
- Apache Log4j JUL Handler (from https://logging.apache.org/log4j/3.x/, https://repo1.maven.org/maven2/org/apache/logging/log4j/log4j-jul)
- Apache Log4j SLF4J 2.0 Binding (from https://repo1.maven.org/maven2/org/apache/logging/log4j/log4j-slf4j2-impl)
- Apache Log4j SLF4J Binding (from https://repo1.maven.org/maven2/org/apache/logging/log4j/log4j-slf4j-impl)
- Apache Log4j to SLF4J Adapter (from https://repo1.maven.org/maven2/org/apache/logging/log4j/log4j-to-slf4j)
......
......@@ -79,6 +79,7 @@ public class LegalTagService {
public LegalTagDto create(LegalTagDto legalTagDto, String tenantName) {
if (legalTagDto == null)
return null;
validateExpirationDate(legalTagDto.getProperties().getExpirationDate());
validator.isValidThrows(legalTagDto);
ILegalTagRepository legalTagRepository = repositories.get(tenantName);
......@@ -190,7 +191,7 @@ public class LegalTagService {
public LegalTagDto update(UpdateLegalTag newLegalTag, String tenantName) {
if (newLegalTag == null)
return null;
validateExpirationDate(newLegalTag.getExpirationDate());
LegalTag currentLegalTag = getLegalTag(newLegalTag.getName(), tenantName);
if (currentLegalTag == null) {
throw AppException.legalTagDoesNotExistError(newLegalTag.getName());
......@@ -626,4 +627,14 @@ public class LegalTagService {
}
return didItMatch;
}
private void validateExpirationDate(Date expirationDate)
{
if(expirationDate != null) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
String yearString = sdf.format(expirationDate);
if (yearString.length() != 4) {
throw new AppException(400, "BadRequest", String.format("Expiration Date has invalid year %s.", yearString));
}
}
}
}
\ No newline at end of file
......@@ -2,13 +2,17 @@ package org.opengroup.osdu.legal.tags.dto;
import java.sql.Date;
import java.util.Map;
import io.swagger.v3.oas.annotations.media.Schema;
import org.opengroup.osdu.core.common.model.legal.validation.ValidDescription;
import org.opengroup.osdu.core.common.model.legal.validation.ValidName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import org.opengroup.osdu.core.common.model.legal.serialization.ExpirationDateDeserializer;
import static org.opengroup.osdu.core.common.util.SerializationUtils.EXPIRATION_DATE_FORMAT;
/**
* If any class variable changed here,
......@@ -32,8 +36,10 @@ public class UpdateLegalTag {
private String description = "";
@Schema(description = "The optional expiration date of the contract in the format YYYY-MM-DD", example = "2025-12-25")
@JsonDeserialize(using = ExpirationDateDeserializer.class)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = EXPIRATION_DATE_FORMAT)
private Date expirationDate;
@Schema(description = "The optional object field to attach any company specific attributes.")
private Map<String, Object> extensionProperties;
}
}
\ No newline at end of file
......@@ -501,6 +501,38 @@ public class LegalTagServiceTests {
verify(messagePublisherMock, never()).publish(any(), any(), any());
}
@Test
public void should_notCreateLegalTag_when_ExpirationDateIsInvalid() throws Exception {
LegalTagDto legalTagDto = LegalTestUtils.createValidLegalTagDto("mylegaltag");
long expirationDate = 5555555555555555L;
legalTagDto.getProperties().setExpirationDate(new Date(expirationDate)) ;
LegalTagService testService = createSut();
AppException thrown = assertThrows(AppException.class, () -> {
testService.create(legalTagDto, "tenant2");
});
// Assert: Check if the exception has the expected status code, error type, and message.
assertEquals("Expected 400 BadRequest error code", 400, thrown.getError().getCode());
assertEquals("Expected BadRequest error type", "BadRequest", thrown.getError().getReason());
}
@Test
public void should_notUpdateLegalTag_when_ExpirationDateIsInvalid() throws Exception {
UpdateLegalTag updateLegalTagDto = LegalTestUtils.createUpdateLegalTag("mylegaltag");
long expirationDate = 5555555555555555L;
updateLegalTagDto.setExpirationDate(new Date(expirationDate)) ;
LegalTagService testService = createSut();
AppException thrown = assertThrows(AppException.class, () -> {
testService.update(updateLegalTagDto, "tenant2");
});
// Assert: Check if the exception has the expected status code, error type, and message.
assertEquals("Expected 400 BadRequest error code", 400, thrown.getError().getCode());
assertEquals("Expected BadRequest error type", "BadRequest", thrown.getError().getReason());
}
@Test
public void should_returnLegalTag_when_givenExistingNames() {
sut = createSut();
......
/*
* Copyright 2025 © Microsoft Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.opengroup.osdu.legal.tags.model;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.opengroup.osdu.legal.tags.dto.UpdateLegalTag;
import static org.opengroup.osdu.core.common.util.SerializationUtils.EXPIRATION_DATE_FORMAT;
import org.junit.Test;
import java.util.Date;
import java.text.SimpleDateFormat;
import static org.junit.Assert.*;
import static junit.framework.TestCase.assertEquals;
public class UpdateLegalTagTests {
private final ObjectMapper objectMapper = new ObjectMapper();
private static final String EXPIRATION_DATE_STRING = "2025-12-25";
/**
* Tests that the ExpirationDateDeserializer is correctly deserializing the expiration date
* and @JsonFormat annotation correctly formats the expiration date during deserialization.
*/
@Test
public void testExpirationDate_Deserialization() throws Exception {
String json = "{\"expirationDate\":\"2025-12-25\"}";
UpdateLegalTag tag = objectMapper.readValue(json, UpdateLegalTag.class);
SimpleDateFormat sdf = new SimpleDateFormat(EXPIRATION_DATE_FORMAT);
Date expectedDate = sdf.parse(EXPIRATION_DATE_STRING);
assertNotNull(tag.getExpirationDate());
assertEquals(expectedDate, tag.getExpirationDate());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment