-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c02eb3b
commit cc64d49
Showing
4 changed files
with
145 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
marketplace-service/src/test/java/com/axonivy/market/logging/LoggableAspectTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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("<Logs>"), "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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters