2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
24 package org.onap.clamp.clds.it.sdc.controller.installer;
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;
32 import com.att.aft.dme2.internal.apache.commons.io.IOUtils;
33 import com.att.aft.dme2.internal.apache.commons.lang.RandomStringUtils;
35 import java.io.IOException;
36 import java.util.ArrayList;
37 import java.util.HashMap;
38 import java.util.List;
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;
64 @RunWith(SpringRunner.class)
66 public class CsarInstallerItCase {
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 INSTANCE_NAME_RESOURCE1 = "ResourceInstanceName1";
73 private static final String INSTANCE_NAME_RESOURCE2 = "ResourceInstanceName2";
75 private CsarInstaller csarInstaller;
77 private CldsDao cldsDao;
79 @Test(expected = SdcArtifactInstallerException.class)
80 public void testInstallTheCsarFail()
81 throws SdcArtifactInstallerException, SdcToscaParserException, CsarHandlerException, IOException {
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(IOUtils
89 .toString(ResourceFileUtil.getResourceAsStream("example/sdc/blueprint-dcae/not-recognized.yaml")));
90 csarInstaller.installTheCsar(csarHandler);
91 fail("Should have raised an SdcArtifactInstallerException");
94 private BlueprintArtifact buildFakeBuildprintArtifact(String instanceName, String invariantResourceUuid,
95 String blueprintFilePath, String csarArtifactName, 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(csarArtifactName);
103 Mockito.when(blueprintArtifact.getBlueprintInvariantServiceUuid()).thenReturn(invariantServiceUuid);
104 Mockito.when(blueprintArtifact.getResourceAttached()).thenReturn(resource);
105 return blueprintArtifact;
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
119 BlueprintArtifact blueprintArtifact = buildFakeBuildprintArtifact(INSTANCE_NAME_RESOURCE1,
120 INVARIANT_RESOURCE1_UUID, "example/sdc/blueprint-dcae/tca.yaml", CSAR_ARTIFACT_NAME,
121 INVARIANT_SERVICE_UUID);
122 listResources.add(blueprintArtifact.getResourceAttached());
123 blueprintMap.put(blueprintArtifact.getResourceAttached().getResourceInstanceName(), blueprintArtifact);
124 // Create fake blueprint artifact 2
125 blueprintArtifact = buildFakeBuildprintArtifact(INSTANCE_NAME_RESOURCE2, INVARIANT_RESOURCE2_UUID,
126 "example/sdc/blueprint-dcae/tca_2.yaml", CSAR_ARTIFACT_NAME, INVARIANT_SERVICE_UUID);
127 listResources.add(blueprintArtifact.getResourceAttached());
128 blueprintMap.put(blueprintArtifact.getResourceAttached().getResourceInstanceName(), blueprintArtifact);
129 // Build fake csarhandler
130 Mockito.when(csarHandler.getSdcNotification()).thenReturn(notificationData);
131 // Build fake csar Helper
132 ISdcCsarHelper csarHelper = Mockito.mock(ISdcCsarHelper.class);
133 Metadata data = Mockito.mock(Metadata.class);
134 Mockito.when(data.getValue("name")).thenReturn(generatedName);
135 Mockito.when(csarHelper.getServiceMetadata()).thenReturn(data);
136 Mockito.when(csarHandler.getSdcCsarHelper()).thenReturn(csarHelper);
141 public void testIsCsarAlreadyDeployedTca()
142 throws SdcArtifactInstallerException, SdcToscaParserException, CsarHandlerException, IOException {
143 String generatedName = RandomStringUtils.randomAlphanumeric(5);
144 CsarHandler csarHandler = buildFakeCsarHandler(generatedName);
145 assertFalse(csarInstaller.isCsarAlreadyDeployed(csarHandler));
146 csarInstaller.installTheCsar(csarHandler);
147 assertTrue(csarInstaller.isCsarAlreadyDeployed(csarHandler));
151 public void testInstallTheCsarTca()
152 throws SdcArtifactInstallerException, SdcToscaParserException, CsarHandlerException, IOException {
153 String generatedName = RandomStringUtils.randomAlphanumeric(5);
154 CsarHandler csar = buildFakeCsarHandler(generatedName);
155 csarInstaller.installTheCsar(csar);
156 CldsModel cldsModel1 = verifyClosedLoopModelLoadedInDb(csar, generatedName, INSTANCE_NAME_RESOURCE1);
157 JSONAssert.assertEquals(
159 ResourceFileUtil.getResourceAsStream("example/sdc/blueprint-dcae/prop-text-for-tca.json")),
160 cldsModel1.getPropText(), true);
161 CldsModel cldsModel2 = verifyClosedLoopModelLoadedInDb(csar, generatedName, INSTANCE_NAME_RESOURCE2);
162 JSONAssert.assertEquals(
164 ResourceFileUtil.getResourceAsStream("example/sdc/blueprint-dcae/prop-text-for-tca-2.json")),
165 cldsModel2.getPropText(), true);
168 private CldsModel verifyClosedLoopModelLoadedInDb(CsarHandler csar, String generatedName,
169 String instanceNameResource) throws SdcArtifactInstallerException {
170 // Get the template back from DB
171 CldsTemplate templateFromDb = CldsTemplate.retrieve(cldsDao,
172 CsarInstallerImpl.TEMPLATE_NAME_PREFIX + CsarInstallerImpl.buildModelName(csar, instanceNameResource),
174 assertNotNull(templateFromDb);
175 assertNotNull(templateFromDb.getBpmnText());
176 assertNotNull(templateFromDb.getImageText());
177 assertNotNull(templateFromDb.getPropText());
178 assertTrue(templateFromDb.getPropText().contains("global")
179 && templateFromDb.getPropText().contains("node_templates:"));
180 assertEquals(templateFromDb.getName(),
181 CsarInstallerImpl.TEMPLATE_NAME_PREFIX + CsarInstallerImpl.buildModelName(csar, instanceNameResource));
182 // Get the Model back from DB
183 CldsModel modelFromDb = CldsModel.retrieve(cldsDao,
184 CsarInstallerImpl.buildModelName(csar, instanceNameResource), true);
185 assertNotNull(modelFromDb);
186 assertNotNull(modelFromDb.getBpmnText());
187 assertNotNull(modelFromDb.getImageText());
188 assertNotNull(modelFromDb.getPropText());
189 assertTrue(modelFromDb.getPropText().contains("policy_id"));
190 assertEquals(CsarInstallerImpl.buildModelName(csar, instanceNameResource), modelFromDb.getName());
191 assertEquals(CsarInstallerImpl.CONTROL_NAME_PREFIX, modelFromDb.getControlNamePrefix());