2201d1a2e03f0a5394e29d50cdb51f5bf2e33791
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2018, 2021 AT&T Intellectual Property. All rights
6  *                             reserved.
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
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
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  * ===================================================================
23  *
24  */
25
26 package org.onap.policy.clamp.clds.it.sdc.controller;
27
28 import static org.assertj.core.api.Assertions.assertThat;
29 import static org.mockito.Mockito.mock;
30
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;
36 import org.assertj.core.api.Assertions;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mockito;
41 import org.onap.policy.clamp.clds.config.ClampProperties;
42 import org.onap.policy.clamp.clds.config.sdc.SdcSingleControllerConfigurationTest;
43 import org.onap.policy.clamp.clds.exception.sdc.controller.SdcControllerException;
44 import org.onap.policy.clamp.clds.sdc.controller.SdcSingleController;
45 import org.onap.policy.clamp.clds.sdc.controller.SdcSingleControllerStatus;
46 import org.onap.policy.clamp.clds.sdc.controller.installer.CsarHandler;
47 import org.onap.policy.clamp.loop.CsarInstaller;
48 import org.onap.sdc.api.notification.IArtifactInfo;
49 import org.onap.sdc.api.notification.INotificationData;
50 import org.onap.sdc.api.notification.IResourceInstance;
51 import org.slf4j.MDC;
52 import org.slf4j.spi.MDCAdapter;
53 import org.springframework.beans.factory.annotation.Autowired;
54 import org.springframework.boot.test.context.SpringBootTest;
55 import org.springframework.test.context.ActiveProfiles;
56 import org.springframework.test.context.junit4.SpringRunner;
57
58 @RunWith(SpringRunner.class)
59 @SpringBootTest
60 @ActiveProfiles({"clamp-default", "clamp-default-user", "clamp-sdc-controller"})
61 public class SdcSingleControllerItTestCase {
62
63     private static final String CSAR_ARTIFACT_NAME = "testArtifact.csar";
64     private static final String SERVICE_UUID = "serviceUUID";
65     private static final String RESOURCE1_UUID = "resource1UUID";
66     private static final String RESOURCE1_INSTANCE_NAME = "sim-1802 0";
67
68     @Autowired
69     private ClampProperties clampProp;
70
71     private SdcSingleController sdcSingleController;
72
73     private CsarInstaller csarInstaller = mock(CsarInstaller.class);
74
75     private INotificationData buildFakeSdcNotification() {
76         // BUild what is needed for CSAR
77         IArtifactInfo serviceArtifact = mock(IArtifactInfo.class);
78         Mockito.when(serviceArtifact.getArtifactType()).thenReturn(CsarHandler.CSAR_TYPE);
79         Mockito.when(serviceArtifact.getArtifactName()).thenReturn(CSAR_ARTIFACT_NAME);
80         List<IArtifactInfo> servicesList = new ArrayList<>();
81         servicesList.add(serviceArtifact);
82         INotificationData notifData = mock(INotificationData.class);
83         Mockito.when(notifData.getServiceArtifacts()).thenReturn(servicesList);
84         // Build what is needed for UUID
85         Mockito.when(notifData.getServiceInvariantUUID()).thenReturn(SERVICE_UUID);
86         // Build fake resource with one artifact BLUEPRINT
87         IResourceInstance resource1 = mock(IResourceInstance.class);
88         Mockito.when(resource1.getResourceType()).thenReturn("VF");
89         Mockito.when(resource1.getResourceInvariantUUID()).thenReturn(RESOURCE1_UUID);
90         Mockito.when(resource1.getResourceInstanceName()).thenReturn(RESOURCE1_INSTANCE_NAME);
91         // Create a fake artifact for resource
92         IArtifactInfo blueprintArtifact = mock(IArtifactInfo.class);
93         Mockito.when(blueprintArtifact.getArtifactType()).thenReturn(CsarHandler.BLUEPRINT_TYPE);
94         List<IArtifactInfo> artifactsListForResource = new ArrayList<>();
95         artifactsListForResource.add(blueprintArtifact);
96         Mockito.when(resource1.getArtifacts()).thenReturn(artifactsListForResource);
97         List<IResourceInstance> resourcesList = new ArrayList<>();
98         resourcesList.add(resource1);
99         Mockito.when(notifData.getResources()).thenReturn(resourcesList);
100         return notifData;
101     }
102
103     /**
104      * Initialization method.
105      */
106     @Before
107     public void init() {
108         sdcSingleController = new SdcSingleController(clampProp, csarInstaller, SdcSingleControllerConfigurationTest
109                 .loadControllerConfiguration("clds/sdc-controller-config-TLS.json", "sdc-controller1"), null) {
110         };
111     }
112
113     @Test
114     public void testTreatNotification() {
115         // when
116         sdcSingleController.treatNotification(buildFakeSdcNotification());
117         // then
118         Assertions.assertThat(sdcSingleController.getNbOfNotificationsOngoing()).isZero();
119     }
120
121     @Test
122     public void testCloseSdc() throws SdcControllerException {
123         // when
124         sdcSingleController.closeSdc();
125         // then
126         assertThat(sdcSingleController.getControllerStatus()).isEqualTo(SdcSingleControllerStatus.STOPPED);
127     }
128
129     @Test
130     public void testActivateCallback() throws InvocationTargetException, IllegalAccessException, NoSuchMethodException,
131             InstantiationException, ClassNotFoundException {
132         // given
133
134         Class<?> innerClass = Class
135                 .forName("org.onap.policy.clamp.clds.sdc.controller.SdcSingleController$SdcNotificationCallBack");
136         Constructor<?> constructor = innerClass.getDeclaredConstructor(SdcSingleController.class,
137                 SdcSingleController.class);
138         constructor.setAccessible(true);
139         Object child = constructor.newInstance(sdcSingleController, sdcSingleController);
140         Method method = child.getClass().getDeclaredMethod("activateCallback", INotificationData.class);
141         method.setAccessible(true);
142         MDCAdapter mdcAdapter = MDC.getMDCAdapter();
143         // when
144         method.invoke(child, buildFakeSdcNotification());
145         // then
146         assertThat(mdcAdapter.get("ResponseCode")).isEqualTo("0");
147         assertThat(mdcAdapter.get("StatusCode")).isEqualTo("COMPLETE");
148         assertThat(mdcAdapter.get("ResponseDescription"))
149                 .isEqualTo("SDC Notification received and processed successfully");
150         assertThat(mdcAdapter.get("ClassName")).isEqualTo(child.getClass().getName());
151     }
152 }