Fix warning/sonar
[clamp.git] / src / test / java / org / onap / clamp / clds / it / sdc / controller / installer / CsarInstallerItCase.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 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  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.clds.it.sdc.controller.installer;
25
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertTrue;
30 import static org.junit.Assert.fail;
31
32 import java.io.IOException;
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.List;
36 import java.util.Map;
37
38 import org.apache.commons.io.IOUtils;
39 import org.apache.commons.lang3.RandomStringUtils;
40 import org.json.JSONException;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.mockito.Mockito;
44 import org.onap.clamp.clds.dao.CldsDao;
45 import org.onap.clamp.clds.exception.policy.PolicyModelException;
46 import org.onap.clamp.clds.exception.sdc.controller.CsarHandlerException;
47 import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException;
48 import org.onap.clamp.clds.model.CldsModel;
49 import org.onap.clamp.clds.model.CldsTemplate;
50 import org.onap.clamp.clds.sdc.controller.installer.BlueprintArtifact;
51 import org.onap.clamp.clds.sdc.controller.installer.CsarHandler;
52 import org.onap.clamp.clds.sdc.controller.installer.CsarInstaller;
53 import org.onap.clamp.clds.sdc.controller.installer.CsarInstallerImpl;
54 import org.onap.clamp.clds.util.ResourceFileUtil;
55 import org.onap.sdc.api.notification.INotificationData;
56 import org.onap.sdc.api.notification.IResourceInstance;
57 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
58 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
59 import org.onap.sdc.toscaparser.api.elements.Metadata;
60 import org.skyscreamer.jsonassert.JSONAssert;
61 import org.springframework.beans.factory.annotation.Autowired;
62 import org.springframework.boot.test.context.SpringBootTest;
63 import org.springframework.test.context.ActiveProfiles;
64 import org.springframework.test.context.junit4.SpringRunner;
65
66 @RunWith(SpringRunner.class)
67 @SpringBootTest
68 @ActiveProfiles(profiles = "clamp-default,clamp-default-user,clamp-sdc-controller")
69 public class CsarInstallerItCase {
70
71     private static final String CSAR_ARTIFACT_NAME = "testArtifact.csar";
72     private static final String INVARIANT_SERVICE_UUID = "4cc5b45a-1f63-4194-8100-cd8e14248c92";
73     private static final String INVARIANT_RESOURCE1_UUID = "07e266fc-49ab-4cd7-8378-ca4676f1b9ec";
74     private static final String INVARIANT_RESOURCE2_UUID = "023a3f0d-1161-45ff-b4cf-8918a8ccf3ad";
75     private static final String RESOURCE_INSTANCE_NAME_RESOURCE1 = "ResourceInstanceName1";
76     private static final String RESOURCE_INSTANCE_NAME_RESOURCE2 = "ResourceInstanceName2";
77     @Autowired
78     private CsarInstaller csarInstaller;
79     @Autowired
80     private CldsDao cldsDao;
81
82     @Test(expected = SdcArtifactInstallerException.class)
83     public void testInstallTheCsarFail() throws SdcArtifactInstallerException, SdcToscaParserException,
84         CsarHandlerException, IOException, InterruptedException, PolicyModelException {
85         CsarHandler csarHandler = Mockito.mock(CsarHandler.class);
86         BlueprintArtifact blueprintArtifact = Mockito.mock(BlueprintArtifact.class);
87         Mockito.when(blueprintArtifact.getResourceAttached()).thenReturn(Mockito.mock(IResourceInstance.class));
88         Map<String, BlueprintArtifact> blueprintMap = new HashMap<>();
89         blueprintMap.put("resourceid", blueprintArtifact);
90         Mockito.when(csarHandler.getMapOfBlueprints()).thenReturn(blueprintMap);
91         Mockito.when(blueprintArtifact.getDcaeBlueprint()).thenReturn(
92             IOUtils.toString(ResourceFileUtil.getResourceAsStream("example/sdc/blueprint-dcae/not-recognized.yaml")));
93         csarInstaller.installTheCsar(csarHandler);
94         fail("Should have raised an SdcArtifactInstallerException");
95     }
96
97     private BlueprintArtifact buildFakeBuildprintArtifact(String instanceName, String invariantResourceUuid,
98         String blueprintFilePath, String artifactName, String invariantServiceUuid) throws IOException {
99         IResourceInstance resource = Mockito.mock(IResourceInstance.class);
100         Mockito.when(resource.getResourceInstanceName()).thenReturn(instanceName);
101         Mockito.when(resource.getResourceInvariantUUID()).thenReturn(invariantResourceUuid);
102         BlueprintArtifact blueprintArtifact = Mockito.mock(BlueprintArtifact.class);
103         Mockito.when(blueprintArtifact.getDcaeBlueprint())
104             .thenReturn(ResourceFileUtil.getResourceAsString(blueprintFilePath));
105         Mockito.when(blueprintArtifact.getBlueprintArtifactName()).thenReturn(artifactName);
106         Mockito.when(blueprintArtifact.getBlueprintInvariantServiceUuid()).thenReturn(invariantServiceUuid);
107         Mockito.when(blueprintArtifact.getResourceAttached()).thenReturn(resource);
108         return blueprintArtifact;
109     }
110
111     private CsarHandler buildFakeCsarHandler(String generatedName) throws IOException {
112         // Create fake notification
113         INotificationData notificationData = Mockito.mock(INotificationData.class);
114         Mockito.when(notificationData.getServiceVersion()).thenReturn("1.0");
115         // Create fake resource in notification
116         CsarHandler csarHandler = Mockito.mock(CsarHandler.class);
117         List<IResourceInstance> listResources = new ArrayList<>();
118         Mockito.when(notificationData.getResources()).thenReturn(listResources);
119         Map<String, BlueprintArtifact> blueprintMap = new HashMap<>();
120         Mockito.when(csarHandler.getMapOfBlueprints()).thenReturn(blueprintMap);
121         // Create fake blueprint artifact 1 on resource1
122         BlueprintArtifact blueprintArtifact = buildFakeBuildprintArtifact(RESOURCE_INSTANCE_NAME_RESOURCE1,
123             INVARIANT_RESOURCE1_UUID, "example/sdc/blueprint-dcae/tca.yaml", "tca.yaml", INVARIANT_SERVICE_UUID);
124         listResources.add(blueprintArtifact.getResourceAttached());
125         blueprintMap.put(blueprintArtifact.getBlueprintArtifactName(), blueprintArtifact);
126         // Create fake blueprint artifact 2 on resource2
127         blueprintArtifact = buildFakeBuildprintArtifact(RESOURCE_INSTANCE_NAME_RESOURCE2, INVARIANT_RESOURCE2_UUID,
128             "example/sdc/blueprint-dcae/tca_2.yaml", "tca_2.yaml", INVARIANT_SERVICE_UUID);
129         listResources.add(blueprintArtifact.getResourceAttached());
130         blueprintMap.put(blueprintArtifact.getBlueprintArtifactName(), blueprintArtifact);
131
132         // Create fake blueprint artifact 3 on resource 1 so that it's possible to
133         // test multiple CL deployment per Service/vnf
134         blueprintArtifact = buildFakeBuildprintArtifact(RESOURCE_INSTANCE_NAME_RESOURCE1, INVARIANT_RESOURCE1_UUID,
135             "example/sdc/blueprint-dcae/tca_3.yaml", "tca_3.yaml", INVARIANT_SERVICE_UUID);
136         blueprintMap.put(blueprintArtifact.getBlueprintArtifactName(), blueprintArtifact);
137
138         // Build fake csarhandler
139         Mockito.when(csarHandler.getSdcNotification()).thenReturn(notificationData);
140         // Build fake csar Helper
141         ISdcCsarHelper csarHelper = Mockito.mock(ISdcCsarHelper.class);
142         Metadata data = Mockito.mock(Metadata.class);
143         Mockito.when(data.getValue("name")).thenReturn(generatedName);
144         Mockito.when(csarHelper.getServiceMetadata()).thenReturn(data);
145         Mockito.when(csarHandler.getSdcCsarHelper()).thenReturn(csarHelper);
146         return csarHandler;
147     }
148
149     @Test
150     public void testIsCsarAlreadyDeployedTca() throws SdcArtifactInstallerException, SdcToscaParserException,
151         CsarHandlerException, IOException, InterruptedException, PolicyModelException {
152         String generatedName = RandomStringUtils.randomAlphanumeric(5);
153         CsarHandler csarHandler = buildFakeCsarHandler(generatedName);
154         assertFalse(csarInstaller.isCsarAlreadyDeployed(csarHandler));
155         csarInstaller.installTheCsar(csarHandler);
156         assertTrue(csarInstaller.isCsarAlreadyDeployed(csarHandler));
157     }
158
159     @Test
160     public void testInstallTheCsarTca() throws SdcArtifactInstallerException, SdcToscaParserException,
161         CsarHandlerException, IOException, JSONException, InterruptedException, PolicyModelException {
162         String generatedName = RandomStringUtils.randomAlphanumeric(5);
163         CsarHandler csar = buildFakeCsarHandler(generatedName);
164         csarInstaller.installTheCsar(csar);
165         CldsModel cldsModel1 = verifyClosedLoopModelLoadedInDb(csar, "tca.yaml");
166         JSONAssert.assertEquals(
167             IOUtils.toString(ResourceFileUtil.getResourceAsStream("example/sdc/blueprint-dcae/prop-text-for-tca.json")),
168             cldsModel1.getPropText(), true);
169         CldsModel cldsModel2 = verifyClosedLoopModelLoadedInDb(csar, "tca_2.yaml");
170         JSONAssert.assertEquals(
171             IOUtils
172                 .toString(ResourceFileUtil.getResourceAsStream("example/sdc/blueprint-dcae/prop-text-for-tca-2.json")),
173             cldsModel2.getPropText(), true);
174         CldsModel cldsModel3 = verifyClosedLoopModelLoadedInDb(csar, "tca_3.yaml");
175         JSONAssert.assertEquals(
176             IOUtils.toString(ResourceFileUtil.getResourceAsStream("example/sdc/blueprint-dcae/prop-text-for-tca.json")),
177             cldsModel3.getPropText(), true);
178     }
179
180     private CldsModel verifyClosedLoopModelLoadedInDb(CsarHandler csar, String artifactName)
181         throws SdcArtifactInstallerException {
182
183         // Get the template back from DB
184         CldsTemplate templateFromDb = CldsTemplate.retrieve(cldsDao, CsarInstallerImpl.TEMPLATE_NAME_PREFIX
185             + CsarInstallerImpl.buildModelName(csar, csar.getMapOfBlueprints().get(artifactName)), false);
186         assertNotNull(templateFromDb);
187         assertNotNull(templateFromDb.getBpmnText());
188         assertNotNull(templateFromDb.getImageText());
189         assertNotNull(templateFromDb.getPropText());
190         assertTrue(templateFromDb.getPropText().contains("global")
191             && templateFromDb.getPropText().contains("node_templates:"));
192         assertEquals(templateFromDb.getName(), CsarInstallerImpl.TEMPLATE_NAME_PREFIX
193             + CsarInstallerImpl.buildModelName(csar, csar.getMapOfBlueprints().get(artifactName)));
194         // Get the Model back from DB
195         CldsModel modelFromDb = CldsModel.retrieve(cldsDao,
196             CsarInstallerImpl.buildModelName(csar, csar.getMapOfBlueprints().get(artifactName)), true);
197         assertNotNull(modelFromDb);
198         assertNotNull(modelFromDb.getBpmnText());
199         assertNotNull(modelFromDb.getImageText());
200         assertNotNull(modelFromDb.getPropText());
201         assertTrue(modelFromDb.getPropText().contains("policy_id"));
202         assertEquals(CsarInstallerImpl.buildModelName(csar, csar.getMapOfBlueprints().get(artifactName)),
203             modelFromDb.getName());
204         assertEquals(CsarInstallerImpl.CONTROL_NAME_PREFIX, modelFromDb.getControlNamePrefix());
205         return modelFromDb;
206     }
207 }