aboutsummaryrefslogtreecommitdiffstats
path: root/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java
diff options
context:
space:
mode:
Diffstat (limited to 'appc-sdc-listener/appc-sdc-listener-bundle/src/test/java')
-rw-r--r--appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/openecomp/appc/sdc/listener/SdcCallbackTest.java192
-rw-r--r--appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/openecomp/appc/sdc/listener/SdcListenerTest.java158
-rw-r--r--appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/openecomp/appc/sdc/listener/TestRun.java41
3 files changed, 391 insertions, 0 deletions
diff --git a/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/openecomp/appc/sdc/listener/SdcCallbackTest.java b/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/openecomp/appc/sdc/listener/SdcCallbackTest.java
new file mode 100644
index 000000000..9e2a86051
--- /dev/null
+++ b/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/openecomp/appc/sdc/listener/SdcCallbackTest.java
@@ -0,0 +1,192 @@
+/*-
+ * ============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.sdc.listener;
+
+import org.junit.Before;
+import org.junit.runner.RunWith;
+import org.mockito.Matchers;
+import org.mockito.Mockito;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+import org.openecomp.appc.adapter.message.EventSender;
+import org.openecomp.appc.sdc.artifacts.helper.ArtifactStorageService;
+import org.openecomp.appc.sdc.artifacts.impl.ArtifactProcessorFactory;
+import org.openecomp.appc.sdc.artifacts.impl.ToscaCsarArtifactProcessor;
+import org.openecomp.appc.sdc.artifacts.object.SDCArtifact;
+import org.openecomp.sdc.api.IDistributionClient;
+import org.openecomp.sdc.api.consumer.IDistributionStatusMessage;
+import org.openecomp.sdc.api.consumer.INotificationCallback;
+import org.openecomp.sdc.api.notification.IArtifactInfo;
+import org.openecomp.sdc.api.notification.INotificationData;
+import org.openecomp.sdc.api.notification.IResourceInstance;
+import org.openecomp.sdc.api.results.IDistributionClientDownloadResult;
+import org.openecomp.sdc.impl.DistributionClientDownloadResultImpl;
+import org.openecomp.sdc.utils.DistributionActionResultEnum;
+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 java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.ArrayList;
+import java.util.List;
+
+@RunWith(PowerMockRunner.class)
+@PrepareForTest({IDistributionClient.class,
+ EventSender.class,
+ ArtifactStorageService.class,
+ ToscaCsarArtifactProcessor.class,
+ ArtifactProcessorFactory.class})
+public class SdcCallbackTest {
+
+ IDistributionClient client;
+ private EventSender eventSender;
+ private INotificationCallback sdcCallback;
+ private ArtifactStorageService storageService;
+ private ToscaCsarArtifactProcessor artifactProcessor;
+
+
+ @Before
+ public void setup() throws Exception {
+ client = PowerMockito.mock(IDistributionClient.class);
+ eventSender = PowerMockito.mock(EventSender.class);
+ sdcCallback = new SdcCallback(null,client);
+
+ artifactProcessor = Mockito.spy(new ToscaCsarArtifactProcessor(client,eventSender,getNotificationData(),getResources().get(0)
+ ,getServiceArtifacts().get(0),null));
+ storageService = PowerMockito.mock(ArtifactStorageService.class);
+ Whitebox.setInternalState(artifactProcessor,"artifactStorageService", storageService);
+
+ PowerMockito.doCallRealMethod().when(artifactProcessor).processArtifact((IDistributionClientDownloadResult) Matchers.anyObject());
+ PowerMockito.doCallRealMethod().when(artifactProcessor).run();
+
+
+ PowerMockito.mockStatic(ArtifactProcessorFactory.class);
+ PowerMockito.when(ArtifactProcessorFactory.getArtifactProcessor((IDistributionClient)Matchers.anyObject(), (EventSender)Matchers.anyObject(),
+ (INotificationData)Matchers.anyObject(), (IResourceInstance)Matchers.anyObject(),
+ (IArtifactInfo)Matchers.anyObject(), (URI)Matchers.anyObject())).thenReturn(artifactProcessor);
+
+ Whitebox.setInternalState(sdcCallback,"eventSender", eventSender);
+ PowerMockito.doReturn(readDownloadResult()).when(client).download((IArtifactInfo) Matchers.anyObject());
+ PowerMockito.doReturn(null).when(client).sendDownloadStatus((IDistributionStatusMessage) Matchers.anyObject());
+
+ PowerMockito.doReturn(null).when(storageService).retrieveSDCArtifact(Matchers.anyString(),Matchers.anyString(),Matchers.anyString());
+
+ PowerMockito.doAnswer(new Answer<Object>() {
+ @Override
+ public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
+ System.out.print(invocationOnMock.getArguments()[0].toString());
+ return null;
+ }
+ }).when(storageService).storeSDCArtifact((SDCArtifact)Matchers.anyObject());
+ }
+
+ private IDistributionClientDownloadResult readDownloadResult() throws IOException, URISyntaxException {
+ DistributionClientDownloadResultImpl downloadResult = new DistributionClientDownloadResultImpl(DistributionActionResultEnum.SUCCESS,"Download success");
+ File file = new File(this.getClass().getResource("/csar/service-ServiceAppc-csar.csar").toURI());
+
+ byte[] bFile = new byte[(int) file.length()];
+ FileInputStream fileInputStream = new FileInputStream(file);
+ fileInputStream.read(bFile);
+ fileInputStream.close();
+
+ downloadResult.setArtifactPayload(bFile);
+ return downloadResult;
+ }
+
+
+// @Test
+ public void testSDCListener() throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException {
+
+
+ INotificationData notificationData = getNotificationData();
+ sdcCallback.activateCallback(notificationData);
+
+// pause();
+ }
+
+// private void pause(){
+// try {
+// Thread.sleep(50000000);
+// } catch (InterruptedException e) {
+// e.printStackTrace();
+// }
+// }
+
+ private INotificationData getNotificationData() throws ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException {
+
+ INotificationData notificationData = (INotificationData)getObject("org.openecomp.sdc.impl.NotificationDataImpl");
+
+ List<IArtifactInfo> serviceArtifacts = getServiceArtifacts();
+
+ invokeMethod(notificationData, "setServiceArtifacts", serviceArtifacts);
+ return notificationData;
+ }
+
+ private List<IResourceInstance> getResources() throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException {
+ List<IResourceInstance> resources = new ArrayList<>();
+ IResourceInstance resource = (IResourceInstance)getObject("org.openecomp.sdc.impl.JsonContainerResourceInstance");
+
+ List<IArtifactInfo> serviceArtifacts = getServiceArtifacts();
+ invokeMethod(resource,"setArtifacts",serviceArtifacts);
+ invokeMethod(resource,"setResourceName","Vnf");
+ invokeMethod(resource,"setResourceVersion","1.0");
+
+ resources.add(resource);
+ return resources;
+ }
+
+ private void invokeMethod(Object object, String methodName,Object... arguments) throws IllegalAccessException, InvocationTargetException {
+ Method[] methods = object.getClass().getDeclaredMethods();
+ for(Method method:methods){
+ if(methodName.equalsIgnoreCase(method.getName())){
+ method.setAccessible(true);
+ method.invoke(object,arguments);
+ }
+ }
+ }
+
+ private Object getObject(String fqcn) throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException {
+ Constructor constructor = Class.forName(fqcn).getDeclaredConstructors()[0];
+ constructor.setAccessible(true);
+ return constructor.newInstance();
+ }
+
+ private List<IArtifactInfo> getServiceArtifacts() throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException {
+ List<IArtifactInfo> serviceArtifacts = new ArrayList<>();
+ IArtifactInfo artifactInfo = (IArtifactInfo)getObject("org.openecomp.sdc.impl.ArtifactInfoImpl");
+ invokeMethod(artifactInfo,"setArtifactType","TOSCA_CSAR");
+ invokeMethod(artifactInfo,"setArtifactUUID","abcd-efgh-ijkl");
+ serviceArtifacts.add(artifactInfo);
+ return serviceArtifacts;
+ }
+}
diff --git a/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/openecomp/appc/sdc/listener/SdcListenerTest.java b/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/openecomp/appc/sdc/listener/SdcListenerTest.java
new file mode 100644
index 000000000..17e286bc5
--- /dev/null
+++ b/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/openecomp/appc/sdc/listener/SdcListenerTest.java
@@ -0,0 +1,158 @@
+/*-
+ * ============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.sdc.listener;
+
+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.openecomp.sdc.api.IDistributionClient;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+import org.powermock.reflect.Whitebox;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.times;
+
+@RunWith(PowerMockRunner.class)
+@PrepareForTest(Thread.class)
+public class SdcListenerTest {
+ private SdcListener sdcListener;
+ private EELFLogger mockLogger = mock(EELFLogger.class);
+
+ @Before
+ public void setUp() throws Exception {
+ sdcListener = new SdcListener();
+
+ // to avoid operation on logger fail, mock up the logger
+ Whitebox.setInternalState(sdcListener, "logger", mockLogger);
+ }
+
+ @Test
+ public void testStart() throws Exception {
+ sdcListener.start();
+ Assert.assertTrue("Should created startThread",
+ Whitebox.getInternalState(sdcListener, "startThread") != null);
+ }
+
+ @Test
+ public void testStop() throws Exception {
+ // test interrupt thread and other null case
+ MockThread mockThread = spy(new MockThread());
+ mockThread.setNewState(Thread.State.TIMED_WAITING);
+ Whitebox.setInternalState(sdcListener, "startThread", mockThread);
+
+ sdcListener.stop();
+ Mockito.verify(mockThread, times(1)).interrupt();
+ Assert.assertTrue("Should reset startThread",
+ Whitebox.getInternalState(sdcListener, "startThread") == null);
+
+ // test other non-null case and thread null case
+ IDistributionClient mockClient = mock(IDistributionClient.class);
+ Whitebox.setInternalState(sdcListener, "client", mockClient);
+ SdcCallback mockCallback = mock(SdcCallback.class);
+ Whitebox.setInternalState(sdcListener, "callback", mockCallback);
+ CountDownLatch mockLatch = mock(CountDownLatch.class);
+ Whitebox.setInternalState(sdcListener, "latch", mockLatch);
+
+ sdcListener.stop();
+
+ Mockito.verify(mockLatch, times(1)).await(10, TimeUnit.SECONDS);
+ Mockito.verify(mockCallback, times(1)).stop();
+ Mockito.verify(mockClient, times(1)).stop();
+ Assert.assertTrue("Should reset latch",
+ Whitebox.getInternalState(sdcListener, "latch") == null);
+ Assert.assertTrue("Should reset callback",
+ Whitebox.getInternalState(sdcListener, "callback") == null);
+ Assert.assertTrue("Should reset client",
+ Whitebox.getInternalState(sdcListener, "client") == null);
+ }
+
+ @Test
+ public void testStopStartThread() throws Exception {
+ // null case
+ sdcListener.stopStartThread(123);
+ Mockito.verify(mockLogger, times(0)).debug(String.valueOf(any()));
+
+ MockThread mockThread = spy(new MockThread());
+
+ // thread terminated case
+ Whitebox.setInternalState(sdcListener, "startThread", mockThread);
+ mockThread.setNewState(Thread.State.TERMINATED);
+ sdcListener.stopStartThread(123);
+ Mockito.verify(mockThread, times(0)).interrupt();
+ Mockito.verify(mockLogger, times(1)).debug(String.valueOf(any()));
+ Assert.assertTrue("Should reset startThread",
+ Whitebox.getInternalState(sdcListener, "startThread") == null);
+
+ // thread not termianted case
+ int timesCallThread = 0;
+ int timesCallLogger = 1;
+ for(Thread.State state : Thread.State.values()) {
+ if (state == Thread.State.TERMINATED) {
+ continue;
+ }
+ Whitebox.setInternalState(sdcListener, "startThread", mockThread);
+ mockThread.setNewState(state);
+ sdcListener.stopStartThread(123);
+ Mockito.verify(mockThread, times(++ timesCallThread)).interrupt();
+ Mockito.verify(mockLogger, times(timesCallLogger += 2)).debug(String.valueOf(any()));
+ Assert.assertTrue("Should reset startThread",
+ Whitebox.getInternalState(sdcListener, "startThread") == null);
+ }
+ }
+
+ /*
+ * I have used the following PowerMockito (due to Thread.getName() is a final method)
+ * try to mock up the thread behavior. But the mock Thread.getName() always returns null
+ * which works in intelliJ Junit test, but not Jenkins build:
+ * Thread mockThread = PowerMockito.mock(Thread.class);
+ * PowerMockito.doReturn(Thread.State.TERMINATED).when(mockThread).getState();
+ * PowerMockito.doReturn("testing").when(mockThread).getName();
+ * Hence, here goes the MockThread class to override Thread to my expected behavior.
+ */
+ class MockThread extends Thread {
+ private State state;
+
+ private MockThread() {
+ super.setName("testing");
+ }
+
+ void setNewState(State newState) {
+ state = newState;
+ }
+
+ @Override
+ public State getState() {
+ return state;
+ }
+ }
+} \ No newline at end of file
diff --git a/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/openecomp/appc/sdc/listener/TestRun.java b/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/openecomp/appc/sdc/listener/TestRun.java
new file mode 100644
index 000000000..84337e471
--- /dev/null
+++ b/appc-sdc-listener/appc-sdc-listener-bundle/src/test/java/org/openecomp/appc/sdc/listener/TestRun.java
@@ -0,0 +1,41 @@
+/*-
+ * ============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.sdc.listener;
+
+import org.junit.Test;
+
+public class TestRun {
+
+ @Test
+ public void testRunning() throws Exception {
+ /*
+ AppcSdcListenerActivator a = new AppcSdcListenerActivator();
+ a.start(null);
+ Thread.sleep(10000);
+ a.stop(null);
+ */
+ }
+
+}