Generate API documents
[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 com.att.aft.dme2.internal.apache.commons.io.IOUtils;
33 import com.att.aft.dme2.internal.apache.commons.lang.RandomStringUtils;
34
35 import java.io.IOException;
36 import java.util.ArrayList;
37 import java.util.HashMap;
38 import java.util.List;
39 import java.util.Map;
40
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.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.junit4.SpringRunner;
64
65 @RunWith(SpringRunner.class)
66 @SpringBootTest
67 public class CsarInstallerItCase {
68
69     private static final String CSAR_ARTIFACT_NAME = "testArtifact.csar";
70     private static final String INVARIANT_SERVICE_UUID = "4cc5b45a-1f63-4194-8100-cd8e14248c92";
71     private static final String INVARIANT_RESOURCE1_UUID = "07e266fc-49ab-4cd7-8378-ca4676f1b9ec";
72     private static final String INVARIANT_RESOURCE2_UUID = "023a3f0d-1161-45ff-b4cf-8918a8ccf3ad";
73     private static final String INSTANCE_NAME_RESOURCE1 = "ResourceInstanceName1";
74     private static final String INSTANCE_NAME_RESOURCE2 = "ResourceInstanceName2";
75     @Autowired
76     private CsarInstaller csarInstaller;
77     @Autowired
78     private CldsDao cldsDao;
79
80     @Test(expected = SdcArtifactInstallerException.class)
81     public void testInstallTheCsarFail()
82             throws SdcArtifactInstallerException, SdcToscaParserException, CsarHandlerException, IOException {
83         CsarHandler csarHandler = Mockito.mock(CsarHandler.class);
84         BlueprintArtifact blueprintArtifact = Mockito.mock(BlueprintArtifact.class);
85         Mockito.when(blueprintArtifact.getResourceAttached()).thenReturn(Mockito.mock(IResourceInstance.class));
86         Map<String, BlueprintArtifact> blueprintMap = new HashMap<>();
87         blueprintMap.put("resourceid", blueprintArtifact);
88         Mockito.when(csarHandler.getMapOfBlueprints()).thenReturn(blueprintMap);
89         Mockito.when(blueprintArtifact.getDcaeBlueprint()).thenReturn(IOUtils
90                 .toString(ResourceFileUtil.getResourceAsStream("example/sdc/blueprint-dcae/not-recognized.yaml")));
91         csarInstaller.installTheCsar(csarHandler);
92         fail("Should have raised an SdcArtifactInstallerException");
93     }
94
95     private BlueprintArtifact buildFakeBuildprintArtifact(String instanceName, String invariantResourceUuid,
96             String blueprintFilePath, String csarArtifactName, String invariantServiceUuid) throws IOException {
97         IResourceInstance resource = Mockito.mock(IResourceInstance.class);
98         Mockito.when(resource.getResourceInstanceName()).thenReturn(instanceName);
99         Mockito.when(resource.getResourceInvariantUUID()).thenReturn(invariantResourceUuid);
100         BlueprintArtifact blueprintArtifact = Mockito.mock(BlueprintArtifact.class);
101         Mockito.when(blueprintArtifact.getDcaeBlueprint())
102                 .thenReturn(ResourceFileUtil.getResourceAsString(blueprintFilePath));
103         Mockito.when(blueprintArtifact.getBlueprintArtifactName()).thenReturn(csarArtifactName);
104         Mockito.when(blueprintArtifact.getBlueprintInvariantServiceUuid()).thenReturn(invariantServiceUuid);
105         Mockito.when(blueprintArtifact.getResourceAttached()).thenReturn(resource);
106         return blueprintArtifact;
107     }
108
109     private CsarHandler buildFakeCsarHandler(String generatedName) throws IOException {
110         // Create fake notification
111         INotificationData notificationData = Mockito.mock(INotificationData.class);
112         Mockito.when(notificationData.getServiceVersion()).thenReturn("1.0");
113         // Create fake resource in notification
114         CsarHandler csarHandler = Mockito.mock(CsarHandler.class);
115         List<IResourceInstance> listResources = new ArrayList<>();
116         Mockito.when(notificationData.getResources()).thenReturn(listResources);
117         Map<String, BlueprintArtifact> blueprintMap = new HashMap<>();
118         Mockito.when(csarHandler.getMapOfBlueprints()).thenReturn(blueprintMap);
119         // Create fake blueprint artifact 1
120         BlueprintArtifact blueprintArtifact = buildFakeBuildprintArtifact(INSTANCE_NAME_RESOURCE1,
121                 INVARIANT_RESOURCE1_UUID, "example/sdc/blueprint-dcae/tca.yaml", CSAR_ARTIFACT_NAME,
122                 INVARIANT_SERVICE_UUID);
123         listResources.add(blueprintArtifact.getResourceAttached());
124         blueprintMap.put(blueprintArtifact.getResourceAttached().getResourceInstanceName(), blueprintArtifact);
125         // Create fake blueprint artifact 2
126         blueprintArtifact = buildFakeBuildprintArtifact(INSTANCE_NAME_RESOURCE2, INVARIANT_RESOURCE2_UUID,
127                 "example/sdc/blueprint-dcae/tca_2.yaml", CSAR_ARTIFACT_NAME, INVARIANT_SERVICE_UUID);
128         listResources.add(blueprintArtifact.getResourceAttached());
129         blueprintMap.put(blueprintArtifact.getResourceAttached().getResourceInstanceName(), blueprintArtifact);
130         // Build fake csarhandler
131         Mockito.when(csarHandler.getSdcNotification()).thenReturn(notificationData);
132         // Build fake csar Helper
133         ISdcCsarHelper csarHelper = Mockito.mock(ISdcCsarHelper.class);
134         Metadata data = Mockito.mock(Metadata.class);
135         Mockito.when(data.getValue("name")).thenReturn(generatedName);
136         Mockito.when(csarHelper.getServiceMetadata()).thenReturn(data);
137         Mockito.when(csarHandler.getSdcCsarHelper()).thenReturn(csarHelper);
138         return csarHandler;
139     }
140
141     @Test
142     public void testIsCsarAlreadyDeployedTca()
143             throws SdcArtifactInstallerException, SdcToscaParserException, CsarHandlerException, IOException {
144         String generatedName = RandomStringUtils.randomAlphanumeric(5);
145         CsarHandler csarHandler = buildFakeCsarHandler(generatedName);
146         assertFalse(csarInstaller.isCsarAlreadyDeployed(csarHandler));
147         csarInstaller.installTheCsar(csarHandler);
148         assertTrue(csarInstaller.isCsarAlreadyDeployed(csarHandler));
149     }
150
151     @Test
152     public void testInstallTheCsarTca()
153             throws SdcArtifactInstallerException, SdcToscaParserException, CsarHandlerException, IOException, JSONException {
154         String generatedName = RandomStringUtils.randomAlphanumeric(5);
155         CsarHandler csar = buildFakeCsarHandler(generatedName);
156         csarInstaller.installTheCsar(csar);
157         CldsModel cldsModel1 = verifyClosedLoopModelLoadedInDb(csar, generatedName, INSTANCE_NAME_RESOURCE1);
158         JSONAssert.assertEquals(
159                 IOUtils.toString(
160                         ResourceFileUtil.getResourceAsStream("example/sdc/blueprint-dcae/prop-text-for-tca.json")),
161                 cldsModel1.getPropText(), true);
162         CldsModel cldsModel2 = verifyClosedLoopModelLoadedInDb(csar, generatedName, INSTANCE_NAME_RESOURCE2);
163         JSONAssert.assertEquals(
164                 IOUtils.toString(
165                         ResourceFileUtil.getResourceAsStream("example/sdc/blueprint-dcae/prop-text-for-tca-2.json")),
166                 cldsModel2.getPropText(), true);
167     }
168
169     private CldsModel verifyClosedLoopModelLoadedInDb(CsarHandler csar, String generatedName,
170             String instanceNameResource) throws SdcArtifactInstallerException {
171         // Get the template back from DB
172         CldsTemplate templateFromDb = CldsTemplate.retrieve(cldsDao,
173                 CsarInstallerImpl.TEMPLATE_NAME_PREFIX + CsarInstallerImpl.buildModelName(csar, instanceNameResource),
174                 false);
175         assertNotNull(templateFromDb);
176         assertNotNull(templateFromDb.getBpmnText());
177         assertNotNull(templateFromDb.getImageText());
178         assertNotNull(templateFromDb.getPropText());
179         assertTrue(templateFromDb.getPropText().contains("global")
180                 && templateFromDb.getPropText().contains("node_templates:"));
181         assertEquals(templateFromDb.getName(),
182                 CsarInstallerImpl.TEMPLATE_NAME_PREFIX + CsarInstallerImpl.buildModelName(csar, instanceNameResource));
183         // Get the Model back from DB
184         CldsModel modelFromDb = CldsModel.retrieve(cldsDao,
185                 CsarInstallerImpl.buildModelName(csar, instanceNameResource), true);
186         assertNotNull(modelFromDb);
187         assertNotNull(modelFromDb.getBpmnText());
188         assertNotNull(modelFromDb.getImageText());
189         assertNotNull(modelFromDb.getPropText());
190         assertTrue(modelFromDb.getPropText().contains("policy_id"));
191         assertEquals(CsarInstallerImpl.buildModelName(csar, instanceNameResource), modelFromDb.getName());
192         assertEquals(CsarInstallerImpl.CONTROL_NAME_PREFIX, modelFromDb.getControlNamePrefix());
193         return modelFromDb;
194     }
195 }