2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Modifications Copyright (c) 2019 Samsung
9 * ================================================================================
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ============LICENSE_END============================================
22 * ===================================================================
26 package org.onap.clamp.clds.it.sdc.controller;
28 import static org.assertj.core.api.Assertions.assertThat;
29 import static org.mockito.Mockito.mock;
31 import java.lang.reflect.Constructor;
32 import java.lang.reflect.InvocationTargetException;
33 import java.lang.reflect.Method;
34 import java.util.ArrayList;
35 import java.util.List;
37 import org.assertj.core.api.Assertions;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.Mockito;
42 import org.onap.clamp.clds.config.ClampProperties;
43 import org.onap.clamp.clds.config.sdc.SdcSingleControllerConfigurationTest;
44 import org.onap.clamp.clds.exception.sdc.controller.SdcControllerException;
45 import org.onap.clamp.clds.sdc.controller.SdcSingleController;
46 import org.onap.clamp.clds.sdc.controller.SdcSingleControllerStatus;
47 import org.onap.clamp.clds.sdc.controller.installer.CsarHandler;
48 import org.onap.clamp.loop.CsarInstaller;
49 import org.onap.sdc.api.notification.IArtifactInfo;
50 import org.onap.sdc.api.notification.INotificationData;
51 import org.onap.sdc.api.notification.IResourceInstance;
53 import org.slf4j.spi.MDCAdapter;
54 import org.springframework.beans.factory.annotation.Autowired;
55 import org.springframework.boot.test.context.SpringBootTest;
56 import org.springframework.test.context.ActiveProfiles;
57 import org.springframework.test.context.junit4.SpringRunner;
59 @RunWith(SpringRunner.class)
61 @ActiveProfiles(profiles = "clamp-default,clamp-default-user,clamp-sdc-controller")
62 public class SdcSingleControllerItCase {
64 private static final String CSAR_ARTIFACT_NAME = "testArtifact.csar";
65 private static final String SERVICE_UUID = "serviceUUID";
66 private static final String RESOURCE1_UUID = "resource1UUID";
67 private static final String RESOURCE1_INSTANCE_NAME = "sim-1802 0";
70 private ClampProperties clampProp;
72 private SdcSingleController sdcSingleController;
74 private CsarInstaller csarInstaller = mock(CsarInstaller.class);
76 private INotificationData buildFakeSdcNotification() {
77 // BUild what is needed for CSAR
78 IArtifactInfo serviceArtifact = mock(IArtifactInfo.class);
79 Mockito.when(serviceArtifact.getArtifactType()).thenReturn(CsarHandler.CSAR_TYPE);
80 Mockito.when(serviceArtifact.getArtifactName()).thenReturn(CSAR_ARTIFACT_NAME);
81 List<IArtifactInfo> servicesList = new ArrayList<>();
82 servicesList.add(serviceArtifact);
83 INotificationData notifData = mock(INotificationData.class);
84 Mockito.when(notifData.getServiceArtifacts()).thenReturn(servicesList);
85 // Build what is needed for UUID
86 Mockito.when(notifData.getServiceInvariantUUID()).thenReturn(SERVICE_UUID);
87 // Build fake resource with one artifact BLUEPRINT
88 IResourceInstance resource1 = mock(IResourceInstance.class);
89 Mockito.when(resource1.getResourceType()).thenReturn("VF");
90 Mockito.when(resource1.getResourceInvariantUUID()).thenReturn(RESOURCE1_UUID);
91 Mockito.when(resource1.getResourceInstanceName()).thenReturn(RESOURCE1_INSTANCE_NAME);
92 // Create a fake artifact for resource
93 IArtifactInfo blueprintArtifact = mock(IArtifactInfo.class);
94 Mockito.when(blueprintArtifact.getArtifactType()).thenReturn(CsarHandler.BLUEPRINT_TYPE);
95 List<IArtifactInfo> artifactsListForResource = new ArrayList<>();
96 artifactsListForResource.add(blueprintArtifact);
97 Mockito.when(resource1.getArtifacts()).thenReturn(artifactsListForResource);
98 List<IResourceInstance> resourcesList = new ArrayList<>();
99 resourcesList.add(resource1);
100 Mockito.when(notifData.getResources()).thenReturn(resourcesList);
105 * Initialization method.
109 sdcSingleController = new SdcSingleController(clampProp, csarInstaller, SdcSingleControllerConfigurationTest
110 .loadControllerConfiguration("clds/sdc-controller-config-TLS.json", "sdc-controller1"), null) {
115 public void testTreatNotification() {
117 sdcSingleController.treatNotification(buildFakeSdcNotification());
119 Assertions.assertThat(sdcSingleController.getNbOfNotificationsOngoing()).isEqualTo(0);
123 public void testCloseSdc() throws SdcControllerException {
125 sdcSingleController.closeSdc();
127 assertThat(sdcSingleController.getControllerStatus()).isEqualTo(SdcSingleControllerStatus.STOPPED);
131 public void testActivateCallback() throws InvocationTargetException, IllegalAccessException, NoSuchMethodException,
132 InstantiationException, ClassNotFoundException {
135 Class<?> innerClass = Class
136 .forName("org.onap.clamp.clds.sdc.controller.SdcSingleController$SdcNotificationCallBack");
137 Constructor<?> constructor = innerClass.getDeclaredConstructor(SdcSingleController.class,
138 SdcSingleController.class);
139 constructor.setAccessible(true);
140 Object child = constructor.newInstance(sdcSingleController, sdcSingleController);
141 Method method = child.getClass().getDeclaredMethod("activateCallback", INotificationData.class);
142 method.setAccessible(true);
143 MDCAdapter mdcAdapter = MDC.getMDCAdapter();
145 method.invoke(child, buildFakeSdcNotification());
147 assertThat(mdcAdapter.get("ResponseCode")).isEqualTo("0");
148 assertThat(mdcAdapter.get("StatusCode")).isEqualTo("COMPLETE");
149 assertThat(mdcAdapter.get("ResponseDescription"))
150 .isEqualTo("SDC Notification received and processed successfully");
151 assertThat(mdcAdapter.get("ClassName")).isEqualTo(child.getClass().getName());