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;
30 import java.lang.reflect.Constructor;
31 import java.lang.reflect.InvocationTargetException;
32 import java.lang.reflect.Method;
33 import java.util.ArrayList;
34 import java.util.List;
35 import org.assertj.core.api.Assertions;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.Mockito;
40 import org.onap.clamp.clds.config.ClampProperties;
41 import org.onap.clamp.clds.config.sdc.SdcSingleControllerConfigurationTest;
42 import org.onap.clamp.clds.exception.sdc.controller.SdcControllerException;
43 import org.onap.clamp.clds.sdc.controller.SdcSingleController;
44 import org.onap.clamp.clds.sdc.controller.SdcSingleControllerStatus;
45 import org.onap.clamp.clds.sdc.controller.installer.CsarHandler;
46 import org.onap.clamp.clds.sdc.controller.installer.CsarInstaller;
47 import org.onap.sdc.api.notification.IArtifactInfo;
48 import org.onap.sdc.api.notification.INotificationData;
49 import org.onap.sdc.api.notification.IResourceInstance;
51 import org.slf4j.spi.MDCAdapter;
52 import org.springframework.beans.factory.annotation.Autowired;
53 import org.springframework.boot.test.context.SpringBootTest;
54 import org.springframework.test.context.ActiveProfiles;
55 import org.springframework.test.context.junit4.SpringRunner;
57 @RunWith(SpringRunner.class)
59 @ActiveProfiles(profiles = "clamp-default,clamp-default-user,clamp-sdc-controller")
60 public class SdcSingleControllerItCase {
62 private static final String CSAR_ARTIFACT_NAME = "testArtifact.csar";
63 private static final String SERVICE_UUID = "serviceUUID";
64 private static final String RESOURCE1_UUID = "resource1UUID";
65 private static final String RESOURCE1_INSTANCE_NAME = "sim-1802 0";
68 private ClampProperties clampProp;
70 private SdcSingleController sdcSingleController;
72 private CsarInstaller csarInstaller = mock(CsarInstaller.class);
74 private INotificationData buildFakeSdcNotification() {
75 // BUild what is needed for CSAR
76 IArtifactInfo serviceArtifact = mock(IArtifactInfo.class);
77 Mockito.when(serviceArtifact.getArtifactType()).thenReturn(CsarHandler.CSAR_TYPE);
78 Mockito.when(serviceArtifact.getArtifactName()).thenReturn(CSAR_ARTIFACT_NAME);
79 List<IArtifactInfo> servicesList = new ArrayList<>();
80 servicesList.add(serviceArtifact);
81 INotificationData notifData = mock(INotificationData.class);
82 Mockito.when(notifData.getServiceArtifacts()).thenReturn(servicesList);
83 // Build what is needed for UUID
84 Mockito.when(notifData.getServiceInvariantUUID()).thenReturn(SERVICE_UUID);
85 // Build fake resource with one artifact BLUEPRINT
86 IResourceInstance resource1 = mock(IResourceInstance.class);
87 Mockito.when(resource1.getResourceType()).thenReturn("VF");
88 Mockito.when(resource1.getResourceInvariantUUID()).thenReturn(RESOURCE1_UUID);
89 Mockito.when(resource1.getResourceInstanceName()).thenReturn(RESOURCE1_INSTANCE_NAME);
90 // Create a fake artifact for resource
91 IArtifactInfo blueprintArtifact = mock(IArtifactInfo.class);
92 Mockito.when(blueprintArtifact.getArtifactType()).thenReturn(CsarHandler.BLUEPRINT_TYPE);
93 List<IArtifactInfo> artifactsListForResource = new ArrayList<>();
94 artifactsListForResource.add(blueprintArtifact);
95 Mockito.when(resource1.getArtifacts()).thenReturn(artifactsListForResource);
96 List<IResourceInstance> resourcesList = new ArrayList<>();
97 resourcesList.add(resource1);
98 Mockito.when(notifData.getResources()).thenReturn(resourcesList);
103 * Initialization method.
107 sdcSingleController = new SdcSingleController(clampProp, csarInstaller,
108 SdcSingleControllerConfigurationTest.loadControllerConfiguration("clds/sdc-controller-config-TLS.json",
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, InstantiationException, ClassNotFoundException {
133 MDCAdapter mdcAdapter = MDC.getMDCAdapter();
134 Class<?> innerClass = Class.forName("org.onap.clamp.clds.sdc.controller.SdcSingleController$SdcNotificationCallBack");
135 Constructor<?> constructor = innerClass.getDeclaredConstructor(SdcSingleController.class, SdcSingleController.class);
136 constructor.setAccessible(true);
137 Object child = constructor.newInstance(sdcSingleController,sdcSingleController);
138 Method method = child.getClass().getDeclaredMethod("activateCallback",INotificationData.class);
139 method.setAccessible(true);
141 method.invoke(child,buildFakeSdcNotification());
143 assertThat(mdcAdapter.get("ResponseCode")).isEqualTo("0");
144 assertThat(mdcAdapter.get("StatusCode")).isEqualTo("COMPLETE");
145 assertThat(mdcAdapter.get("ResponseDescription")).isEqualTo("SDC Notification received and processed successfully");
146 assertThat(mdcAdapter.get("ClassName")).isEqualTo(child.getClass().getName());