From cc64d49ef046cb1acfe28ae1de92dec5489fb571 Mon Sep 17 00:00:00 2001 From: "AAVN\\pvquan" Date: Mon, 16 Dec 2024 13:50:23 +0700 Subject: [PATCH] update test coverage --- .../market/logging/LoggableAspect.java | 4 +- .../market/logging/LoggableAspectTest.java | 82 +++++++++++++++++++ .../axonivy/market/util/FileUtilsTest.java | 29 ++++++- .../axonivy/market/util/LoggingUtilsTest.java | 37 ++++++++- 4 files changed, 145 insertions(+), 7 deletions(-) create mode 100644 marketplace-service/src/test/java/com/axonivy/market/logging/LoggableAspectTest.java diff --git a/marketplace-service/src/main/java/com/axonivy/market/logging/LoggableAspect.java b/marketplace-service/src/main/java/com/axonivy/market/logging/LoggableAspect.java index 4f50ea59..f04fbb8e 100644 --- a/marketplace-service/src/main/java/com/axonivy/market/logging/LoggableAspect.java +++ b/marketplace-service/src/main/java/com/axonivy/market/logging/LoggableAspect.java @@ -30,7 +30,7 @@ public class LoggableAspect { @Value("${loggable.log-path}") - private String logFilePath; + public String logFilePath; private static final String REQUESTED_BY = "marketplace-website"; @@ -58,7 +58,7 @@ private Map extractHeaders(HttpServletRequest request, MethodSig "timestamp", escapeXml(getCurrentTimestamp()), "user-agent", escapeXml(request.getHeader(CommonConstants.USER_AGENT)), "arguments", escapeXml(getArgumentsString(signature.getParameterNames(), joinPoint.getArgs())), - "x-requested-by", escapeXml(request.getHeader(CommonConstants.REQUESTED_BY)) + "X-Requested-By", escapeXml(request.getHeader(CommonConstants.REQUESTED_BY)) ); } diff --git a/marketplace-service/src/test/java/com/axonivy/market/logging/LoggableAspectTest.java b/marketplace-service/src/test/java/com/axonivy/market/logging/LoggableAspectTest.java new file mode 100644 index 00000000..583e0810 --- /dev/null +++ b/marketplace-service/src/test/java/com/axonivy/market/logging/LoggableAspectTest.java @@ -0,0 +1,82 @@ +package com.axonivy.market.logging; + +import com.axonivy.market.constants.CommonConstants; +import com.axonivy.market.exceptions.model.MissingHeaderException; +import com.axonivy.market.util.LoggingUtils; +import jakarta.servlet.http.HttpServletRequest; +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.reflect.MethodSignature; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class LoggableAspectTest { + + @Mock + private HttpServletRequest request; + + private LoggableAspect loggableAspect; + + @BeforeEach + void setUp() throws IOException { + MockitoAnnotations.openMocks(this); + loggableAspect = new LoggableAspect(); + loggableAspect.logFilePath = Files.createTempDirectory("logs").toString(); // Use temp directory for tests + } + + @Test + void testLogFileCreation() throws Exception { + mockRequestAttributes("marketplace-website", "test-agent"); + MethodSignature signature = mockMethodSignature(); + + loggableAspect.logMethodCall(mockJoinPoint(signature)); + + Path logFilePath = Path.of(loggableAspect.logFilePath, "log-" + LoggingUtils.getCurrentDate() + ".xml"); + assertTrue(Files.exists(logFilePath), "Log file should be created"); + + String content = Files.readString(logFilePath); + assertTrue(content.contains(""), "Log file should contain log entries"); + } + + @Test + void testMissingHeaderException() { + mockRequestAttributes("invalid-source", "mock-agent"); + MethodSignature signature = mockMethodSignature(); + + assertThrows(MissingHeaderException.class, () -> { + loggableAspect.logMethodCall(mockJoinPoint(signature)); + }); + } + + private JoinPoint mockJoinPoint(MethodSignature signature) { + JoinPoint joinPoint = mock(JoinPoint.class); + when(joinPoint.getSignature()).thenReturn(signature); + when(joinPoint.getArgs()).thenReturn(new Object[]{"arg1", "arg2"}); // Example arguments + return joinPoint; + } + + private void mockRequestAttributes(String requestedBy, String userAgent) { + when(request.getHeader(CommonConstants.REQUESTED_BY)).thenReturn(requestedBy); + when(request.getHeader(CommonConstants.USER_AGENT)).thenReturn(userAgent); + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); + } + + private MethodSignature mockMethodSignature() { + MethodSignature signature = mock(MethodSignature.class); + when(signature.getMethod()).thenReturn(this.getClass().getMethods()[0]); // Use any available method + return signature; + } + +} diff --git a/marketplace-service/src/test/java/com/axonivy/market/util/FileUtilsTest.java b/marketplace-service/src/test/java/com/axonivy/market/util/FileUtilsTest.java index 05c201e7..b7db9681 100644 --- a/marketplace-service/src/test/java/com/axonivy/market/util/FileUtilsTest.java +++ b/marketplace-service/src/test/java/com/axonivy/market/util/FileUtilsTest.java @@ -1,6 +1,5 @@ package com.axonivy.market.util; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.junit.jupiter.MockitoExtension; @@ -9,6 +8,8 @@ import java.io.IOException; import java.nio.file.Files; +import static org.junit.jupiter.api.Assertions.*; + @ExtendWith(MockitoExtension.class) class FileUtilsTest { @@ -17,12 +18,32 @@ class FileUtilsTest { @Test void testCreateFile() throws IOException { File createdFile = FileUtils.createFile(FILE_PATH); - Assertions.assertTrue(createdFile.exists(), "File should exist"); - Assertions.assertTrue(createdFile.isFile(), "Should be a file"); + assertTrue(createdFile.exists(), "File should exist"); + assertTrue(createdFile.isFile(), "Should be a file"); createdFile.delete(); } + @Test + void testFailedToCreateDirectory() throws IOException { + File createdFile = new File("testDirAsFile"); + try { + if (!createdFile.exists()) { + assertTrue(createdFile.createNewFile(), "Setup failed: could not create file"); + } + + IOException exception = assertThrows(IOException.class, () -> { + FileUtils.createFile("testDirAsFile/subDir/testFile.txt"); + }); + assertTrue(exception.getMessage().contains("Failed to create directory"), + "Exception message does not contain expected text"); + } catch (IOException e) { + fail("Setup failed: " + e.getMessage()); + } finally { + createdFile.delete(); + } + } + @Test void testWriteFile() throws IOException { File createdFile = FileUtils.createFile(FILE_PATH); @@ -30,7 +51,7 @@ void testWriteFile() throws IOException { FileUtils.writeToFile(createdFile, content); String fileContent = Files.readString(createdFile.toPath()); - Assertions.assertEquals(content, fileContent, "File content should match the written content"); + assertEquals(content, fileContent, "File content should match the written content"); createdFile.delete(); diff --git a/marketplace-service/src/test/java/com/axonivy/market/util/LoggingUtilsTest.java b/marketplace-service/src/test/java/com/axonivy/market/util/LoggingUtilsTest.java index 01d58ad0..00d78874 100644 --- a/marketplace-service/src/test/java/com/axonivy/market/util/LoggingUtilsTest.java +++ b/marketplace-service/src/test/java/com/axonivy/market/util/LoggingUtilsTest.java @@ -5,19 +5,29 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.junit.jupiter.MockitoExtension; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; import java.util.Map; @ExtendWith(MockitoExtension.class) class LoggingUtilsTest { @Test - void testEscapeXml() { + void testEscapeXmlSuccess() { String input = ""; String expectedValue = "<Test'& "Method>"; String result = LoggingUtils.escapeXml(input); Assertions.assertEquals(expectedValue, result); } + @Test + void testEscapeXmlOnNullValue() { + String expectedValue = ""; + String result = LoggingUtils.escapeXml(null); + Assertions.assertEquals(expectedValue, result); + } + @Test void testGetArgumentsString() { String expectedValue = "a: random, b: sample"; @@ -25,6 +35,13 @@ void testGetArgumentsString() { Assertions.assertEquals(expectedValue, result); } + @Test + void testGetArgumentsStringOnNullValue() { + String expectedValue = "No arguments"; + String result = LoggingUtils.getArgumentsString(null, null); + Assertions.assertEquals(expectedValue, result); + } + @Test void testBuildLogEntry() { Map given = Map.of( @@ -43,4 +60,22 @@ void testBuildLogEntry() { Assertions.assertEquals(expected, result); } + @Test + void testGetCurrentDate() { + String expectedDate = LocalDate.now().toString(); + String actualDate = LoggingUtils.getCurrentDate(); + + Assertions.assertEquals(expectedDate, actualDate, "The returned date does not match the current date"); + } + + @Test + void testGetCurrentTimestamp() { + String expectedTimestamp = LocalDateTime.now() + .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); + String actualTimestamp = LoggingUtils.getCurrentTimestamp(); + + Assertions.assertEquals(expectedTimestamp.substring(0, 19), actualTimestamp.substring(0, 19), + "The returned timestamp does not match the expected format or value"); + } + }