From 9f16510b826e0e0908969920a72031ebfeffa859 Mon Sep 17 00:00:00 2001 From: Ashutosh Rai <ashutoshrai@microsoft.com> Date: Tue, 18 Mar 2025 18:21:50 +0000 Subject: [PATCH] Fixed invalid expiration date bug in Create/Update Legal Tag API calls --- NOTICE | 2 +- .../osdu/legal/tags/LegalTagService.java | 13 ++++- .../osdu/legal/tags/dto/UpdateLegalTag.java | 10 +++- .../osdu/legal/tags/LegalTagServiceTests.java | 32 +++++++++++++ .../legal/tags/model/UpdateLegalTagTests.java | 47 +++++++++++++++++++ 5 files changed, 100 insertions(+), 4 deletions(-) create mode 100644 legal-core/src/test/java/org/opengroup/osdu/legal/tags/model/UpdateLegalTagTests.java diff --git a/NOTICE b/NOTICE index 492ef43ec..5cf35faf5 100644 --- a/NOTICE +++ b/NOTICE @@ -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) diff --git a/legal-core/src/main/java/org/opengroup/osdu/legal/tags/LegalTagService.java b/legal-core/src/main/java/org/opengroup/osdu/legal/tags/LegalTagService.java index c0814913d..89a920a0e 100644 --- a/legal-core/src/main/java/org/opengroup/osdu/legal/tags/LegalTagService.java +++ b/legal-core/src/main/java/org/opengroup/osdu/legal/tags/LegalTagService.java @@ -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 diff --git a/legal-core/src/main/java/org/opengroup/osdu/legal/tags/dto/UpdateLegalTag.java b/legal-core/src/main/java/org/opengroup/osdu/legal/tags/dto/UpdateLegalTag.java index 14617b5c2..52ef63f6a 100644 --- a/legal-core/src/main/java/org/opengroup/osdu/legal/tags/dto/UpdateLegalTag.java +++ b/legal-core/src/main/java/org/opengroup/osdu/legal/tags/dto/UpdateLegalTag.java @@ -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 diff --git a/legal-core/src/test/java/org/opengroup/osdu/legal/tags/LegalTagServiceTests.java b/legal-core/src/test/java/org/opengroup/osdu/legal/tags/LegalTagServiceTests.java index 57b0219ba..23af933a2 100644 --- a/legal-core/src/test/java/org/opengroup/osdu/legal/tags/LegalTagServiceTests.java +++ b/legal-core/src/test/java/org/opengroup/osdu/legal/tags/LegalTagServiceTests.java @@ -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(); diff --git a/legal-core/src/test/java/org/opengroup/osdu/legal/tags/model/UpdateLegalTagTests.java b/legal-core/src/test/java/org/opengroup/osdu/legal/tags/model/UpdateLegalTagTests.java new file mode 100644 index 000000000..5ef11b152 --- /dev/null +++ b/legal-core/src/test/java/org/opengroup/osdu/legal/tags/model/UpdateLegalTagTests.java @@ -0,0 +1,47 @@ +/* + * 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()); + } +} -- GitLab