Merge "Some rework around DAO classes"
[clamp.git] / src / test / java / org / onap / clamp / clds / it / SdcCatalogServicesIT.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017 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  * ===================================================================
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23
24 package org.onap.clamp.clds.it;
25
26 import static org.junit.Assert.assertTrue;
27
28 import java.util.LinkedList;
29 import java.util.List;
30
31 import org.apache.commons.io.IOUtils;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.Mockito;
35 import org.onap.clamp.clds.AbstractIT;
36 import org.onap.clamp.clds.client.SdcCatalogServices;
37 import org.onap.clamp.clds.model.CldsAlarmCondition;
38 import org.onap.clamp.clds.model.CldsSdcResource;
39 import org.onap.clamp.clds.model.CldsSdcResourceBasicInfo;
40 import org.onap.clamp.clds.model.CldsSdcServiceInfo;
41 import org.onap.clamp.clds.model.CldsServiceData;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.boot.test.context.SpringBootTest;
44 import org.springframework.test.context.junit4.SpringRunner;
45
46 /**
47  * Test DCAE API in org.onap.clamp.ClampDesigner.client package - replicate DCAE
48  * Delegates in test.
49  */
50 @RunWith(SpringRunner.class)
51 @SpringBootTest
52 public class SdcCatalogServicesIT extends AbstractIT {
53     @Autowired
54     private SdcCatalogServices sdcCatalogWired = new SdcCatalogServices();
55
56     @Test
57     public void removeDuplicateServicesTest() throws Exception {
58
59         CldsSdcServiceInfo service1a = new CldsSdcServiceInfo();
60         service1a.setName("service1");
61         service1a.setVersion("1.0");
62         service1a.setInvariantUUID("invariantUUID1.0");
63
64         List<CldsSdcServiceInfo> rawCldsSdcServiceList = new LinkedList<CldsSdcServiceInfo>();
65         rawCldsSdcServiceList.add(service1a);
66         rawCldsSdcServiceList.add(service1a);
67
68         CldsSdcServiceInfo service1b = new CldsSdcServiceInfo();
69         service1b.setName("service1");
70         service1b.setVersion("1.1");
71         service1b.setInvariantUUID("invariantUUID1.1");
72         rawCldsSdcServiceList.add(service1b);
73
74         CldsSdcServiceInfo service1c = new CldsSdcServiceInfo();
75         service1c.setName("service1");
76         service1c.setVersion("1.2");
77         service1c.setInvariantUUID("invariantUUID1.2");
78         rawCldsSdcServiceList.add(service1c);
79
80         CldsSdcServiceInfo service2 = new CldsSdcServiceInfo();
81         service2.setName("service2");
82         service2.setVersion("1.0");
83         service2.setInvariantUUID("invariantUUID2.0");
84         rawCldsSdcServiceList.add(service2);
85
86         SdcCatalogServices catalogServices = new SdcCatalogServices();
87         List<CldsSdcServiceInfo> resultList = catalogServices.removeDuplicateServices(rawCldsSdcServiceList);
88
89         assertTrue(resultList.size() == 2);
90
91         CldsSdcServiceInfo res1;
92         CldsSdcServiceInfo res2;
93         if ("service1".equals(resultList.get(0).getName())) {
94             res1 = resultList.get(0);
95             res2 = resultList.get(1);
96         } else {
97             res1 = resultList.get(1);
98             res2 = resultList.get(0);
99         }
100
101         assertTrue("service1".equals(res1.getName()));
102         assertTrue("1.2".equals(res1.getVersion()));
103
104         assertTrue("service2".equals(res2.getName()));
105         assertTrue("1.0".equals(res2.getVersion()));
106
107     }
108
109     @Test
110     public void removeDuplicateSdcResourceInstancesTest() {
111
112         List<CldsSdcResource> rawCldsSdcResourceList = new LinkedList<CldsSdcResource>();
113
114         CldsSdcResource sdcResource1a = new CldsSdcResource();
115         sdcResource1a.setResourceInstanceName("resource1");
116         sdcResource1a.setResourceVersion("1.0");
117         rawCldsSdcResourceList.add(sdcResource1a);
118
119         CldsSdcResource sdcResource1b = new CldsSdcResource();
120         sdcResource1b.setResourceInstanceName("resource1");
121         sdcResource1b.setResourceVersion("1.1");
122         rawCldsSdcResourceList.add(sdcResource1b);
123
124         CldsSdcResource sdcResource1c = new CldsSdcResource();
125         sdcResource1c.setResourceInstanceName("resource1");
126         sdcResource1c.setResourceVersion("1.2");
127         rawCldsSdcResourceList.add(sdcResource1c);
128
129         CldsSdcResource sdcResource2 = new CldsSdcResource();
130         sdcResource2.setResourceInstanceName("resource2");
131         sdcResource2.setResourceVersion("1.0");
132         rawCldsSdcResourceList.add(sdcResource2);
133
134         SdcCatalogServices catalogServices = new SdcCatalogServices();
135         List<CldsSdcResource> resultList = catalogServices.removeDuplicateSdcResourceInstances(rawCldsSdcResourceList);
136
137         CldsSdcResource res1;
138         CldsSdcResource res2;
139         if ("resource1".equals(resultList.get(0).getResourceInstanceName())) {
140             res1 = resultList.get(0);
141             res2 = resultList.get(1);
142         } else {
143             res1 = resultList.get(1);
144             res2 = resultList.get(0);
145         }
146
147         assertTrue("resource1".equals(res1.getResourceInstanceName()));
148         assertTrue("1.2".equals(res1.getResourceVersion()));
149
150         assertTrue("resource2".equals(res2.getResourceInstanceName()));
151         assertTrue("1.0".equals(res2.getResourceVersion()));
152
153     }
154
155     @Test
156     public void removeDuplicateSdcResourceBasicInfoTest() {
157
158         List<CldsSdcResourceBasicInfo> rawCldsSdcResourceList = new LinkedList<CldsSdcResourceBasicInfo>();
159
160         CldsSdcResourceBasicInfo sdcResource1a = new CldsSdcResourceBasicInfo();
161         sdcResource1a.setName("resource1");
162         sdcResource1a.setVersion("1.0");
163         rawCldsSdcResourceList.add(sdcResource1a);
164
165         CldsSdcResourceBasicInfo sdcResource1b = new CldsSdcResourceBasicInfo();
166         sdcResource1b.setName("resource1");
167         sdcResource1b.setVersion("1.1");
168         rawCldsSdcResourceList.add(sdcResource1b);
169
170         CldsSdcResourceBasicInfo sdcResource1c = new CldsSdcResourceBasicInfo();
171         sdcResource1c.setName("resource1");
172         sdcResource1c.setVersion("1.2");
173         rawCldsSdcResourceList.add(sdcResource1c);
174
175         CldsSdcResourceBasicInfo sdcResource2 = new CldsSdcResourceBasicInfo();
176         sdcResource2.setName("resource2");
177         sdcResource2.setVersion("1.0");
178         rawCldsSdcResourceList.add(sdcResource2);
179
180         SdcCatalogServices catalogServices = new SdcCatalogServices();
181         List<CldsSdcResourceBasicInfo> resultList = catalogServices
182                 .removeDuplicateSdcResourceBasicInfo(rawCldsSdcResourceList);
183
184         CldsSdcResourceBasicInfo res1;
185         CldsSdcResourceBasicInfo res2;
186         if ("resource1".equals(resultList.get(0).getName())) {
187             res1 = resultList.get(0);
188             res2 = resultList.get(1);
189         } else {
190             res1 = resultList.get(1);
191             res2 = resultList.get(0);
192         }
193
194         assertTrue("resource1".equals(res1.getName()));
195         assertTrue("1.2".equals(res1.getVersion()));
196
197         assertTrue("resource2".equals(res2.getName()));
198         assertTrue("1.0".equals(res2.getVersion()));
199
200     }
201
202     @Test
203     public void getServiceUuidFromServiceInvariantIdTest() throws Exception {
204         SdcCatalogServices spy = Mockito.spy(sdcCatalogWired);
205
206         Mockito.doReturn(IOUtils.toString(
207                 SdcCatalogServicesIT.class.getResourceAsStream("/example/sdc/sdcServicesListExample.json"), "UTF-8"))
208                 .when(spy).getSdcServicesInformation(null);
209         // Try the vcts4 version 1.0, this one should be replaced by 1.1 so it
210         // should not exist, returning empty string
211         String resUuidVcts4Null = spy.getServiceUuidFromServiceInvariantId("a33ed748-3477-4434-b3f3-b5560f5e7d9b");
212         assertTrue("".equals(resUuidVcts4Null));
213
214         // Try the vcts4 version 1.1, this one should be there as it replaces
215         // the vcts4 v1.0
216         String resUuidVcts4Latest = spy.getServiceUuidFromServiceInvariantId("a33ed748-3477-4434-b3f3-b5560f5e7d9c");
217         assertTrue("29018914-966c-442d-9d08-251b9dc45b8f".equals(resUuidVcts4Latest));
218
219         // Try the vcts5 version 1.0, this one should be there
220         String resUuidVcts5 = spy.getServiceUuidFromServiceInvariantId("a33ed748-3477-4434-b3f3-b5560f5e7d8c");
221         assertTrue("29018914-966c-442d-9d08-251b9dc45b7f".equals(resUuidVcts5));
222
223         // try one that does not exist at all
224         String resUuidUnknown = spy.getServiceUuidFromServiceInvariantId("testuuid");
225         assertTrue("".equals(resUuidUnknown));
226
227     }
228
229     @Test
230     public void getCldsServiceDataWithAlarmConditionsTest() throws Exception {
231         SdcCatalogServices spy = Mockito.spy(sdcCatalogWired);
232
233         Mockito.doReturn(IOUtils.toString(
234                 SdcCatalogServicesIT.class.getResourceAsStream("/example/sdc/sdcServicesListExample.json"), "UTF-8"))
235                 .when(spy).getSdcServicesInformation(null);
236
237         // This invariant uuid is the one from vcts4 v1.1
238         String serviceResourceDetailUrl = refProp.getStringValue("sdc.serviceUrl")
239                 + "/29018914-966c-442d-9d08-251b9dc45b8f/metadata";
240         Mockito.doReturn(IOUtils.toString(
241                 SdcCatalogServicesIT.class.getResourceAsStream("/example/sdc/sdcServiceDetailsExample.json"), "UTF-8"))
242                 .when(spy).getCldsServicesOrResourcesBasedOnURL(serviceResourceDetailUrl, false);
243
244         String resourceDetailUrl = refProp.getStringValue("sdc.catalog.url")
245                 + "resources/585822c7-4027-4f84-ba50-e9248606f136/metadata";
246         Mockito.doReturn(IOUtils.toString(
247                 SdcCatalogServicesIT.class.getResourceAsStream("/example/sdc/sdcResourceDetailsExample.json"), "UTF-8"))
248                 .when(spy).getCldsServicesOrResourcesBasedOnURL(resourceDetailUrl, false);
249
250         String securityRulesDetailUrl = refProp.getStringValue("sdc.catalog.url")
251                 + "resources/d57e57d2-e3c6-470d-8d16-e6ea05f536c5/metadata";
252         Mockito.doReturn(IOUtils.toString(
253                 SdcCatalogServicesIT.class.getResourceAsStream("/example/sdc/sdcSecurityRules.json"), "UTF-8"))
254                 .when(spy).getCldsServicesOrResourcesBasedOnURL(securityRulesDetailUrl, false);
255
256         String cinderVolumeDetailUrl = refProp.getStringValue("sdc.catalog.url")
257                 + "resources/b4288e07-597a-44a2-aa98-ad36e551a39d/metadata";
258         Mockito.doReturn(IOUtils
259                 .toString(SdcCatalogServicesIT.class.getResourceAsStream("/example/sdc/sdcCinderVolume.json"), "UTF-8"))
260                 .when(spy).getCldsServicesOrResourcesBasedOnURL(cinderVolumeDetailUrl, false);
261
262         String vfcGenericDetailUrl = refProp.getStringValue("sdc.catalog.url")
263                 + "resources/2c8f1219-8000-4001-aa13-496a0396d40f/metadata";
264         Mockito.doReturn(IOUtils.toString(
265                 SdcCatalogServicesIT.class.getResourceAsStream("/example/sdc/sdcVFCGenericWithAlarms.json"), "UTF-8"))
266                 .when(spy).getCldsServicesOrResourcesBasedOnURL(vfcGenericDetailUrl, false);
267
268         String csvAlarmsDetailUrl = refProp.getStringValue("sdc.catalog.url")
269                 + "resources/2c8f1219-8000-4001-aa13-496a0396d40f/resourceInstances/virc_fe_be/artifacts/5138e316-0237-49aa-817a-b3d8eaf77392";
270         Mockito.doReturn(IOUtils
271                 .toString(SdcCatalogServicesIT.class.getResourceAsStream("/example/sdc/sdcAlarmsList.csv"), "UTF-8"))
272                 .when(spy).getCldsServicesOrResourcesBasedOnURL(csvAlarmsDetailUrl, false);
273
274         Mockito.doReturn(IOUtils
275                 .toString(SdcCatalogServicesIT.class.getResourceAsStream("/example/sdc/sdcAlarmsList.csv"), "UTF-8"))
276                 .when(spy).getCldsServicesOrResourcesBasedOnURL(csvAlarmsDetailUrl, true);
277
278         String csvAlarmsDetailUrl2 = refProp.getStringValue("sdc.catalog.url")
279                 + "resources/d7646638-2572-4a94-b497-c028ac15f9ca/artifacts/5138e316-0237-49aa-817a-b3d8eaf77392";
280         Mockito.doReturn(IOUtils
281                 .toString(SdcCatalogServicesIT.class.getResourceAsStream("/example/sdc/sdcAlarmsList.csv"), "UTF-8"))
282                 .when(spy).getCldsServicesOrResourcesBasedOnURL(csvAlarmsDetailUrl2, true);
283
284         String allVfResourcesDetailUrl = refProp.getStringValue("sdc.catalog.url") + "resources?resourceType=VF";
285         Mockito.doReturn(IOUtils
286                 .toString(SdcCatalogServicesIT.class.getResourceAsStream("/example/sdc/sdcVFResources.json"), "UTF-8"))
287                 .when(spy).getCldsServicesOrResourcesBasedOnURL(allVfResourcesDetailUrl, false);
288
289         String cVfcResourcesDetailUrl = refProp.getStringValue("sdc.catalog.url")
290                 + "resources/a0475018-1e7e-4ddd-8bee-33cbf958c2e6/metadata";
291         Mockito.doReturn(IOUtils.toString(
292                 SdcCatalogServicesIT.class.getResourceAsStream("/example/sdc/sdcCVFCResourceExample.json"), "UTF-8"))
293                 .when(spy).getCldsServicesOrResourcesBasedOnURL(cVfcResourcesDetailUrl, false);
294
295         String allVfcResourcesDetailUrl = refProp.getStringValue("sdc.catalog.url") + "resources?resourceType=VFC";
296         Mockito.doReturn(IOUtils
297                 .toString(SdcCatalogServicesIT.class.getResourceAsStream("/example/sdc/sdcVFCResources.json"), "UTF-8"))
298                 .when(spy).getCldsServicesOrResourcesBasedOnURL(allVfcResourcesDetailUrl, false);
299
300         String allVfAlarms = refProp.getStringValue("sdc.catalog.url")
301                 + "resources/84855843-5247-4e97-a2bd-5395a510253b/artifacts/d57ac7ec-f3c3-4793-983a-c75ac3a43153";
302         Mockito.doReturn(IOUtils.toString(
303                 SdcCatalogServicesIT.class.getResourceAsStream("/example/sdc/sdcMeasurementsList.csv"), "UTF-8"))
304                 .when(spy).getCldsServicesOrResourcesBasedOnURL(allVfAlarms, true);
305
306         String vfcResourceExample = refProp.getStringValue("sdc.catalog.url")
307                 + "resources/d7646638-2572-4a94-b497-c028ac15f9ca/metadata";
308         Mockito.doReturn(IOUtils.toString(
309                 SdcCatalogServicesIT.class.getResourceAsStream("/example/sdc/sdcVFCResourceExample.json"), "UTF-8"))
310                 .when(spy).getCldsServicesOrResourcesBasedOnURL(vfcResourceExample, false);
311
312         CldsServiceData cldsServiceData = spy
313                 .getCldsServiceDataWithAlarmConditions("a33ed748-3477-4434-b3f3-b5560f5e7d9c");
314         assertTrue("a33ed748-3477-4434-b3f3-b5560f5e7d9c".equals(cldsServiceData.getServiceInvariantUUID()));
315         assertTrue("29018914-966c-442d-9d08-251b9dc45b8f".equals(cldsServiceData.getServiceUUID()));
316         assertTrue(cldsServiceData.getCldsVfs().size() == 1);
317
318         List<CldsAlarmCondition> alarmsList = spy.getAllAlarmConditionsFromCldsServiceData(cldsServiceData,
319                 "alarmCondition");
320         assertTrue(alarmsList.size() == 12);
321
322     }
323
324 }