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.springframework.beans.factory.annotation.Autowired;
60 import org.springframework.boot.test.context.SpringBootTest;
61 import org.springframework.test.context.junit4.SpringRunner;
63 @RunWith(SpringRunner.class)
65 public class CsarInstallerItCase {
67 private static final String CSAR_ARTIFACT_NAME = "testArtifact.csar";
68 private static final String INVARIANT_SERVICE_UUID = "4cc5b45a-1f63-4194-8100-cd8e14248c92";
69 private static final String INVARIANT_RESOURCE1_UUID = "07e266fc-49ab-4cd7-8378-ca4676f1b9ec";
70 private static final String INVARIANT_RESOURCE2_UUID = "023a3f0d-1161-45ff-b4cf-8918a8ccf3ad";
71 private static final String INSTANCE_NAME_RESOURCE1 = "ResourceInstanceName1";
72 private static final String INSTANCE_NAME_RESOURCE2 = "ResourceInstanceName2";
74 private CsarInstaller csarInstaller;
76 private CldsDao cldsDao;
78 @Test(expected = SdcArtifactInstallerException.class)
79 public void testInstallTheCsarFail()
80 throws SdcArtifactInstallerException, SdcToscaParserException, CsarHandlerException, IOException {
81 CsarHandler csarHandler = Mockito.mock(CsarHandler.class);
82 BlueprintArtifact blueprintArtifact = Mockito.mock(BlueprintArtifact.class);
83 Mockito.when(blueprintArtifact.getResourceAttached()).thenReturn(Mockito.mock(IResourceInstance.class));
84 Map<String, BlueprintArtifact> blueprintMap = new HashMap<>();
85 blueprintMap.put("resourceid", blueprintArtifact);
86 Mockito.when(csarHandler.getMapOfBlueprints()).thenReturn(blueprintMap);
87 Mockito.when(blueprintArtifact.getDcaeBlueprint()).thenReturn(IOUtils
88 .toString(ResourceFileUtil.getResourceAsStream("example/sdc/blueprint-dcae/not-recognized.yaml")));
89 csarInstaller.installTheCsar(csarHandler);
90 fail("Should have raised an SdcArtifactInstallerException");
93 private BlueprintArtifact buildFakeBuildprintArtifact(String instanceName, String invariantResourceUuid,
94 String blueprintFilePath, String csarArtifactName, String invariantServiceUuid) throws IOException {
95 IResourceInstance resource = Mockito.mock(IResourceInstance.class);
96 Mockito.when(resource.getResourceInstanceName()).thenReturn(instanceName);
97 Mockito.when(resource.getResourceInvariantUUID()).thenReturn(invariantResourceUuid);
98 BlueprintArtifact blueprintArtifact = Mockito.mock(BlueprintArtifact.class);
99 Mockito.when(blueprintArtifact.getDcaeBlueprint())
100 .thenReturn(ResourceFileUtil.getResourceAsString(blueprintFilePath));
101 Mockito.when(blueprintArtifact.getBlueprintArtifactName()).thenReturn(csarArtifactName);
102 Mockito.when(blueprintArtifact.getBlueprintInvariantServiceUuid()).thenReturn(invariantServiceUuid);
103 Mockito.when(blueprintArtifact.getResourceAttached()).thenReturn(resource);
104 return blueprintArtifact;
107 private CsarHandler buildFakeCsarHandler(String generatedName) throws IOException {
108 // Create fake notification
109 INotificationData notificationData = Mockito.mock(INotificationData.class);
110 Mockito.when(notificationData.getServiceVersion()).thenReturn("1.0");
111 // Create fake resource in notification
112 CsarHandler csarHandler = Mockito.mock(CsarHandler.class);
113 List<IResourceInstance> listResources = new ArrayList<>();
114 Mockito.when(notificationData.getResources()).thenReturn(listResources);
115 Map<String, BlueprintArtifact> blueprintMap = new HashMap<>();
116 Mockito.when(csarHandler.getMapOfBlueprints()).thenReturn(blueprintMap);
117 // Create fake blueprint artifact 1
118 BlueprintArtifact blueprintArtifact = buildFakeBuildprintArtifact(INSTANCE_NAME_RESOURCE1,
119 INVARIANT_RESOURCE1_UUID, "example/sdc/blueprint-dcae/tca.yaml", CSAR_ARTIFACT_NAME,
120 INVARIANT_SERVICE_UUID);
121 listResources.add(blueprintArtifact.getResourceAttached());
122 blueprintMap.put(blueprintArtifact.getResourceAttached().getResourceInstanceName(), blueprintArtifact);
123 // Create fake blueprint artifact 2
124 blueprintArtifact = buildFakeBuildprintArtifact(INSTANCE_NAME_RESOURCE2, INVARIANT_RESOURCE2_UUID,
125 "example/sdc/blueprint-dcae/tca_2.yaml", CSAR_ARTIFACT_NAME, INVARIANT_SERVICE_UUID);
126 listResources.add(blueprintArtifact.getResourceAttached());
127 blueprintMap.put(blueprintArtifact.getResourceAttached().getResourceInstanceName(), blueprintArtifact);
128 // Build fake csarhandler
129 Mockito.when(csarHandler.getSdcNotification()).thenReturn(notificationData);
130 // Build fake csar Helper
131 ISdcCsarHelper csarHelper = Mockito.mock(ISdcCsarHelper.class);
132 Metadata data = Mockito.mock(Metadata.class);
133 Mockito.when(data.getValue("name")).thenReturn(generatedName);
134 Mockito.when(csarHelper.getServiceMetadata()).thenReturn(data);
135 Mockito.when(csarHandler.getSdcCsarHelper()).thenReturn(csarHelper);
140 public void testIsCsarAlreadyDeployedTca()
141 throws SdcArtifactInstallerException, SdcToscaParserException, CsarHandlerException, IOException {
142 String generatedName = RandomStringUtils.randomAlphanumeric(5);
143 CsarHandler csarHandler = buildFakeCsarHandler(generatedName);
144 assertFalse(csarInstaller.isCsarAlreadyDeployed(csarHandler));
145 csarInstaller.installTheCsar(csarHandler);
146 assertTrue(csarInstaller.isCsarAlreadyDeployed(csarHandler));
150 public void testInstallTheCsarTca()
151 throws SdcArtifactInstallerException, SdcToscaParserException, CsarHandlerException, IOException {
152 String generatedName = RandomStringUtils.randomAlphanumeric(5);
153 CsarHandler csar = buildFakeCsarHandler(generatedName);
154 csarInstaller.installTheCsar(csar);
155 // Get the template back from DB
156 CldsTemplate templateFromDb = CldsTemplate.retrieve(cldsDao, CsarInstallerImpl.TEMPLATE_NAME_PREFIX
157 + CsarInstallerImpl.buildModelName(csar, INSTANCE_NAME_RESOURCE1), false);
158 assertNotNull(templateFromDb);
159 assertNotNull(templateFromDb.getBpmnText());
160 assertNotNull(templateFromDb.getImageText());
161 assertNotNull(templateFromDb.getPropText());
162 assertTrue(templateFromDb.getPropText().contains("global")
163 && templateFromDb.getPropText().contains("node_templates:"));
164 assertEquals(templateFromDb.getName(), CsarInstallerImpl.TEMPLATE_NAME_PREFIX
165 + CsarInstallerImpl.buildModelName(csar, INSTANCE_NAME_RESOURCE1));
166 // Get the Model back from DB
167 CldsModel modelFromDb = CldsModel.retrieve(cldsDao,
168 CsarInstallerImpl.buildModelName(csar, INSTANCE_NAME_RESOURCE2), true);
169 assertNotNull(modelFromDb);
170 assertNotNull(modelFromDb.getBpmnText());
171 assertNotNull(modelFromDb.getImageText());
172 assertNotNull(modelFromDb.getPropText());
173 assertEquals(CsarInstallerImpl.buildModelName(csar, INSTANCE_NAME_RESOURCE2), modelFromDb.getName());
174 assertEquals(CsarInstallerImpl.CONTROL_NAME_PREFIX, modelFromDb.getControlNamePrefix());