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