Fix weakness causing NPE
[clamp.git] / src / test / java / org / onap / clamp / clds / it / SdcCatalogServicesItCase.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License"); 
9  * you may not use this file except in compliance with the License. 
10  * You may obtain a copy of the License at
11  * 
12  * http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software 
15  * distributed under the License is distributed on an "AS IS" BASIS, 
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
17  * See the License for the specific language governing permissions and 
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * Modifications copyright (c) 2018 Nokia
21  * ===================================================================
22  * 
23  */
24
25 package org.onap.clamp.clds.it;
26
27 import static org.assertj.core.api.Assertions.assertThat;
28 import static org.junit.Assert.assertTrue;
29
30 import java.util.ArrayList;
31 import java.util.LinkedList;
32 import java.util.List;
33
34 import org.apache.commons.io.IOUtils;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mockito;
38 import org.onap.clamp.clds.client.req.sdc.SdcCatalogServices;
39 import org.onap.clamp.clds.config.ClampProperties;
40 import org.onap.clamp.clds.model.CldsAlarmCondition;
41 import org.onap.clamp.clds.model.CldsServiceData;
42 import org.onap.clamp.clds.model.sdc.SdcResource;
43 import org.onap.clamp.clds.model.sdc.SdcResourceBasicInfo;
44 import org.onap.clamp.clds.model.sdc.SdcServiceInfo;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.boot.test.context.SpringBootTest;
47 import org.springframework.test.context.junit4.SpringRunner;
48
49 /**
50  * Test SDC Catalog Service class by mocking the SDC answers.
51  */
52 @RunWith(SpringRunner.class)
53 @SpringBootTest
54 public class SdcCatalogServicesItCase {
55
56     @Autowired
57     private ClampProperties refProp;
58     @Autowired
59     private SdcCatalogServices sdcCatalogWired = new SdcCatalogServices();
60
61     @Test
62     public void removeDuplicateServicesTest() {
63         SdcServiceInfo service1a = new SdcServiceInfo();
64         service1a.setName("service1");
65         service1a.setVersion("1.0");
66         service1a.setInvariantUUID("invariantUUID1.0");
67         List<SdcServiceInfo> rawCldsSdcServiceList = new LinkedList<>();
68         rawCldsSdcServiceList.add(service1a);
69         rawCldsSdcServiceList.add(service1a);
70         SdcServiceInfo service1b = new SdcServiceInfo();
71         service1b.setName("service1");
72         service1b.setVersion("1.1");
73         service1b.setInvariantUUID("invariantUUID1.1");
74         rawCldsSdcServiceList.add(service1b);
75         SdcServiceInfo service1c = new SdcServiceInfo();
76         service1c.setName("service1");
77         service1c.setVersion("1.2");
78         service1c.setInvariantUUID("invariantUUID1.2");
79         rawCldsSdcServiceList.add(service1c);
80         SdcServiceInfo service2 = new SdcServiceInfo();
81         service2.setName("service2");
82         service2.setVersion("1.0");
83         service2.setInvariantUUID("invariantUUID2.0");
84         rawCldsSdcServiceList.add(service2);
85         SdcCatalogServices catalogServices = new SdcCatalogServices();
86         List<SdcServiceInfo> resultList = catalogServices.removeDuplicateServices(rawCldsSdcServiceList);
87         assertTrue(resultList.size() == 2);
88         SdcServiceInfo res1;
89         SdcServiceInfo res2;
90         if ("service1".equals(resultList.get(0).getName())) {
91             res1 = resultList.get(0);
92             res2 = resultList.get(1);
93         } else {
94             res1 = resultList.get(1);
95             res2 = resultList.get(0);
96         }
97         assertTrue("service1".equals(res1.getName()));
98         assertTrue("1.2".equals(res1.getVersion()));
99         assertTrue("service2".equals(res2.getName()));
100         assertTrue("1.0".equals(res2.getVersion()));
101     }
102
103     @Test
104     public void removeDuplicateSdcResourceInstancesTest() {
105         List<SdcResource> rawCldsSdcResourceList = new LinkedList<>();
106         SdcResource sdcResource1a = new SdcResource();
107         sdcResource1a.setResourceInstanceName("resource1");
108         sdcResource1a.setResourceVersion("1.0");
109         rawCldsSdcResourceList.add(sdcResource1a);
110         SdcResource sdcResource1b = new SdcResource();
111         sdcResource1b.setResourceInstanceName("resource1");
112         sdcResource1b.setResourceVersion("1.1");
113         rawCldsSdcResourceList.add(sdcResource1b);
114         SdcResource sdcResource1c = new SdcResource();
115         sdcResource1c.setResourceInstanceName("resource1");
116         sdcResource1c.setResourceVersion("1.2");
117         rawCldsSdcResourceList.add(sdcResource1c);
118         SdcResource sdcResource2 = new SdcResource();
119         sdcResource2.setResourceInstanceName("resource2");
120         sdcResource2.setResourceVersion("1.0");
121         rawCldsSdcResourceList.add(sdcResource2);
122         SdcCatalogServices catalogServices = new SdcCatalogServices();
123         List<SdcResource> resultList = catalogServices.removeDuplicateSdcResourceInstances(rawCldsSdcResourceList);
124         SdcResource res1;
125         SdcResource res2;
126         if ("resource1".equals(resultList.get(0).getResourceInstanceName())) {
127             res1 = resultList.get(0);
128             res2 = resultList.get(1);
129         } else {
130             res1 = resultList.get(1);
131             res2 = resultList.get(0);
132         }
133         assertTrue("resource1".equals(res1.getResourceInstanceName()));
134         assertTrue("1.2".equals(res1.getResourceVersion()));
135         assertTrue("resource2".equals(res2.getResourceInstanceName()));
136         assertTrue("1.0".equals(res2.getResourceVersion()));
137     }
138
139     @Test
140     public void removeDuplicateSdcResourceBasicInfoTest() {
141         List<SdcResourceBasicInfo> rawCldsSdcResourceList = new LinkedList<>();
142         SdcResourceBasicInfo sdcResource1a = new SdcResourceBasicInfo();
143         sdcResource1a.setName("resource1");
144         sdcResource1a.setVersion("1.0");
145         rawCldsSdcResourceList.add(sdcResource1a);
146         SdcResourceBasicInfo sdcResource1b = new SdcResourceBasicInfo();
147         sdcResource1b.setName("resource1");
148         sdcResource1b.setVersion("1.1");
149         rawCldsSdcResourceList.add(sdcResource1b);
150         SdcResourceBasicInfo sdcResource1c = new SdcResourceBasicInfo();
151         sdcResource1c.setName("resource1");
152         sdcResource1c.setVersion("1.2");
153         rawCldsSdcResourceList.add(sdcResource1c);
154         SdcResourceBasicInfo sdcResource2 = new SdcResourceBasicInfo();
155         sdcResource2.setName("resource2");
156         sdcResource2.setVersion("1.0");
157         rawCldsSdcResourceList.add(sdcResource2);
158         SdcCatalogServices catalogServices = new SdcCatalogServices();
159         List<SdcResourceBasicInfo> resultList = catalogServices
160                 .removeDuplicateSdcResourceBasicInfo(rawCldsSdcResourceList);
161         SdcResourceBasicInfo res1;
162         SdcResourceBasicInfo res2;
163         if ("resource1".equals(resultList.get(0).getName())) {
164             res1 = resultList.get(0);
165             res2 = resultList.get(1);
166         } else {
167             res1 = resultList.get(1);
168             res2 = resultList.get(0);
169         }
170         assertTrue("resource1".equals(res1.getName()));
171         assertTrue("1.2".equals(res1.getVersion()));
172         assertTrue("resource2".equals(res2.getName()));
173         assertTrue("1.0".equals(res2.getVersion()));
174     }
175
176
177     @Test
178     public void removeDuplicateSdcFunctionShouldNotReturnNull(){
179         // given
180         SdcCatalogServices catalogServices = new SdcCatalogServices();
181
182         // when
183         List<SdcResourceBasicInfo> firstResult = catalogServices
184             .removeDuplicateSdcResourceBasicInfo(null);
185         List<SdcResourceBasicInfo> secondResult = catalogServices
186             .removeDuplicateSdcResourceBasicInfo(new ArrayList<>());
187
188         // then
189         assertThat(firstResult).isEmpty();
190         assertThat(secondResult).isEmpty();
191     }
192
193     @Test
194     public void getServiceUuidFromServiceInvariantIdTest() throws Exception {
195         SdcCatalogServices spy = Mockito.spy(sdcCatalogWired);
196         Mockito.doReturn(IOUtils.toString(
197                 SdcCatalogServicesItCase.class.getResourceAsStream("/example/sdc/sdcServicesListExample.json"),
198                 "UTF-8")).when(spy).getSdcServicesInformation(null);
199         // Try the vcts4 version 1.0, this one should be replaced by 1.1 so it
200         // should not exist, returning empty string
201         String resUuidVcts4Null = spy.getServiceUuidFromServiceInvariantId("a33ed748-3477-4434-b3f3-b5560f5e7d9b");
202         assertTrue("".equals(resUuidVcts4Null));
203         // Try the vcts4 version 1.1, this one should be there as it replaces
204         // the vcts4 v1.0
205         String resUuidVcts4Latest = spy.getServiceUuidFromServiceInvariantId("a33ed748-3477-4434-b3f3-b5560f5e7d9c");
206         assertTrue("29018914-966c-442d-9d08-251b9dc45b8f".equals(resUuidVcts4Latest));
207         // Try the vcts5 version 1.0, this one should be there
208         String resUuidVcts5 = spy.getServiceUuidFromServiceInvariantId("a33ed748-3477-4434-b3f3-b5560f5e7d8c");
209         assertTrue("29018914-966c-442d-9d08-251b9dc45b7f".equals(resUuidVcts5));
210         // try one that does not exist at all
211         String resUuidUnknown = spy.getServiceUuidFromServiceInvariantId("testuuid");
212         assertTrue("".equals(resUuidUnknown));
213     }
214
215     @Test
216     public void getCldsServiceDataWithAlarmConditionsTest() throws Exception {
217         SdcCatalogServices spy = Mockito.spy(sdcCatalogWired);
218         Mockito.doReturn(IOUtils.toString(
219                 SdcCatalogServicesItCase.class.getResourceAsStream("/example/sdc/sdcServicesListExample.json"),
220                 "UTF-8")).when(spy).getSdcServicesInformation(null);
221         // This invariant uuid is the one from vcts4 v1.1
222         String serviceResourceDetailUrl = refProp.getStringValue("sdc.serviceUrl")
223                 + "/29018914-966c-442d-9d08-251b9dc45b8f/metadata";
224         Mockito.doReturn(IOUtils.toString(
225                 SdcCatalogServicesItCase.class.getResourceAsStream("/example/sdc/sdcServiceDetailsExample.json"),
226                 "UTF-8")).when(spy).getCldsServicesOrResourcesBasedOnURL(serviceResourceDetailUrl);
227         String resourceDetailUrl = refProp.getStringValue("sdc.catalog.url")
228                 + "resources/585822c7-4027-4f84-ba50-e9248606f136/metadata";
229         Mockito.doReturn(IOUtils.toString(
230                 SdcCatalogServicesItCase.class.getResourceAsStream("/example/sdc/sdcResourceDetailsExample.json"),
231                 "UTF-8")).when(spy).getCldsServicesOrResourcesBasedOnURL(resourceDetailUrl);
232         String securityRulesDetailUrl = refProp.getStringValue("sdc.catalog.url")
233                 + "resources/d57e57d2-e3c6-470d-8d16-e6ea05f536c5/metadata";
234         Mockito.doReturn(IOUtils.toString(
235                 SdcCatalogServicesItCase.class.getResourceAsStream("/example/sdc/sdcSecurityRules.json"), "UTF-8"))
236                 .when(spy).getCldsServicesOrResourcesBasedOnURL(securityRulesDetailUrl);
237         String cinderVolumeDetailUrl = refProp.getStringValue("sdc.catalog.url")
238                 + "resources/b4288e07-597a-44a2-aa98-ad36e551a39d/metadata";
239         Mockito.doReturn(IOUtils.toString(
240                 SdcCatalogServicesItCase.class.getResourceAsStream("/example/sdc/sdcCinderVolume.json"), "UTF-8"))
241                 .when(spy).getCldsServicesOrResourcesBasedOnURL(cinderVolumeDetailUrl);
242         String vfcGenericDetailUrl = refProp.getStringValue("sdc.catalog.url")
243                 + "resources/2c8f1219-8000-4001-aa13-496a0396d40f/metadata";
244         Mockito.doReturn(IOUtils.toString(
245                 SdcCatalogServicesItCase.class.getResourceAsStream("/example/sdc/sdcVFCGenericWithAlarms.json"),
246                 "UTF-8")).when(spy).getCldsServicesOrResourcesBasedOnURL(vfcGenericDetailUrl);
247         String csvAlarmsDetailUrl = refProp.getStringValue("sdc.catalog.url")
248                 + "resources/2c8f1219-8000-4001-aa13-496a0396d40f/resourceInstances/virc_fe_be/artifacts/5138e316-0237-49aa-817a-b3d8eaf77392";
249         Mockito.doReturn(IOUtils.toString(
250                 SdcCatalogServicesItCase.class.getResourceAsStream("/example/sdc/sdcAlarmsList.csv"), "UTF-8"))
251                 .when(spy).getCldsServicesOrResourcesBasedOnURL(csvAlarmsDetailUrl);
252         Mockito.doReturn(IOUtils.toString(
253                 SdcCatalogServicesItCase.class.getResourceAsStream("/example/sdc/sdcAlarmsList.csv"), "UTF-8"))
254                 .when(spy).getCldsServicesOrResourcesBasedOnURL(csvAlarmsDetailUrl);
255         String csvAlarmsDetailUrl2 = refProp.getStringValue("sdc.catalog.url")
256                 + "resources/d7646638-2572-4a94-b497-c028ac15f9ca/artifacts/5138e316-0237-49aa-817a-b3d8eaf77392";
257         Mockito.doReturn(IOUtils.toString(
258                 SdcCatalogServicesItCase.class.getResourceAsStream("/example/sdc/sdcAlarmsList.csv"), "UTF-8"))
259                 .when(spy).getCldsServicesOrResourcesBasedOnURL(csvAlarmsDetailUrl2);
260         String allVfResourcesDetailUrl = refProp.getStringValue("sdc.catalog.url") + "resources?resourceType=VF";
261         Mockito.doReturn(IOUtils.toString(
262                 SdcCatalogServicesItCase.class.getResourceAsStream("/example/sdc/sdcVFResources.json"), "UTF-8"))
263                 .when(spy).getCldsServicesOrResourcesBasedOnURL(allVfResourcesDetailUrl);
264         String vfcResourcesDetailUrl = refProp.getStringValue("sdc.catalog.url")
265                 + "resources/a0475018-1e7e-4ddd-8bee-33cbf958c2e6/metadata";
266         Mockito.doReturn(IOUtils.toString(
267                 SdcCatalogServicesItCase.class.getResourceAsStream("/example/sdc/sdcCVFCResourceExample.json"),
268                 "UTF-8")).when(spy).getCldsServicesOrResourcesBasedOnURL(vfcResourcesDetailUrl);
269         String allVfcResourcesDetailUrl = refProp.getStringValue("sdc.catalog.url") + "resources?resourceType=VFC";
270         Mockito.doReturn(IOUtils.toString(
271                 SdcCatalogServicesItCase.class.getResourceAsStream("/example/sdc/sdcVFCResources.json"), "UTF-8"))
272                 .when(spy).getCldsServicesOrResourcesBasedOnURL(allVfcResourcesDetailUrl);
273         String allCvfcResourcesDetailUrl = refProp.getStringValue("sdc.catalog.url") + "resources?resourceType=CVFC";
274         Mockito.doReturn(IOUtils.toString(
275                 SdcCatalogServicesItCase.class.getResourceAsStream("/example/sdc/sdcCVFCResources.json"), "UTF-8"))
276                 .when(spy).getCldsServicesOrResourcesBasedOnURL(allCvfcResourcesDetailUrl);
277         String allVfAlarms = refProp.getStringValue("sdc.catalog.url")
278                 + "resources/84855843-5247-4e97-a2bd-5395a510253b/artifacts/d57ac7ec-f3c3-4793-983a-c75ac3a43153";
279         Mockito.doReturn(IOUtils.toString(
280                 SdcCatalogServicesItCase.class.getResourceAsStream("/example/sdc/sdcMeasurementsList.csv"), "UTF-8"))
281                 .when(spy).getCldsServicesOrResourcesBasedOnURL(allVfAlarms);
282         String vfcResourceExample = refProp.getStringValue("sdc.catalog.url")
283                 + "resources/d7646638-2572-4a94-b497-c028ac15f9ca/metadata";
284         Mockito.doReturn(IOUtils.toString(
285                 SdcCatalogServicesItCase.class.getResourceAsStream("/example/sdc/sdcVFCResourceExample.json"), "UTF-8"))
286                 .when(spy).getCldsServicesOrResourcesBasedOnURL(vfcResourceExample);
287         CldsServiceData cldsServiceData = spy
288                 .getCldsServiceDataWithAlarmConditions("a33ed748-3477-4434-b3f3-b5560f5e7d9c");
289         assertTrue("a33ed748-3477-4434-b3f3-b5560f5e7d9c".equals(cldsServiceData.getServiceInvariantUUID()));
290         assertTrue("29018914-966c-442d-9d08-251b9dc45b8f".equals(cldsServiceData.getServiceUUID()));
291         assertTrue(cldsServiceData.getCldsVfs().size() == 1);
292         List<CldsAlarmCondition> alarmsList = spy.getAllAlarmConditionsFromCldsServiceData(cldsServiceData,
293                 "alarmCondition");
294         assertTrue(alarmsList.size() == 12);
295     }
296 }