diff options
Diffstat (limited to 'appc-oam/appc-oam-bundle/src/test')
3 files changed, 265 insertions, 31 deletions
diff --git a/appc-oam/appc-oam-bundle/src/test/java/org/onap/appc/oam/AppcOamTest.java b/appc-oam/appc-oam-bundle/src/test/java/org/onap/appc/oam/AppcOamTest.java index 3bb805135..5f2ae228b 100644 --- a/appc-oam/appc-oam-bundle/src/test/java/org/onap/appc/oam/AppcOamTest.java +++ b/appc-oam/appc-oam-bundle/src/test/java/org/onap/appc/oam/AppcOamTest.java @@ -5,6 +5,8 @@ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Copyright (C) 2017 Amdocs + * ================================================================================ + * Modifications (C) 2018 Ericsson * ============================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,13 +25,35 @@ package org.onap.appc.oam; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.powermock.api.mockito.PowerMockito.spy; +import java.util.Collection; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; import org.mockito.Mockito; +import org.onap.appc.exceptions.APPCException; +import org.onap.appc.i18n.Msg; +import org.onap.appc.metricservice.MetricRegistry; +import org.onap.appc.metricservice.MetricService; +import org.onap.appc.metricservice.metric.Metric; +import org.onap.appc.metricservice.metric.MetricType; +import org.onap.appc.metricservice.metric.impl.DefaultPrimitiveCounter; +import org.onap.appc.metricservice.metric.impl.DispatchingFuntionMetricImpl; +import org.onap.appc.oam.processor.OamMmodeProcessor; +import org.onap.appc.oam.processor.OamRestartProcessor; +import org.onap.appc.oam.processor.OamStartProcessor; +import org.onap.appc.oam.processor.OamStopProcessor; +import org.onap.appc.oam.util.AsyncTaskHelper; +import org.onap.appc.oam.util.ConfigurationHelper; +import org.onap.appc.oam.util.OperationHelper; +import org.onap.appc.oam.util.StateHelper; import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.AppcState; import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.GetAppcStateOutput; +import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.GetMetricsOutput; import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.MaintenanceModeInput; import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.MaintenanceModeOutput; import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.RestartInput; @@ -40,27 +64,14 @@ import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.StopInput; import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.StopOutput; import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.common.header.CommonHeader; import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.status.Status; +import org.opendaylight.yangtools.yang.common.RpcError; import org.opendaylight.yangtools.yang.common.RpcResult; -import org.onap.appc.oam.processor.OamMmodeProcessor; -import org.onap.appc.oam.processor.OamRestartProcessor; -import org.onap.appc.oam.processor.OamStartProcessor; -import org.onap.appc.oam.processor.OamStopProcessor; -import org.onap.appc.oam.util.OperationHelper; -import org.onap.appc.oam.util.StateHelper; -import org.osgi.framework.FrameworkUtil; -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 com.att.aft.dme2.internal.google.common.collect.Iterables; +import com.att.eelf.configuration.EELFLogger; +import com.google.common.collect.ImmutableMap; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; -import static org.mockito.Mockito.mock; -import static org.powermock.api.mockito.PowerMockito.spy; - -@RunWith(PowerMockRunner.class) -@PrepareForTest({AppcOam.class, FrameworkUtil.class, Executors.class}) public class AppcOamTest { private AppcOam appcOam; @@ -81,8 +92,9 @@ public class AppcOamTest { public void testMaintenanceMode() throws Exception { // mock processor creation OamMmodeProcessor mockProcessor = mock(OamMmodeProcessor.class); - PowerMockito.mockStatic(OamMmodeProcessor.class); - PowerMockito.whenNew(OamMmodeProcessor.class).withAnyArguments().thenReturn(mockProcessor); + Mockito.doReturn(mockProcessor).when(appcOam).getOamMmodeProcessor(Mockito.any(EELFLogger.class), + Mockito.any(ConfigurationHelper.class), Mockito.any(StateHelper.class), + Mockito.any(AsyncTaskHelper.class), Mockito.any(OperationHelper.class)); // mock input MaintenanceModeInput mockInput = mock(MaintenanceModeInput.class); Mockito.doReturn(mockCommonHeader).when(mockInput).getCommonHeader(); @@ -100,8 +112,9 @@ public class AppcOamTest { public void testStart() throws Exception { // mock processor creation OamStartProcessor mockProcessor = mock(OamStartProcessor.class); - PowerMockito.mockStatic(OamStartProcessor.class); - PowerMockito.whenNew(OamStartProcessor.class).withAnyArguments().thenReturn(mockProcessor); + Mockito.doReturn(mockProcessor).when(appcOam).getOamStartProcessor(Mockito.any(EELFLogger.class), + Mockito.any(ConfigurationHelper.class), Mockito.any(StateHelper.class), + Mockito.any(AsyncTaskHelper.class), Mockito.any(OperationHelper.class)); // mock input StartInput mockInput = mock(StartInput.class); Mockito.doReturn(mockCommonHeader).when(mockInput).getCommonHeader(); @@ -119,8 +132,10 @@ public class AppcOamTest { public void testStop() throws Exception { // mock processor creation OamStopProcessor mockProcessor = mock(OamStopProcessor.class); - PowerMockito.mockStatic(OamStopProcessor.class); - PowerMockito.whenNew(OamStopProcessor.class).withAnyArguments().thenReturn(mockProcessor); + //Mockito.doNothing().when(mockProcessor).setInitialLogProperties(); + Mockito.doReturn(mockProcessor).when(appcOam).getOamStopProcessor(Mockito.any(EELFLogger.class), + Mockito.any(ConfigurationHelper.class), Mockito.any(StateHelper.class), + Mockito.any(AsyncTaskHelper.class), Mockito.any(OperationHelper.class)); // mock input StopInput mockInput = mock(StopInput.class); Mockito.doReturn(mockCommonHeader).when(mockInput).getCommonHeader(); @@ -138,8 +153,9 @@ public class AppcOamTest { public void testRestart() throws Exception { // mock processor creation OamRestartProcessor mockProcessor = mock(OamRestartProcessor.class); - PowerMockito.mockStatic(OamRestartProcessor.class); - PowerMockito.whenNew(OamRestartProcessor.class).withAnyArguments().thenReturn(mockProcessor); + Mockito.doReturn(mockProcessor).when(appcOam).getOamRestartProcessor(Mockito.any(EELFLogger.class), + Mockito.any(ConfigurationHelper.class), Mockito.any(StateHelper.class), + Mockito.any(AsyncTaskHelper.class), Mockito.any(OperationHelper.class)); // mock input RestartInput mockInput = mock(RestartInput.class); Mockito.doReturn(mockCommonHeader).when(mockInput).getCommonHeader(); @@ -162,4 +178,52 @@ public class AppcOamTest { Assert.assertEquals("Should return the same state", appcState, state.get().getResult().getState()); } + + @Test + public void testGetMetricsMetricDisabled() throws InterruptedException, ExecutionException { + Whitebox.setInternalState(appcOam, "isMetricEnabled", false); + Future<RpcResult<GetMetricsOutput>> result = appcOam.getMetrics(); + assertEquals("Metric Service not enabled", Iterables.get(result.get().getErrors(), 0).getMessage()); + } + + @Test + public void testGetMetricsNoMetricsService() throws InterruptedException, ExecutionException, APPCException { + Whitebox.setInternalState(appcOam, "isMetricEnabled", true); + Mockito.doThrow(new APPCException()).when(mockOperationHelper).getService(MetricService.class); + Future<RpcResult<GetMetricsOutput>> result = appcOam.getMetrics(); + assertEquals("Metric Service not found", Iterables.get(result.get().getErrors(), 0).getMessage()); + } + + @Test + public void testGetMetricsNoMetrics() throws InterruptedException, ExecutionException, APPCException { + Whitebox.setInternalState(appcOam, "isMetricEnabled", true); + MetricService mockMetricService = mock(MetricService.class); + Mockito.doReturn(mockMetricService).when(mockOperationHelper).getService(MetricService.class); + Future<RpcResult<GetMetricsOutput>> result = appcOam.getMetrics(); + assertEquals("No metrics Registered", Iterables.get(result.get().getErrors(), 0).getMessage()); + } + + @Test + public void testGetMetricsWithMetricRegistry() throws InterruptedException, ExecutionException, APPCException { + Whitebox.setInternalState(appcOam, "isMetricEnabled", true); + MetricService mockMetricService = mock(MetricService.class); + MetricRegistry mockMetricRegistry = mock(MetricRegistry.class); + Mockito.doReturn(mockMetricService).when(mockOperationHelper).getService(MetricService.class); + Mockito.doReturn(ImmutableMap.of("TEST REGISTRY NAME", mockMetricRegistry)).when(mockMetricService).getAllRegistry(); + Metric metric = new DispatchingFuntionMetricImpl("TEST METRIC NAME", MetricType.COUNTER, 0, 0); + Mockito.doReturn(new Metric[] {metric}).when(mockMetricRegistry).metrics(); + Future<RpcResult<GetMetricsOutput>> result = appcOam.getMetrics(); + assertEquals(1, result.get().getResult().getMetrics().size()); + } + + @Test + public void testClose() throws Exception { + ConfigurationHelper mockConfigurationHelper = mock(ConfigurationHelper.class); + Mockito.doReturn("TEST APP NAME").when(mockConfigurationHelper).getAppcName(); + Whitebox.setInternalState(appcOam, "configurationHelper", mockConfigurationHelper); + EELFLogger mockLogger = Mockito.mock(EELFLogger.class); + Whitebox.setInternalState(appcOam, "logger", mockLogger); + appcOam.close(); + Mockito.verify(mockLogger).info(Msg.COMPONENT_TERMINATED, "TEST APP NAME", "oam"); + } } diff --git a/appc-oam/appc-oam-bundle/src/test/java/org/onap/appc/oam/messageadapter/ConverterTest.java b/appc-oam/appc-oam-bundle/src/test/java/org/onap/appc/oam/messageadapter/ConverterTest.java new file mode 100644 index 000000000..33e628f56 --- /dev/null +++ b/appc-oam/appc-oam-bundle/src/test/java/org/onap/appc/oam/messageadapter/ConverterTest.java @@ -0,0 +1,76 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2018 Ericsson + * ============================================================================= + * 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.appc.oam.messageadapter; + +import static org.junit.Assert.assertTrue; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.onap.appc.oam.AppcOam.RPC; +import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.common.header.CommonHeader; +import com.fasterxml.jackson.core.JsonProcessingException; + + + +public class ConverterTest { + + private OAMContext oamContext = Mockito.spy(new OAMContext()); + + @Before + public void setup() { + org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.status.StatusBuilder statusBuilder = + new org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.status.StatusBuilder(); + statusBuilder.setCode(1); + statusBuilder.setMessage("MESSAGE"); + oamContext.setStatus(statusBuilder.build()); + org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.common.header.CommonHeaderBuilder commonHeaderBuilder = + new org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.common.header.CommonHeaderBuilder(); + commonHeaderBuilder.setRequestId("REQUEST_ID"); + CommonHeader commonHeader = commonHeaderBuilder.build(); + oamContext.setCommonHeader(commonHeader); + } + + @Test + public void testStop() throws JsonProcessingException { + oamContext.setRpcName(RPC.stop); + assertTrue(Converter.convAsyncResponseToUebOutgoingMessageJsonString(oamContext).contains("\"rpc-name\":\"stop\"")); + } + + @Test + public void testStart() throws JsonProcessingException { + oamContext.setRpcName(RPC.start); + assertTrue(Converter.convAsyncResponseToUebOutgoingMessageJsonString(oamContext).contains("\"rpc-name\":\"start\"")); + } + + @Test + public void testRestart() throws JsonProcessingException { + oamContext.setRpcName(RPC.restart); + assertTrue(Converter.convAsyncResponseToUebOutgoingMessageJsonString(oamContext).contains("\"rpc-name\":\"restart\"")); + } + + @Test + public void testMmode() throws JsonProcessingException { + oamContext.setRpcName(RPC.maintenance_mode); + assertTrue(Converter.convAsyncResponseToUebOutgoingMessageJsonString(oamContext).contains("\"rpc-name\":\"maintenance_mode\"")); + } + +} diff --git a/appc-oam/appc-oam-bundle/src/test/java/org/onap/appc/oam/messageadapter/MessageAdapterTest.java b/appc-oam/appc-oam-bundle/src/test/java/org/onap/appc/oam/messageadapter/MessageAdapterTest.java index 83db7670a..bb73dfe8a 100644 --- a/appc-oam/appc-oam-bundle/src/test/java/org/onap/appc/oam/messageadapter/MessageAdapterTest.java +++ b/appc-oam/appc-oam-bundle/src/test/java/org/onap/appc/oam/messageadapter/MessageAdapterTest.java @@ -5,6 +5,8 @@ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Copyright (C) 2017 Amdocs + * ================================================================================ + * Modifications (C) 2018 Ericsson * ============================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,8 +31,11 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.onap.appc.adapter.message.MessageAdapterFactory; import org.onap.appc.adapter.message.Producer; - +import org.onap.appc.configuration.Configuration; import org.mockito.Mockito; +import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.common.header.CommonHeader; +import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.common.header.CommonHeaderBuilder; +import org.opendaylight.yang.gen.v1.org.onap.appc.oam.rev170303.status.Status; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; import org.osgi.framework.FrameworkUtil; @@ -39,15 +44,21 @@ 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 com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFLogger.Level; +import com.fasterxml.jackson.core.JsonProcessingException; +import org.onap.appc.configuration.ConfigurationFactory; +import org.onap.appc.oam.AppcOam.RPC; import java.util.HashSet; - +import java.util.Properties; +import java.util.Set; +import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.mock; import static org.powermock.api.mockito.PowerMockito.mockStatic; @RunWith(PowerMockRunner.class) -@PrepareForTest({MessageAdapter.class, FrameworkUtil.class}) +@PrepareForTest({FrameworkUtil.class, ConfigurationFactory.class, Converter.class}) public class MessageAdapterTest { private Producer fakeProducer; @@ -88,6 +99,8 @@ public class MessageAdapterTest { MessageAdapter maSpy = Mockito.spy(messageAdapter); Whitebox.setInternalState(maSpy, "producer", (Object) null); HashSet<String> pool = new HashSet<>(); + pool.add("NOT_HTTPS"); + pool.add("https"); Whitebox.setInternalState(maSpy, "pool", pool); // Prepare all mocks @@ -111,4 +124,85 @@ public class MessageAdapterTest { Assert.assertTrue("MessageAdapter producer does not match",mySpyProducer == fakeProducer); Mockito.verify(maSpy, Mockito.times(1)).createProducer(); } -} + + @Test + public void testUpdateProperties() { + MessageAdapter maSpy = Mockito.spy(messageAdapter); + Mockito.doNothing().when(maSpy).createProducer(); + Whitebox.setInternalState(maSpy, "isDisabled", false); + PowerMockito.mockStatic(ConfigurationFactory.class); + Configuration mockConfig = Mockito.mock(Configuration.class); + Properties properties = new Properties(); + properties.setProperty("appc.OAM.poolMembers", "hostname1,hostname2"); + Mockito.when(ConfigurationFactory.getConfiguration()).thenReturn(mockConfig); + Mockito.doReturn(properties).when(mockConfig).getProperties(); + maSpy.init(); + assertEquals(2, ((Set<String>)Whitebox.getInternalState(maSpy, "pool")).size()); + } + + @Test + public void testPost() throws JsonProcessingException { + MessageAdapter maSpy = Mockito.spy(messageAdapter); + OAMContext oamContext = new OAMContext(); + oamContext.setRpcName(RPC.maintenance_mode); + CommonHeader mockCommonHeader = Mockito.mock(CommonHeader.class); + Mockito.doReturn("TEST REQUEST ID").when(mockCommonHeader).getRequestId(); + oamContext.setCommonHeader(mockCommonHeader); + Status mockStatus = Mockito.mock(Status.class); + oamContext.setStatus(mockStatus); + EELFLogger mockLogger = Mockito.mock(EELFLogger.class); + Mockito.doReturn(true).when(mockLogger).isTraceEnabled(); + Mockito.doReturn(true).when(mockLogger).isDebugEnabled(); + Whitebox.setInternalState(maSpy, "logger", mockLogger); + PowerMockito.mockStatic(Converter.class); + Mockito.when(Converter.convAsyncResponseToUebOutgoingMessageJsonString(oamContext)).thenReturn("{cambriaPartition='MSO', rpcName='maintenance_mode'," + + " body=Body{output=MaintenanceModeOutput [_commonHeader=CommonHeader, hashCode: 14584991," + + " _status=Status, hashCode: 24801521, augmentation=[]]}}"); + Mockito.doNothing().when(maSpy).createProducer(); + maSpy.post(oamContext); + Mockito.verify(mockLogger).trace(Mockito.contains("Entering to post")); + Mockito.verify(mockLogger).trace("Exiting from post with (success = false)"); + } + + @Test + public void testPostExceptionFlow() throws JsonProcessingException { + MessageAdapter maSpy = Mockito.spy(messageAdapter); + OAMContext oamContext = new OAMContext(); + oamContext.setRpcName(RPC.maintenance_mode); + CommonHeader mockCommonHeader = Mockito.mock(CommonHeader.class); + Mockito.doReturn("TEST REQUEST ID").when(mockCommonHeader).getRequestId(); + oamContext.setCommonHeader(mockCommonHeader); + Status mockStatus = Mockito.mock(Status.class); + oamContext.setStatus(mockStatus); + EELFLogger mockLogger = Mockito.mock(EELFLogger.class); + Mockito.doReturn(false).when(mockLogger).isTraceEnabled(); + Mockito.doReturn(false).when(mockLogger).isDebugEnabled(); + Whitebox.setInternalState(maSpy, "logger", mockLogger); + PowerMockito.mockStatic(Converter.class); + Mockito.when(Converter.convAsyncResponseToUebOutgoingMessageJsonString(oamContext)).thenThrow(new JsonProcessingException("ERROR") {}); + Mockito.doNothing().when(maSpy).createProducer(); + maSpy.post(oamContext); + Mockito.verify(mockLogger).error(Mockito.contains("Error generating Json from UEB message")); + } + + @Test + public void testPostExceptionFlow2() throws JsonProcessingException { + MessageAdapter maSpy = Mockito.spy(messageAdapter); + OAMContext oamContext = new OAMContext(); + oamContext.setRpcName(RPC.maintenance_mode); + CommonHeader mockCommonHeader = Mockito.mock(CommonHeader.class); + Mockito.doReturn("TEST REQUEST ID").when(mockCommonHeader).getRequestId(); + oamContext.setCommonHeader(mockCommonHeader); + Status mockStatus = Mockito.mock(Status.class); + oamContext.setStatus(mockStatus); + EELFLogger mockLogger = Mockito.mock(EELFLogger.class); + Mockito.doReturn(false).when(mockLogger).isTraceEnabled(); + Mockito.doReturn(false).when(mockLogger).isDebugEnabled(); + Whitebox.setInternalState(maSpy, "logger", mockLogger); + PowerMockito.mockStatic(Converter.class); + Mockito.when(Converter.convAsyncResponseToUebOutgoingMessageJsonString(oamContext)).thenThrow(new RuntimeException("ERROR")); + Mockito.doNothing().when(maSpy).createProducer(); + maSpy.post(oamContext); + Mockito.verify(mockLogger).error(Mockito.contains("Error sending message to UEB ERROR"), Mockito.any(RuntimeException.class)); + } +}
\ No newline at end of file |