diff options
Diffstat (limited to 'appc-oam/appc-oam-bundle/src/test/java')
5 files changed, 186 insertions, 19 deletions
diff --git a/appc-oam/appc-oam-bundle/src/test/java/org/openecomp/appc/oam/OAMCommandStatusTest.java b/appc-oam/appc-oam-bundle/src/test/java/org/openecomp/appc/oam/OAMCommandStatusTest.java new file mode 100644 index 000000000..c941f43dc --- /dev/null +++ b/appc-oam/appc-oam-bundle/src/test/java/org/openecomp/appc/oam/OAMCommandStatusTest.java @@ -0,0 +1,99 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.appc.oam; + +import org.junit.Assert; +import org.junit.Test; +import org.openecomp.appc.executor.objects.Params; + +import java.util.HashMap; +import java.util.Map; + +public class OAMCommandStatusTest { + private Map<OAMCommandStatus, Integer> CODE_MAP = new HashMap<OAMCommandStatus, Integer>() { + { + put(OAMCommandStatus.ABORT, 304); + put(OAMCommandStatus.ACCEPTED, 100); + put(OAMCommandStatus.INVALID_PARAMETER, 302); + put(OAMCommandStatus.REJECTED, 300); + put(OAMCommandStatus.SUCCESS, 400); + put(OAMCommandStatus.TIMEOUT, 303); + put(OAMCommandStatus.UNEXPECTED_ERROR, 200); + } + }; + private Map<OAMCommandStatus, String> MSG_MAP = new HashMap<OAMCommandStatus, String>() { + { + put(OAMCommandStatus.ABORT, "OPERATION ABORT - ${errorMsg}"); + put(OAMCommandStatus.ACCEPTED, "ACCEPTED - request accepted"); + put(OAMCommandStatus.INVALID_PARAMETER, "INVALID PARAMETER - ${errorMsg}"); + put(OAMCommandStatus.REJECTED, "REJECTED - ${errorMsg}"); + put(OAMCommandStatus.SUCCESS, "SUCCESS - request has been processed successfully"); + put(OAMCommandStatus.TIMEOUT, "OPERATION TIMEOUT REACHED - ${errorMsg}"); + put(OAMCommandStatus.UNEXPECTED_ERROR, "UNEXPECTED ERROR - ${errorMsg}"); + } + }; + + @Test + public void testGetResponseMessage() throws Exception { + for (OAMCommandStatus oamCommandStatus : OAMCommandStatus.values()) { + String expectedMsg = MSG_MAP.get(oamCommandStatus); + Assert.assertEquals(String.format("Should have message (%s).", expectedMsg), + expectedMsg, oamCommandStatus.getResponseMessage()); + } + } + + @Test + public void testGetResponseCode() throws Exception { + for (OAMCommandStatus oamCommandStatus : OAMCommandStatus.values()) { + Integer expectedCode = CODE_MAP.get(oamCommandStatus); + Assert.assertEquals(String.format("Should have code (%d).", expectedCode), + expectedCode, Integer.valueOf(oamCommandStatus.getResponseCode())); + } + } + + @Test + public void testGetFormattedMessage() throws Exception { + String message = "test message"; + Params params = new Params().addParam("errorMsg", message); + for (OAMCommandStatus oamCommandStatus : OAMCommandStatus.values()) { + String expectedMsg1 = MSG_MAP.get(oamCommandStatus); + String expectedMsg2 = expectedMsg1.replaceAll("\\$\\{errorMsg\\}", message); + Assert.assertEquals("Should returned " + expectedMsg1, + expectedMsg1, oamCommandStatus.getFormattedMessage(null)); + Assert.assertEquals("Should returned " + expectedMsg2, + expectedMsg2, oamCommandStatus.getFormattedMessage(params)); + } + } + + @Test + public void testToString() throws Exception { + for (OAMCommandStatus oamCommandStatus : OAMCommandStatus.values()) { + String expectedString = String.format(oamCommandStatus.TO_STRING_FORMAT, + CODE_MAP.get(oamCommandStatus), MSG_MAP.get(oamCommandStatus)); + Assert.assertEquals(String.format("Should have string (%s).", expectedString), + expectedString, oamCommandStatus.toString()); + } + } +} diff --git a/appc-oam/appc-oam-bundle/src/test/java/org/openecomp/appc/oam/processor/BaseActionRunnableTest.java b/appc-oam/appc-oam-bundle/src/test/java/org/openecomp/appc/oam/processor/BaseActionRunnableTest.java index 70adca0fc..c5ad95e43 100644 --- a/appc-oam/appc-oam-bundle/src/test/java/org/openecomp/appc/oam/processor/BaseActionRunnableTest.java +++ b/appc-oam/appc-oam-bundle/src/test/java/org/openecomp/appc/oam/processor/BaseActionRunnableTest.java @@ -186,11 +186,12 @@ public class BaseActionRunnableTest { @Test public void testSetAbortStatus() throws Exception { testBaseAcionRunnable.setAbortStatus(); - Assert.assertEquals("Should return reject code", OAMCommandStatus.REJECTED.getResponseCode(), + Assert.assertEquals("Should return abort code", OAMCommandStatus.ABORT.getResponseCode(), testBaseAcionRunnable.status.getCode().intValue()); - Assert.assertTrue("Should set abort message", + Assert.assertTrue("Should set abort due to execution error message", testBaseAcionRunnable.status.getMessage().endsWith( - String.format(testBaseAcionRunnable.ABORT_MESSAGE_FORMAT, testRpc.name()))); + String.format(testBaseAcionRunnable.ABORT_MESSAGE_FORMAT, + testRpc.name(), testBaseAcionRunnable.DUE_TO_EXECUTION_ERROR))); } @Test @@ -281,4 +282,20 @@ public class BaseActionRunnableTest { String.format(testBaseAcionRunnable.BUNDLE_OPERATION_FAILED_FORMAT, failedNumber)); Mockito.verify(testBaseAcionRunnable, times(1)).postAction(AppcOamStates.Error); } + + @Test + public void testAbortRunnable() throws Exception { + Mockito.doReturn(AppcOamStates.Restarting).when(mockStateHelper).getCurrentOamState(); + AppcOam.RPC newRpc = AppcOam.RPC.restart; + testBaseAcionRunnable.abortRunnable(newRpc); + Assert.assertEquals("Should return abort code", OAMCommandStatus.ABORT.getResponseCode(), + testBaseAcionRunnable.status.getCode().intValue()); + Assert.assertTrue("Should set abort due to new request message", + testBaseAcionRunnable.status.getMessage().endsWith( + String.format(testBaseAcionRunnable.ABORT_MESSAGE_FORMAT, testRpc.name(), + String.format(testBaseAcionRunnable.NEW_RPC_OPERATION_REQUEST, newRpc.name())))); + Mockito.verify(mockOperHelper, times(1)).sendNotificationMessage(any(), any(), any()); + Mockito.verify(testBaseAcionRunnable, times(1)).resetLogProperties(false); + Mockito.verify(testBaseAcionRunnable, times(1)).resetLogProperties(true); + } } diff --git a/appc-oam/appc-oam-bundle/src/test/java/org/openecomp/appc/oam/processor/BaseCommonTest.java b/appc-oam/appc-oam-bundle/src/test/java/org/openecomp/appc/oam/processor/BaseCommonTest.java index 4e1025781..3a9e76f27 100644 --- a/appc-oam/appc-oam-bundle/src/test/java/org/openecomp/appc/oam/processor/BaseCommonTest.java +++ b/appc-oam/appc-oam-bundle/src/test/java/org/openecomp/appc/oam/processor/BaseCommonTest.java @@ -22,14 +22,15 @@ * ============LICENSE_END========================================================= */ - package org.openecomp.appc.oam.processor; import com.att.eelf.configuration.EELFLogger; import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; import org.mockito.Mockito; +import org.opendaylight.yang.gen.v1.org.openecomp.appc.oam.rev170303.common.header.CommonHeader; import org.opendaylight.yang.gen.v1.org.openecomp.appc.oam.rev170303.status.Status; import org.openecomp.appc.exceptions.InvalidInputException; import org.openecomp.appc.exceptions.InvalidStateException; @@ -39,10 +40,21 @@ import org.openecomp.appc.oam.util.ConfigurationHelper; import org.openecomp.appc.oam.util.OperationHelper; import org.openecomp.appc.oam.util.StateHelper; import org.openecomp.appc.statemachine.impl.readers.AppcOamStates; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.reflect.Whitebox; +import org.slf4j.MDC; + +import java.util.Map; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.powermock.api.mockito.PowerMockito.mockStatic; +@RunWith(PowerMockRunner.class) +@PrepareForTest({BaseCommon.class, MDC.class}) public class BaseCommonTest { private class TestAbc extends BaseCommon { @@ -65,10 +77,14 @@ public class BaseCommonTest { private TestAbc testBaseCommon; private ConfigurationHelper mockConfigHelper = mock(ConfigurationHelper.class); private StateHelper mockStateHelper = mock(StateHelper.class); + private CommonHeader mockCommonHeader = mock(CommonHeader.class); @Before public void setUp() throws Exception { - testBaseCommon = new TestAbc(null, mockConfigHelper, mockStateHelper, null); + testBaseCommon = spy(new TestAbc(null, mockConfigHelper, mockStateHelper, null)); + + Whitebox.setInternalState(testBaseCommon, "commonHeader", mockCommonHeader); + Whitebox.setInternalState(testBaseCommon, "rpc", AppcOam.RPC.maintenance_mode); // to avoid operation on logger fail, mock up the logger EELFLogger mockLogger = mock(EELFLogger.class); @@ -98,7 +114,8 @@ public class BaseCommonTest { public void testSetErrorStatus() throws Exception { Mockito.doReturn("testName").when(mockConfigHelper).getAppcName(); Mockito.doReturn(AppcOamStates.Started).when(mockStateHelper).getCurrentOamState(); - Whitebox.setInternalState(testBaseCommon, "rpc", AppcOam.RPC.maintenance_mode); + Mockito.doReturn("testRequestId").when(mockCommonHeader).getRequestId(); + Mockito.doReturn("testOrigId").when(mockCommonHeader).getOriginatorId(); String exceptionMessage = "testing"; @@ -107,18 +124,50 @@ public class BaseCommonTest { testBaseCommon.setErrorStatus(t); Status status = testBaseCommon.status; Assert.assertEquals("Should have code", oamCommandStatus.getResponseCode(), status.getCode().intValue()); + Mockito.verify(testBaseCommon, times(1)).resetLogProperties(false); + Mockito.verify(testBaseCommon, times(1)).resetLogProperties(true); oamCommandStatus = OAMCommandStatus.REJECTED; t = new InvalidStateException(exceptionMessage); testBaseCommon.setErrorStatus(t); status = testBaseCommon.status; Assert.assertEquals("Should have code", oamCommandStatus.getResponseCode(), status.getCode().intValue()); + Mockito.verify(testBaseCommon, times(2)).resetLogProperties(false); + Mockito.verify(testBaseCommon, times(2)).resetLogProperties(true); oamCommandStatus = OAMCommandStatus.UNEXPECTED_ERROR; t = new NullPointerException(exceptionMessage); testBaseCommon.setErrorStatus(t); status = testBaseCommon.status; Assert.assertEquals("Should have code", oamCommandStatus.getResponseCode(), status.getCode().intValue()); + Mockito.verify(testBaseCommon, times(3)).resetLogProperties(false); + Mockito.verify(testBaseCommon, times(3)).resetLogProperties(true); + } + + @Test + public void testSetInitialLogProperties() throws Exception { + mockStatic(MDC.class); + testBaseCommon.setInitialLogProperties(); + PowerMockito.verifyStatic(times(5)); } + @Test + public void testClearRequestLogProperties() throws Exception { + mockStatic(MDC.class); + testBaseCommon.clearRequestLogProperties(); + PowerMockito.verifyStatic(times(5)); + } + + @Test + public void testResetLogProperties() throws Exception { + testBaseCommon.setInitialLogProperties(); + + testBaseCommon.resetLogProperties(false); + Mockito.verify(testBaseCommon, times(2)).setInitialLogProperties(); + Map<String, String> oldMdcMap = Whitebox.getInternalState(testBaseCommon, "oldMdcContent"); + Assert.assertTrue("Should have 5 entries in persisted map", oldMdcMap.size() == 5); + + testBaseCommon.resetLogProperties(false); + Mockito.verify(testBaseCommon, times(3)).setInitialLogProperties(); + } } diff --git a/appc-oam/appc-oam-bundle/src/test/java/org/openecomp/appc/oam/processor/BaseProcessorTest.java b/appc-oam/appc-oam-bundle/src/test/java/org/openecomp/appc/oam/processor/BaseProcessorTest.java index 7a023e162..354053c8e 100644 --- a/appc-oam/appc-oam-bundle/src/test/java/org/openecomp/appc/oam/processor/BaseProcessorTest.java +++ b/appc-oam/appc-oam-bundle/src/test/java/org/openecomp/appc/oam/processor/BaseProcessorTest.java @@ -165,13 +165,10 @@ public class BaseProcessorTest { testBaseProcessor.scheduleAsyncTask(); Mockito.verify(mockTaskHelper, times(0)).scheduleAsyncTask(any(), any()); - // test runnable - Runnable runnable = () -> { - // do nothing - }; - Whitebox.setInternalState(testBaseProcessor, "runnable", runnable); + BaseActionRunnable mockRunnable = mock(BaseActionRunnable.class); + Whitebox.setInternalState(testBaseProcessor, "runnable", mockRunnable); testBaseProcessor.scheduleAsyncTask(); - Mockito.verify(mockTaskHelper, times(1)).scheduleAsyncTask(testRpc, runnable); + Mockito.verify(mockTaskHelper, times(1)).scheduleAsyncTask(testRpc, mockRunnable); } @Test diff --git a/appc-oam/appc-oam-bundle/src/test/java/org/openecomp/appc/oam/util/AsyncTaskHelperTest.java b/appc-oam/appc-oam-bundle/src/test/java/org/openecomp/appc/oam/util/AsyncTaskHelperTest.java index d080b735b..873b21795 100644 --- a/appc-oam/appc-oam-bundle/src/test/java/org/openecomp/appc/oam/util/AsyncTaskHelperTest.java +++ b/appc-oam/appc-oam-bundle/src/test/java/org/openecomp/appc/oam/util/AsyncTaskHelperTest.java @@ -30,6 +30,7 @@ import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; import org.openecomp.appc.oam.AppcOam; +import org.openecomp.appc.oam.processor.BaseActionRunnable; import org.powermock.reflect.Whitebox; import java.util.Arrays; @@ -44,6 +45,7 @@ import static org.mockito.Mockito.times; public class AsyncTaskHelperTest { private AsyncTaskHelper asyncTaskHelper; private ScheduledExecutorService mockScheduler = mock(ScheduledExecutorService.class); + private BaseActionRunnable mockRunnable = mock(BaseActionRunnable.class); @Before public void setUp() throws Exception { @@ -70,15 +72,14 @@ public class AsyncTaskHelperTest { @Test public void testScheduleAsyncTaskWithMmod() throws Exception { - Runnable mockRunnable = mock(Runnable.class); - - // test maintance mode + // test maintenance mode ScheduledFuture<?> mockTask0 = mock(ScheduledFuture.class); Whitebox.setInternalState(asyncTaskHelper, "backgroundOamTask", mockTask0); ScheduledFuture<?> mockTask1 = mock(ScheduledFuture.class); Mockito.doReturn(mockTask1).when(mockScheduler).scheduleWithFixedDelay( - mockRunnable, asyncTaskHelper.MMODE_TASK_DELAY, asyncTaskHelper.MMODE_TASK_DELAY, TimeUnit.MILLISECONDS); + mockRunnable, asyncTaskHelper.MMODE_TASK_DELAY, + asyncTaskHelper.MMODE_TASK_DELAY, TimeUnit.MILLISECONDS); asyncTaskHelper.scheduleAsyncTask(AppcOam.RPC.maintenance_mode, mockRunnable); Mockito.verify(mockTask0, times(1)).cancel(true); Assert.assertEquals(mockTask1, asyncTaskHelper.scheduleAsyncTask(AppcOam.RPC.maintenance_mode, mockRunnable)); @@ -93,15 +94,18 @@ public class AsyncTaskHelperTest { } private void runTest(AppcOam.RPC rpc) { - Runnable mockRunnable = mock(Runnable.class); ScheduledFuture<?> mockTask0 = mock(ScheduledFuture.class); Whitebox.setInternalState(asyncTaskHelper, "backgroundOamTask", mockTask0); + BaseActionRunnable mockRunnable0 = mock(BaseActionRunnable.class); + Whitebox.setInternalState(asyncTaskHelper, "taskRunnable", mockRunnable0); ScheduledFuture<?> mockTask2 = mock(ScheduledFuture.class); Mockito.doReturn(mockTask2).when(mockScheduler).scheduleWithFixedDelay( - mockRunnable, asyncTaskHelper.COMMON_INITIAL_DELAY, asyncTaskHelper.COMMON_INTERVAL, TimeUnit.MILLISECONDS); + mockRunnable, asyncTaskHelper.COMMON_INITIAL_DELAY, + asyncTaskHelper.COMMON_INTERVAL, TimeUnit.MILLISECONDS); asyncTaskHelper.scheduleAsyncTask(rpc, mockRunnable); Mockito.verify(mockTask0, times(1)).cancel(true); + Mockito.verify(mockRunnable0, times(1)).abortRunnable(rpc); Assert.assertEquals(mockTask2, asyncTaskHelper.scheduleAsyncTask(rpc, mockRunnable)); Assert.assertEquals("Should set backgroundOamTask", mockTask2, asyncTaskHelper.getCurrentAsyncTask()); } @@ -120,7 +124,8 @@ public class AsyncTaskHelperTest { Future<?> mockTask2 = mock(Future.class); asyncTaskHelper.cancelAsyncTask(mockTask2); Mockito.verify(mockTask2, times(1)).cancel(false); - Assert.assertEquals("Should not reset backgroundOamTask", mockTask, asyncTaskHelper.getCurrentAsyncTask()); + Assert.assertEquals("Should not reset backgroundOamTask", + mockTask, asyncTaskHelper.getCurrentAsyncTask()); } } |