Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@

import org.junit.jupiter.api.Test;
import org.mockito.MockedConstruction;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.slf4j.Logger;

import com.checkmarx.ast.wrapper.CxException;
import com.checkmarx.ast.wrapper.CxWrapper;
import com.checkmarx.eclipse.runner.Authenticator;
import com.checkmarx.eclipse.utils.CxLogger;
import com.checkmarx.eclipse.utils.PluginConstants;

class AuthenticatorTest {
Expand All @@ -24,14 +26,15 @@ void testDoAuthenticationSuccess() throws Exception {

try (MockedConstruction<CxWrapper> mocked =
Mockito.mockConstruction(CxWrapper.class,
(mock, context) -> when(mock.authValidate()).thenReturn("SUCCESS"))) {
(mock, context) -> when(mock.authValidate()).thenReturn("SUCCESS"));
MockedStatic<CxLogger> mockedCxLogger = Mockito.mockStatic(CxLogger.class)) {

Authenticator authenticator = new Authenticator(mockLogger);

String result = authenticator.doAuthentication("dummyKey", "--param");

assertEquals("SUCCESS", result);
verify(mockLogger).info("Authentication Status: SUCCESS");
mockedCxLogger.verify(() -> CxLogger.info("Authentication Status: SUCCESS"));
}
}

Expand All @@ -43,17 +46,18 @@ void testDoAuthenticationIOException() throws Exception {
try (MockedConstruction<CxWrapper> mocked =
Mockito.mockConstruction(CxWrapper.class,
(mock, context) -> when(mock.authValidate())
.thenThrow(new IOException("IO error")))) {
.thenThrow(new IOException("IO error")));
MockedStatic<CxLogger> mockedCxLogger = Mockito.mockStatic(CxLogger.class)) {

Authenticator authenticator = new Authenticator(mockLogger);

String result = authenticator.doAuthentication("dummyKey", "--param");

assertEquals("IO error", result);
verify(mockLogger).error(
eq(String.format(PluginConstants.ERROR_AUTHENTICATING_AST, "IO error")),
any(IOException.class)
);
mockedCxLogger.verify(() -> CxLogger.error(
eq(String.format(PluginConstants.ERROR_AUTHENTICATING_AST, "IO error")),
any(IOException.class)
));
}
}

Expand All @@ -65,17 +69,18 @@ void testDoAuthenticationInterruptedException() throws Exception {
try (MockedConstruction<CxWrapper> mocked =
Mockito.mockConstruction(CxWrapper.class,
(mock, context) -> when(mock.authValidate())
.thenThrow(new InterruptedException("Interrupted")))) {
.thenThrow(new InterruptedException("Interrupted")));
MockedStatic<CxLogger> mockedCxLogger = Mockito.mockStatic(CxLogger.class)) {

Authenticator authenticator = new Authenticator(mockLogger);

String result = authenticator.doAuthentication("dummyKey", "--param");

assertEquals("Interrupted", result);
verify(mockLogger).error(
eq(String.format(PluginConstants.ERROR_AUTHENTICATING_AST, "Interrupted")),
any(InterruptedException.class)
);
mockedCxLogger.verify(() -> CxLogger.error(
eq(String.format(PluginConstants.ERROR_AUTHENTICATING_AST, "Interrupted")),
any(InterruptedException.class)
));
}
}

Expand All @@ -87,17 +92,18 @@ void testDoAuthenticationCxException() throws Exception {
try (MockedConstruction<CxWrapper> mocked =
Mockito.mockConstruction(CxWrapper.class,
(mock, context) -> when(mock.authValidate())
.thenThrow(new CxException(1, "Cx error")))) {
.thenThrow(new CxException(1, "Cx error")));
MockedStatic<CxLogger> mockedCxLogger = Mockito.mockStatic(CxLogger.class)) {

Authenticator authenticator = new Authenticator(mockLogger);

String result = authenticator.doAuthentication("dummyKey", "--param");

assertEquals("Cx error", result);
verify(mockLogger).error(
eq(String.format(PluginConstants.ERROR_AUTHENTICATING_AST, "Cx error")),
any(CxException.class)
);
mockedCxLogger.verify(() -> CxLogger.error(
eq(String.format(PluginConstants.ERROR_AUTHENTICATING_AST, "Cx error")),
any(CxException.class)
));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ public String doAuthentication(String apiKey, String additionalParams) {
try {
CxWrapper wrapper = new CxWrapper(config, log);
String cxValidateOutput = wrapper.authValidate();
log.info(AUTH_STATUS + cxValidateOutput);
CxLogger.info(AUTH_STATUS + cxValidateOutput);
return cxValidateOutput;
} catch (IOException | InterruptedException | CxException e) {
log.error(String.format(PluginConstants.ERROR_AUTHENTICATING_AST, e.getMessage()), e);
CxLogger.error(String.format(PluginConstants.ERROR_AUTHENTICATING_AST, e.getMessage()), e);
return e.getMessage();
}
}
Expand Down
Loading