|
| 1 | +package checkmarx.ast.eclipse.plugin.tests.unit.runner; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.*; |
| 4 | +import static org.mockito.Mockito.*; |
| 5 | + |
| 6 | +import java.io.IOException; |
| 7 | + |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | +import org.mockito.MockedConstruction; |
| 10 | +import org.mockito.Mockito; |
| 11 | +import org.slf4j.Logger; |
| 12 | + |
| 13 | +import com.checkmarx.ast.wrapper.CxException; |
| 14 | +import com.checkmarx.ast.wrapper.CxWrapper; |
| 15 | +import com.checkmarx.eclipse.runner.Authenticator; |
| 16 | +import com.checkmarx.eclipse.utils.PluginConstants; |
| 17 | + |
| 18 | +class AuthenticatorTest { |
| 19 | + |
| 20 | + @Test |
| 21 | + void testDoAuthenticationSuccess() throws Exception { |
| 22 | + |
| 23 | + Logger mockLogger = mock(Logger.class); |
| 24 | + |
| 25 | + try (MockedConstruction<CxWrapper> mocked = |
| 26 | + Mockito.mockConstruction(CxWrapper.class, |
| 27 | + (mock, context) -> when(mock.authValidate()).thenReturn("SUCCESS"))) { |
| 28 | + |
| 29 | + Authenticator authenticator = new Authenticator(mockLogger); |
| 30 | + |
| 31 | + String result = authenticator.doAuthentication("dummyKey", "--param"); |
| 32 | + |
| 33 | + assertEquals("SUCCESS", result); |
| 34 | + verify(mockLogger).info("Authentication Status: SUCCESS"); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + void testDoAuthenticationIOException() throws Exception { |
| 40 | + |
| 41 | + Logger mockLogger = mock(Logger.class); |
| 42 | + |
| 43 | + try (MockedConstruction<CxWrapper> mocked = |
| 44 | + Mockito.mockConstruction(CxWrapper.class, |
| 45 | + (mock, context) -> when(mock.authValidate()) |
| 46 | + .thenThrow(new IOException("IO error")))) { |
| 47 | + |
| 48 | + Authenticator authenticator = new Authenticator(mockLogger); |
| 49 | + |
| 50 | + String result = authenticator.doAuthentication("dummyKey", "--param"); |
| 51 | + |
| 52 | + assertEquals("IO error", result); |
| 53 | + verify(mockLogger).error( |
| 54 | + eq(String.format(PluginConstants.ERROR_AUTHENTICATING_AST, "IO error")), |
| 55 | + any(IOException.class) |
| 56 | + ); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void testDoAuthenticationInterruptedException() throws Exception { |
| 62 | + |
| 63 | + Logger mockLogger = mock(Logger.class); |
| 64 | + |
| 65 | + try (MockedConstruction<CxWrapper> mocked = |
| 66 | + Mockito.mockConstruction(CxWrapper.class, |
| 67 | + (mock, context) -> when(mock.authValidate()) |
| 68 | + .thenThrow(new InterruptedException("Interrupted")))) { |
| 69 | + |
| 70 | + Authenticator authenticator = new Authenticator(mockLogger); |
| 71 | + |
| 72 | + String result = authenticator.doAuthentication("dummyKey", "--param"); |
| 73 | + |
| 74 | + assertEquals("Interrupted", result); |
| 75 | + verify(mockLogger).error( |
| 76 | + eq(String.format(PluginConstants.ERROR_AUTHENTICATING_AST, "Interrupted")), |
| 77 | + any(InterruptedException.class) |
| 78 | + ); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + void testDoAuthenticationCxException() throws Exception { |
| 84 | + |
| 85 | + Logger mockLogger = mock(Logger.class); |
| 86 | + |
| 87 | + try (MockedConstruction<CxWrapper> mocked = |
| 88 | + Mockito.mockConstruction(CxWrapper.class, |
| 89 | + (mock, context) -> when(mock.authValidate()) |
| 90 | + .thenThrow(new CxException(1, "Cx error")))) { |
| 91 | + |
| 92 | + Authenticator authenticator = new Authenticator(mockLogger); |
| 93 | + |
| 94 | + String result = authenticator.doAuthentication("dummyKey", "--param"); |
| 95 | + |
| 96 | + assertEquals("Cx error", result); |
| 97 | + verify(mockLogger).error( |
| 98 | + eq(String.format(PluginConstants.ERROR_AUTHENTICATING_AST, "Cx error")), |
| 99 | + any(CxException.class) |
| 100 | + ); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + void testSingletonInstanceNotNull() { |
| 106 | + assertNotNull(Authenticator.INSTANCE); |
| 107 | + } |
| 108 | +} |
0 commit comments