diff options
Diffstat (limited to 'aai-els-onap-logging/src/test/java/org/onap/aai/logging')
11 files changed, 860 insertions, 0 deletions
diff --git a/aai-els-onap-logging/src/test/java/org/onap/aai/logging/CNNameTest.java b/aai-els-onap-logging/src/test/java/org/onap/aai/logging/CNNameTest.java new file mode 100644 index 00000000..01b7bda1 --- /dev/null +++ b/aai-els-onap-logging/src/test/java/org/onap/aai/logging/CNNameTest.java @@ -0,0 +1,108 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.aai.logging; + +import ch.qos.logback.access.spi.IAccessEvent; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Spy; +import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.mock.web.MockHttpServletRequest; + +import javax.security.auth.x500.X500Principal; +import java.security.cert.X509Certificate; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class CNNameTest { + @Mock + X509Certificate cert; + + @Mock + IAccessEvent accessEvent; + + @Spy + @InjectMocks + private CNName cnName; + + @Before + public void setup() { + when(cnName.isStarted()).thenReturn(true); + } + @Test + public void basicAuthTest(){ + + MockHttpServletRequest https = new MockHttpServletRequest(); + https.addHeader("Authorization", "Basic dXNlcjpwYXNzd29yZA=="); + https.setAttribute("javax.servlet.request.cipher_suite", ""); + https.setAttribute("javax.servlet.request.X509Certificate", null); + + when(accessEvent.getRequest()).thenReturn(https); + assertEquals("user", cnName.convert(accessEvent)); + + } + @Test + public void incorrectHeaderBasicAuthTest(){ + + MockHttpServletRequest https = new MockHttpServletRequest(); + + https.addHeader("Authorization", "dXNlcjpwYXNzd29yZA=="); + https.setAttribute("javax.servlet.request.cipher_suite", ""); + https.setAttribute("javax.servlet.request.X509Certificate", null); + + when(accessEvent.getRequest()).thenReturn(https); + assertEquals("-", cnName.convert(accessEvent)); + + } + @Test + public void noCipherSuiteTest(){ + + MockHttpServletRequest https = new MockHttpServletRequest(); + + https.addHeader("Authorization", "Basic dXNlcjpwYXNzd29yZA=="); + https.setAttribute("javax.servlet.request.cipher_suite", null); + https.setAttribute("javax.servlet.request.X509Certificate", null); + + when(accessEvent.getRequest()).thenReturn(https); + assertEquals("-", cnName.convert(accessEvent)); + + } + @Test + public void certificateTest(){ + String testSubject = "CN=TestName, OU=TestOU, O=TestOrg, C=Country"; + X509Certificate[] certChain = { cert }; + MockHttpServletRequest https = new MockHttpServletRequest(); + + https.setAttribute("javax.servlet.request.cipher_suite", ""); + https.setAttribute("javax.servlet.request.X509Certificate", certChain ); + + when(accessEvent.getRequest()).thenReturn(https); + when(cert.getSubjectX500Principal()).thenReturn(new X500Principal(testSubject) ); + + assertEquals(testSubject, cnName.convert(accessEvent)); + } + +} diff --git a/aai-els-onap-logging/src/test/java/org/onap/aai/logging/CustomLogPatternLayoutTest.java b/aai-els-onap-logging/src/test/java/org/onap/aai/logging/CustomLogPatternLayoutTest.java new file mode 100644 index 00000000..71a07aa1 --- /dev/null +++ b/aai-els-onap-logging/src/test/java/org/onap/aai/logging/CustomLogPatternLayoutTest.java @@ -0,0 +1,38 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.aai.logging; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class CustomLogPatternLayoutTest { + + private CustomLogPatternLayout customLogPatternLayout = new CustomLogPatternLayout(); + + @Test + public void defaultConverterTest(){ + + assertEquals(customLogPatternLayout.getDefaultConverterMap().get("z"), CNName.class.getName()); + assertEquals(customLogPatternLayout.getDefaultConverterMap().get("y"), DME2RestFlag.class.getName()); + + } +} diff --git a/aai-els-onap-logging/src/test/java/org/onap/aai/logging/DME2RestFlagTest.java b/aai-els-onap-logging/src/test/java/org/onap/aai/logging/DME2RestFlagTest.java new file mode 100644 index 00000000..737cb624 --- /dev/null +++ b/aai-els-onap-logging/src/test/java/org/onap/aai/logging/DME2RestFlagTest.java @@ -0,0 +1,67 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.aai.logging; + +import ch.qos.logback.access.spi.IAccessEvent; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Spy; +import org.mockito.junit.MockitoJUnitRunner; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class DME2RestFlagTest { + + @Mock + IAccessEvent accessEvent; + + @Spy + @InjectMocks + private DME2RestFlag dme2RestFlag; + + @Before + public void setup() { + when(dme2RestFlag.isStarted()).thenReturn(true); + } + @Test + public void dme2Test(){ + String[] contextArray = {"a", "b", "c"}; + String[] routeOfferArray = {"d", "e", "f"}; + String[] versionArray = {"1", "2", "3"}; + when(accessEvent.getRequestParameter("envContext")).thenReturn(contextArray); + when(accessEvent.getRequestParameter("routeOffer")).thenReturn(routeOfferArray); + when(accessEvent.getRequestParameter("version")).thenReturn(versionArray); + assertEquals("DME2", dme2RestFlag.convert(accessEvent)); + } + @Test + public void restTest(){ + String[] contextArray = {""}; + String[] routeOfferArray = {""}; + String[] versionArray = {""}; + when(accessEvent.getRequestParameter("envContext")).thenReturn(contextArray); + assertEquals("REST", dme2RestFlag.convert(accessEvent)); + } +} diff --git a/aai-els-onap-logging/src/test/java/org/onap/aai/logging/ErrorLogHelperTest.java b/aai-els-onap-logging/src/test/java/org/onap/aai/logging/ErrorLogHelperTest.java new file mode 100644 index 00000000..ae174691 --- /dev/null +++ b/aai-els-onap-logging/src/test/java/org/onap/aai/logging/ErrorLogHelperTest.java @@ -0,0 +1,151 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.aai.logging; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.aai.domain.restPolicyException.PolicyException; +import org.onap.aai.domain.restPolicyException.RESTResponse; +import org.onap.aai.domain.restPolicyException.RequestError; +import org.onap.aai.domain.restServiceException.ServiceException; +import org.onap.aai.exceptions.AAIException; +import org.onap.aai.util.LogFile; +import org.onap.aai.util.MapperUtil; +import org.slf4j.MDC; + +import javax.ws.rs.core.MediaType; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static java.lang.Thread.sleep; +import static org.junit.Assert.*; + +public class ErrorLogHelperTest { + + private static final String ErrorLogFileName = "error.log"; + + @Before + public void init() { + System.setProperty("AJSC_HOME", "."); + + } + @After + public void cleanup() throws IOException{ + MDC.clear(); + LogFile.deleteContents(ErrorLogFileName); + } + @Test + public void logErrorTest() throws IOException, InterruptedException { + //||main|UNKNOWN||||ERROR|500|Node cannot be deleted:3100:Bad Request:|ERR.5.4.6110 + ErrorLogHelper.logError("AAI_6110"); + sleep(5000); + String logContents = LogFile.getContents(ErrorLogFileName); + + assertNotNull(logContents); + + String logContentParts[] = logContents.split("\\|"); + + assertTrue(logContentParts.length >= 11 ); + assertEquals ("ERROR", logContentParts[7]); + assertEquals (AaiElsErrorCode.BUSINESS_PROCESS_ERROR, logContentParts[8]); + assertTrue (logContentParts[10].startsWith("ERR.5.4.6110")); + } + + @Test + public void logErrorWithMessageTest() throws IOException, InterruptedException { + //||main|UNKNOWN||||ERROR|500|Node cannot be deleted:3100:Bad Request:|ERR.5.4.6110 message + String errorMessage = "Object is referenced by additional objects"; + ErrorLogHelper.logError("AAI_6110", errorMessage); + sleep(5000); + String logContents = LogFile.getContents(ErrorLogFileName); + + assertNotNull(logContents); + + String logContentParts[] = logContents.split("\\|"); + + assertTrue(logContentParts.length >= 11 ); + assertTrue (logContentParts[9].contains(errorMessage)); + assertTrue (logContentParts[10].startsWith("ERR.5.4.6110")); + } + + @Test + public void getRESTAPIPolicyErrorResponseTest() throws AAIException{ + //AAI_3002=5:1:WARN:3002:400:3002:Error writing output performing %1 on %2:300 + ArrayList<MediaType> headers = new ArrayList<MediaType>(Arrays.asList(MediaType.APPLICATION_JSON_TYPE)); + ArrayList<String> args = new ArrayList<String>(Arrays.asList("PUT", "resource")); + + AAIException aaie = new AAIException("AAI_3002"); + String errorResponse = ErrorLogHelper.getRESTAPIErrorResponse(headers, aaie, args); + assertNotNull(errorResponse); + + RESTResponse resp = MapperUtil.readAsObjectOf(RESTResponse.class, errorResponse); + RequestError requestError = resp.getRequestError(); + assertNotNull(requestError); + PolicyException policyException = requestError.getPolicyException(); + assertNotNull(policyException); + assertEquals("POL3002", policyException.getMessageId()); + + List<String> vars = policyException.getVariables(); + assertTrue(vars.contains("PUT")); + assertTrue(vars.contains("resource")); + } + @Test + public void getRESTAPIServiceErrorResponseTest() throws AAIException{ + //AAI_3009=5:6:WARN:3009:400:3009:Malformed URL:300 + ArrayList<MediaType> headers = new ArrayList<MediaType>(Arrays.asList(MediaType.APPLICATION_JSON_TYPE)); + ArrayList<String> args = new ArrayList<String>(); + + AAIException aaie = new AAIException("AAI_3009"); + String errorResponse = ErrorLogHelper.getRESTAPIErrorResponse(headers, aaie, args); + assertNotNull(errorResponse); + + org.onap.aai.domain.restServiceException.RESTResponse resp = MapperUtil.readAsObjectOf(org.onap.aai.domain.restServiceException.RESTResponse.class, errorResponse); + org.onap.aai.domain.restServiceException.RequestError requestError = resp.getRequestError(); + assertNotNull(requestError); + ServiceException serviceException = requestError.getServiceException(); + assertNotNull(serviceException); + assertEquals("SVC3009", serviceException.getMessageId()); + + } + @Test + public void getRESTAPIServiceErrorResponseWithLoggingTest() throws IOException, InterruptedException{ + //AAI_3009=5:6:WARN:3009:400:3009:Malformed URL:300 + ArrayList<MediaType> headers = new ArrayList<MediaType>(Arrays.asList(MediaType.APPLICATION_JSON_TYPE)); + ArrayList<String> args = new ArrayList<String>(); + + AAIException aaie = new AAIException("AAI_3009"); + String errorResponse = ErrorLogHelper.getRESTAPIErrorResponseWithLogging(headers, aaie, args); + sleep(5000); + assertNotNull(errorResponse); + String logContents = LogFile.getContents(ErrorLogFileName); + + assertNotNull(logContents); + String logContentParts[] = logContents.split("\\|"); + + assertTrue(logContentParts.length >= 11 ); + assertTrue (logContentParts[10].startsWith("ERR.5.6.3009")); + + } + +} diff --git a/aai-els-onap-logging/src/test/java/org/onap/aai/logging/ErrorObjectFormatExceptionTest.java b/aai-els-onap-logging/src/test/java/org/onap/aai/logging/ErrorObjectFormatExceptionTest.java new file mode 100644 index 00000000..a5a8e496 --- /dev/null +++ b/aai-els-onap-logging/src/test/java/org/onap/aai/logging/ErrorObjectFormatExceptionTest.java @@ -0,0 +1,35 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.aai.logging; + +import org.junit.Test; + +import static org.junit.Assert.assertTrue; + +public class ErrorObjectFormatExceptionTest { + + @Test + public void errorObjectFormatExceptionTest() { + ErrorObjectFormatException e = new ErrorObjectFormatException(); + assertTrue(e instanceof Exception); + } + +} diff --git a/aai-els-onap-logging/src/test/java/org/onap/aai/logging/ErrorObjectNotFoundExceptionTest.java b/aai-els-onap-logging/src/test/java/org/onap/aai/logging/ErrorObjectNotFoundExceptionTest.java new file mode 100644 index 00000000..8bffe4ff --- /dev/null +++ b/aai-els-onap-logging/src/test/java/org/onap/aai/logging/ErrorObjectNotFoundExceptionTest.java @@ -0,0 +1,100 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.aai.logging; + +import org.junit.Test; + +import java.io.IOException; + +import static org.junit.Assert.*; + +public class ErrorObjectNotFoundExceptionTest { + + @Test + public void defaultExceptionTest() { + ErrorObjectNotFoundException e = new ErrorObjectNotFoundException(); + assertTrue(e instanceof Exception); + } + @Test + public void errorObjectNotFoundExceptionWithMessageTest() { + ErrorObjectNotFoundException e = new ErrorObjectNotFoundException("Error Message"); + assertTrue(e instanceof Exception); + assertEquals("Error Message", e.getMessage()); + } + @Test + public void errorObjectNotFoundExceptionWithCauseTest() { + ErrorObjectNotFoundException e = new ErrorObjectNotFoundException(new ArithmeticException()); + assertTrue(e instanceof Exception); + assertTrue(e.getCause() instanceof ArithmeticException); + } + @Test + public void errorObjectNotFoundExceptionWithMsgCauseTest() { + ErrorObjectNotFoundException e = new ErrorObjectNotFoundException("Error Message", new ArithmeticException()); + assertTrue(e instanceof Exception); + assertTrue(e.getCause() instanceof ArithmeticException); + assertEquals("Error Message", e.getMessage()); + } + @Test + public void errorObjectNotFoundExceptionTest() { + + ErrorObjectNotFoundException e = new ErrorObjectNotFoundException("Error Message", new ArithmeticException(), true, true); + assertTrue(e instanceof Exception); + assertTrue(e.getCause() instanceof ArithmeticException); + assertEquals("Error Message", e.getMessage()); + e.addSuppressed(new IOException("Test IO Exception")); + assertTrue((e.getSuppressed())[0] instanceof IOException); + assertEquals("Test IO Exception", (e.getSuppressed())[0].getMessage()); + + ErrorObjectNotFoundException e1 = new ErrorObjectNotFoundException("Error Message", new ArithmeticException(), false, true); + e1.addSuppressed(new IOException("Test IO Exception")); + assertTrue((e1.getSuppressed() == null) || e1.getSuppressed().length == 0); + + } + + @Test + public void errorObjectNotFoundExceptionStackTraceTest() { + + ErrorObjectNotFoundException e1 = new ErrorObjectNotFoundException("Error Message", new ArithmeticException(), true, true); + try { + throw e1; + } + catch (ErrorObjectNotFoundException e) { + StackTraceElement[] stackTrace = e.getStackTrace(); + assertNotNull(stackTrace); + assertTrue(stackTrace.length > 0); + } + + ErrorObjectNotFoundException e2 = new ErrorObjectNotFoundException("Error Message", new ArithmeticException(), true, false); + try { + throw e2; + } + catch (ErrorObjectNotFoundException e) { + StackTraceElement[] stackTrace = e.getStackTrace(); + assertTrue(stackTrace == null || stackTrace.length == 0); + } + + } + /*public ErrorObjectNotFoundException(String message, Throwable cause, boolean enableSuppression, + boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + // TODO Auto-generated constructor stub + }*/ +} diff --git a/aai-els-onap-logging/src/test/java/org/onap/aai/logging/ErrorObjectTest.java b/aai-els-onap-logging/src/test/java/org/onap/aai/logging/ErrorObjectTest.java new file mode 100644 index 00000000..ff20105c --- /dev/null +++ b/aai-els-onap-logging/src/test/java/org/onap/aai/logging/ErrorObjectTest.java @@ -0,0 +1,95 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.aai.logging; + +import org.junit.Test; + +import javax.ws.rs.core.Response; + +import static org.junit.Assert.assertEquals; + +public class ErrorObjectTest { + private ErrorObject errorObject; + private static final String ERROR_DISPOSITION = "5"; + private static final String ERROR_SEVERITY = "ERROR"; + private static final String ERROR_CATEGORY = "4"; + private static final Integer ERROR_HTTP_RESPONSE_CODE = new Integer(401); + private static final String ERROR_REST_CODE = "3300"; + private static final String ERROR_CODE = "4000"; + private static final String ERROR_TEXT = "Test data error"; + private static final String ERROR_PATTERN = "ERR.5.4.4000"; + private static final String ERROR_SEVERITY_CODE = "2"; + @Test + public void errorObjectDefaultConstructorTest() { + errorObject = new ErrorObject(); + assertEquals("3002", errorObject.getRESTErrorCode()); + assertEquals(Response.Status.fromStatusCode(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()), errorObject.getHTTPResponseCode()); + assertEquals(AaiElsErrorCode.UNKNOWN_ERROR,errorObject.getAaiElsErrorCode()); + assertEquals(ERROR_SEVERITY_CODE, errorObject.getSeverityCode(ERROR_SEVERITY)); + } + @Test + public void errorObjectConstructor7Test() { + errorObject = new ErrorObject(ERROR_DISPOSITION, ERROR_CATEGORY, ERROR_SEVERITY, ERROR_HTTP_RESPONSE_CODE, + ERROR_REST_CODE, ERROR_CODE, ERROR_TEXT); + assertEquals(ERROR_DISPOSITION, errorObject.getDisposition()); + assertEquals(ERROR_SEVERITY, errorObject.getSeverity()); + assertEquals(ERROR_CATEGORY, errorObject.getCategory()); + assertEquals(Response.Status.fromStatusCode(ERROR_HTTP_RESPONSE_CODE), errorObject.getHTTPResponseCode()); + assertEquals(ERROR_REST_CODE, errorObject.getRESTErrorCode()); + assertEquals(ERROR_CODE, errorObject.getErrorCode()); + assertEquals(ERROR_TEXT, errorObject.getErrorText()); + assertEquals(AaiElsErrorCode.UNKNOWN_ERROR,errorObject.getAaiElsErrorCode()); + assertEquals(ERROR_SEVERITY_CODE, errorObject.getSeverityCode(ERROR_SEVERITY)); + } + + @Test + public void errorObjectConstructor5Test() { + errorObject = new ErrorObject(ERROR_SEVERITY, ERROR_CODE, ERROR_TEXT, ERROR_DISPOSITION, ERROR_CATEGORY); + assertEquals(ERROR_DISPOSITION, errorObject.getDisposition()); + assertEquals(ERROR_SEVERITY, errorObject.getSeverity()); + assertEquals(ERROR_CATEGORY, errorObject.getCategory()); + assertEquals(Response.Status.fromStatusCode(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()), errorObject.getHTTPResponseCode()); + assertEquals("3002", errorObject.getRESTErrorCode()); + assertEquals(ERROR_CODE, errorObject.getErrorCode()); + assertEquals(ERROR_TEXT, errorObject.getErrorText()); + assertEquals(AaiElsErrorCode.UNKNOWN_ERROR,errorObject.getAaiElsErrorCode()); + assertEquals(ERROR_PATTERN, errorObject.getErrorCodeString()); + assertEquals(ERROR_SEVERITY_CODE, errorObject.getSeverityCode(ERROR_SEVERITY)); + } + + @Test + public void errorObjectConstructor6Test() { + errorObject = new ErrorObject(ERROR_SEVERITY, ERROR_HTTP_RESPONSE_CODE, ERROR_CODE, ERROR_TEXT, ERROR_DISPOSITION, ERROR_CATEGORY); + assertEquals(ERROR_DISPOSITION, errorObject.getDisposition()); + assertEquals(ERROR_SEVERITY, errorObject.getSeverity()); + assertEquals(ERROR_CATEGORY, errorObject.getCategory()); + assertEquals(Response.Status.fromStatusCode(ERROR_HTTP_RESPONSE_CODE), errorObject.getHTTPResponseCode()); + assertEquals("3002", errorObject.getRESTErrorCode()); + assertEquals(ERROR_CODE, errorObject.getErrorCode()); + assertEquals(ERROR_TEXT, errorObject.getErrorText()); + assertEquals(AaiElsErrorCode.UNKNOWN_ERROR, errorObject.getAaiElsErrorCode()); + assertEquals(ERROR_PATTERN, errorObject.getErrorCodeString()); + assertEquals(ERROR_SEVERITY_CODE, errorObject.getSeverityCode(ERROR_SEVERITY)); + + } + + +} diff --git a/aai-els-onap-logging/src/test/java/org/onap/aai/logging/LogFormatToolsTest.java b/aai-els-onap-logging/src/test/java/org/onap/aai/logging/LogFormatToolsTest.java new file mode 100644 index 00000000..695d7c08 --- /dev/null +++ b/aai-els-onap-logging/src/test/java/org/onap/aai/logging/LogFormatToolsTest.java @@ -0,0 +1,39 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.aai.logging; + +import org.junit.Test; +import org.onap.aai.exceptions.AAIException; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class LogFormatToolsTest { + + @Test + public void testLogFormatStackTrace() { + AAIException aaiException = new AAIException("AAI_4009", new ArithmeticException()); + + String stackTrace = LogFormatTools.getStackTop(aaiException); + assertTrue(stackTrace.startsWith("root cause=java.lang.ArithmeticException")); + assertFalse(stackTrace.contains("\\n")); + } +} diff --git a/aai-els-onap-logging/src/test/java/org/onap/aai/logging/LoggingContextTest.java b/aai-els-onap-logging/src/test/java/org/onap/aai/logging/LoggingContextTest.java new file mode 100644 index 00000000..245840bc --- /dev/null +++ b/aai-els-onap-logging/src/test/java/org/onap/aai/logging/LoggingContextTest.java @@ -0,0 +1,78 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.aai.logging; + +import org.junit.After; +import org.junit.Test; +import org.slf4j.MDC; + +import java.util.concurrent.TimeUnit; + +import static org.junit.Assert.*; + +public class LoggingContextTest { + + @After + public void cleanup(){ + MDC.clear(); + } + @Test + public void elapsedTimeTest() { + LoggingContext.elapsedTime(300, TimeUnit.MILLISECONDS); + assertEquals(MDC.get(LoggingContext.LoggingField.ELAPSED_TIME.toString()), "300"); + LoggingContext.init(); + assertTrue(MDC.get(LoggingContext.LoggingField.ELAPSED_TIME.toString()) == null); + } + @Test + public void stopWatchTest() { + LoggingContext.init(); + assertFalse(LoggingContext.isStopWatchStarted()); + + LoggingContext.stopWatchStart(); + assertTrue(LoggingContext.isStopWatchStarted()); + + double elapsedTime = LoggingContext.stopWatchStop(); + assertFalse(LoggingContext.isStopWatchStarted()); + assertTrue(elapsedTime > 0); + } + @Test + public void putClearTest() { + String testServiceName = "TEST-SVC-NAME"; + LoggingContext.put(LoggingContext.LoggingField.SERVICE_NAME.toString(), testServiceName); + assertEquals(testServiceName, MDC.get(LoggingContext.LoggingField.SERVICE_NAME.toString())); + + LoggingContext.clear(); + assertTrue(MDC.get(LoggingContext.LoggingField.SERVICE_NAME.toString()) == null); + + } + + @Test + public void removeTest() { + String testServiceName = "TEST-SVC-NAME"; + LoggingContext.put(LoggingContext.LoggingField.SERVICE_NAME.toString(), testServiceName); + assertEquals(testServiceName, MDC.get(LoggingContext.LoggingField.SERVICE_NAME.toString())); + + LoggingContext.remove(LoggingContext.LoggingField.SERVICE_NAME.toString()); + assertTrue(MDC.get(LoggingContext.LoggingField.SERVICE_NAME.toString()) == null); + + } + +} diff --git a/aai-els-onap-logging/src/test/java/org/onap/aai/logging/StopWatchNotStartedExceptionTest.java b/aai-els-onap-logging/src/test/java/org/onap/aai/logging/StopWatchNotStartedExceptionTest.java new file mode 100644 index 00000000..8a4123f0 --- /dev/null +++ b/aai-els-onap-logging/src/test/java/org/onap/aai/logging/StopWatchNotStartedExceptionTest.java @@ -0,0 +1,56 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.aai.logging; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class StopWatchNotStartedExceptionTest { + + + @Test + public void stopWatchNotStartedExceptionTest() { + StopWatchNotStartedException e = new StopWatchNotStartedException(); + assertTrue(e instanceof RuntimeException); + } + @Test + public void stopWatchNotStartedExceptionWithMessageTest() { + StopWatchNotStartedException e = new StopWatchNotStartedException("Error Message"); + assertTrue(e instanceof RuntimeException); + assertEquals("Error Message", e.getMessage()); + } + @Test + public void stopWatchNotStartedExceptionWithCauseTest() { + StopWatchNotStartedException e = new StopWatchNotStartedException(new ArithmeticException()); + assertTrue(e instanceof RuntimeException); + assertTrue(e.getCause() instanceof ArithmeticException); + } + @Test + public void stopWatchNotStartedExceptionWithMsgCauseTest() { + StopWatchNotStartedException e = new StopWatchNotStartedException("Error Message", new ArithmeticException()); + assertTrue(e instanceof RuntimeException); + assertTrue(e.getCause() instanceof ArithmeticException); + assertEquals("Error Message", e.getMessage()); + } + +} diff --git a/aai-els-onap-logging/src/test/java/org/onap/aai/logging/StopWatchTest.java b/aai-els-onap-logging/src/test/java/org/onap/aai/logging/StopWatchTest.java new file mode 100644 index 00000000..50a2d053 --- /dev/null +++ b/aai-els-onap-logging/src/test/java/org/onap/aai/logging/StopWatchTest.java @@ -0,0 +1,93 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.aai.logging; + +import org.junit.After; +import org.junit.Test; +import org.slf4j.MDC; + +import static java.lang.Thread.sleep; +import static org.junit.Assert.*; + +public class StopWatchTest { + + @After + public void cleanup() { + MDC.clear(); + } + @Test + public void elapsedTimeTest() throws InterruptedException { + StopWatch.start(); + sleep(1010); + StopWatch.stop(); + assertTrue(Long.parseLong(MDC.get(LoggingContext.LoggingField.ELAPSED_TIME.toString())) >= 1000L); + } + @Test + public void elapsedTimeConditionalTest() throws InterruptedException { + StopWatch.conditionalStart(); + sleep(1010); + StopWatch.stopIfStarted(); + String elapsedTimeStr = MDC.get(LoggingContext.LoggingField.ELAPSED_TIME.toString()); + long elapsedTime = Long.parseLong(elapsedTimeStr); + assertTrue(elapsedTime >= 1000L); + } + @Test + public void clearTest() throws InterruptedException { + StopWatch.start(); + sleep(1010); + StopWatch.stop(); + assertNotNull( MDC.get(LoggingContext.LoggingField.ELAPSED_TIME.toString())); + + StopWatch.clear(); + assertNull(MDC.get(LoggingContext.LoggingField.STOP_WATCH_START.toString())); + assertNull(MDC.get(LoggingContext.LoggingField.ELAPSED_TIME.toString())); + + } + + @Test + public void stopTest() throws InterruptedException { + StopWatch.start(); + sleep(1010); + StopWatch.stop(); + + String elapsedTimeStr = MDC.get(LoggingContext.LoggingField.ELAPSED_TIME.toString()); + long elapsedTime1 = Long.parseLong(elapsedTimeStr); + + StopWatch.stopIfStarted(); + elapsedTimeStr = MDC.get(LoggingContext.LoggingField.ELAPSED_TIME.toString()); + long elapsedTime2 = Long.parseLong(elapsedTimeStr); + assertTrue(elapsedTime1 == elapsedTime2); + } + + @Test + public void startTest() throws InterruptedException { + StopWatch.start(); + sleep(1010); + StopWatch.conditionalStart(); + StopWatch.stop(); + + String elapsedTimeStr = MDC.get(LoggingContext.LoggingField.ELAPSED_TIME.toString()); + long elapsedTime = Long.parseLong(elapsedTimeStr); + + assertTrue(elapsedTime >= 1000L); + } + +} |