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