Files
CipherStreamAPI/src/test/java/com/mattrixwv/cipherstream/controller/CipherStreamControllerIntegrationTestBase.java
2024-04-28 15:26:40 -04:00

74 lines
2.2 KiB
Java

package com.mattrixwv.cipherstream.controller;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import java.util.UUID;
import org.junit.jupiter.api.Tag;
import org.mockito.Mock;
import org.slf4j.Logger;
import org.slf4j.spi.MDCAdapter;
import org.springframework.boot.autoconfigure.aop.AopAutoConfiguration;
import org.springframework.context.annotation.Import;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect;
import com.mattrixwv.cipherstream.config.FullFilter;
@Tag("integration-test")
@Import({AopAutoConfiguration.class, FullFilter.class, CipherStreamLoggingAspect.class})
public class CipherStreamControllerIntegrationTestBase{
protected static final ObjectMapper mapper = new ObjectMapper();
//Objects
protected ObjectNode decodedNode;
protected ObjectNode encodedNode;
//Fields
protected static final String requestId = UUID.randomUUID().toString();
protected static final String ipAddress = "192.168.1.1";
//MDC
@Mock
protected MDCAdapter mdc;
//Base
@Mock(name = "com.mattrixwv.cipherstream.controller.CipherStreamController")
protected Logger baseLogger;
//Misc
@Mock(name = "com.mattrixwv.cipherstream.config.FullFilter")
protected Logger filterLogger;
@Mock(name = "com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect")
protected Logger aspectLogger;
protected void verifyFilter(String url){
verify(filterLogger, never()).info(eq("Request parameters: {}"), any(StringBuilder.class));
verify(mdc, times(1)).put(eq("requestId"), any());
verify(mdc, times(1)).put("ip", ipAddress);
verify(mdc, times(1)).put("url", url);
verify(mdc, times(1)).clear();
}
protected void verifyAspectLogging(ObjectNode jsonNode){
//Verify the MDC
jsonNode.fields().forEachRemaining(entry -> {
if(entry.getValue().isTextual()){
verify(mdc, times(1)).put(entry.getKey(), entry.getValue().asText());
}
else{
verify(mdc, times(1)).put(entry.getKey(), entry.getValue().toString());
}
});
verifyNoMoreInteractions(mdc);
//Verify the logger
verify(aspectLogger, times(1)).info("CipherStream log");
verifyNoMoreInteractions(aspectLogger);
}
}