0cadef9ae0c6e70d0d767033c21639a8fa7d8b38
[clamp.git] / src / test / java / org / onap / clamp / loop / CsarInstallerItCase.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 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.loop;
25
26 import static org.assertj.core.api.Assertions.assertThat;
27
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Optional;
34
35 import javax.transaction.Transactional;
36
37 import org.apache.commons.lang3.RandomStringUtils;
38 import org.json.JSONException;
39 import org.junit.Assert;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.mockito.Mockito;
43 import org.onap.clamp.clds.Application;
44 import org.onap.clamp.clds.exception.policy.PolicyModelException;
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.sdc.controller.installer.BlueprintArtifact;
48 import org.onap.clamp.clds.sdc.controller.installer.CsarHandler;
49 import org.onap.clamp.clds.sdc.controller.installer.CsarInstaller;
50 import org.onap.clamp.clds.util.ResourceFileUtil;
51 import org.onap.sdc.api.notification.IArtifactInfo;
52 import org.onap.sdc.api.notification.INotificationData;
53 import org.onap.sdc.api.notification.IResourceInstance;
54 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
55 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
56 import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory;
57 import org.onap.sdc.toscaparser.api.elements.Metadata;
58 import org.springframework.beans.factory.annotation.Autowired;
59 import org.springframework.boot.test.context.SpringBootTest;
60 import org.springframework.test.annotation.Rollback;
61 import org.springframework.test.context.ActiveProfiles;
62 import org.springframework.test.context.junit4.SpringRunner;
63
64 @RunWith(SpringRunner.class)
65 @SpringBootTest(classes = Application.class)
66 @ActiveProfiles(profiles = "clamp-default,clamp-default-user,clamp-sdc-controller-new")
67 public class CsarInstallerItCase {
68
69     private static final String CSAR_ARTIFACT_NAME = "example/sdc/service-Vloadbalancerms-csar.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 RESOURCE_INSTANCE_NAME_RESOURCE1 = "ResourceInstanceName1";
74     private static final String RESOURCE_INSTANCE_NAME_RESOURCE2 = "ResourceInstanceName2";
75
76     @Autowired
77     private LoopsRepository loopsRepo;
78
79     @Autowired
80     private CsarInstaller csarInstaller;
81
82     private BlueprintArtifact buildFakeBuildprintArtifact(String instanceName, String invariantResourceUuid,
83         String blueprintFilePath, String artifactName, String invariantServiceUuid) throws IOException {
84         IResourceInstance resource = Mockito.mock(IResourceInstance.class);
85         Mockito.when(resource.getResourceInstanceName()).thenReturn(instanceName);
86         Mockito.when(resource.getResourceInvariantUUID()).thenReturn(invariantResourceUuid);
87         BlueprintArtifact blueprintArtifact = Mockito.mock(BlueprintArtifact.class);
88         Mockito.when(blueprintArtifact.getDcaeBlueprint())
89             .thenReturn(ResourceFileUtil.getResourceAsString(blueprintFilePath));
90         Mockito.when(blueprintArtifact.getBlueprintArtifactName()).thenReturn(artifactName);
91         Mockito.when(blueprintArtifact.getBlueprintInvariantServiceUuid()).thenReturn(invariantServiceUuid);
92         Mockito.when(blueprintArtifact.getResourceAttached()).thenReturn(resource);
93         return blueprintArtifact;
94     }
95
96     private CsarHandler buildFakeCsarHandler(String generatedName) throws IOException, SdcToscaParserException {
97         // Create fake notification
98         INotificationData notificationData = Mockito.mock(INotificationData.class);
99         Mockito.when(notificationData.getServiceVersion()).thenReturn("1.0");
100         // Create fake resource in notification
101         CsarHandler csarHandler = Mockito.mock(CsarHandler.class);
102         List<IResourceInstance> listResources = new ArrayList<>();
103         Mockito.when(notificationData.getResources()).thenReturn(listResources);
104         Map<String, BlueprintArtifact> blueprintMap = new HashMap<>();
105         Mockito.when(csarHandler.getMapOfBlueprints()).thenReturn(blueprintMap);
106         // Create fake blueprint artifact 1 on resource1
107         BlueprintArtifact blueprintArtifact = buildFakeBuildprintArtifact(RESOURCE_INSTANCE_NAME_RESOURCE1,
108             INVARIANT_RESOURCE1_UUID, "example/sdc/blueprint-dcae/tca.yaml", "tca.yaml", INVARIANT_SERVICE_UUID);
109         listResources.add(blueprintArtifact.getResourceAttached());
110         blueprintMap.put(blueprintArtifact.getBlueprintArtifactName(), blueprintArtifact);
111         // Create fake blueprint artifact 2 on resource2
112         blueprintArtifact = buildFakeBuildprintArtifact(RESOURCE_INSTANCE_NAME_RESOURCE2, INVARIANT_RESOURCE2_UUID,
113             "example/sdc/blueprint-dcae/tca_2.yaml", "tca_2.yaml", INVARIANT_SERVICE_UUID);
114         listResources.add(blueprintArtifact.getResourceAttached());
115         blueprintMap.put(blueprintArtifact.getBlueprintArtifactName(), blueprintArtifact);
116
117         // Create fake blueprint artifact 3 on resource 1 so that it's possible to
118         // test multiple CL deployment per Service/vnf
119         blueprintArtifact = buildFakeBuildprintArtifact(RESOURCE_INSTANCE_NAME_RESOURCE1, INVARIANT_RESOURCE1_UUID,
120             "example/sdc/blueprint-dcae/tca_3.yaml", "tca_3.yaml", INVARIANT_SERVICE_UUID);
121         blueprintMap.put(blueprintArtifact.getBlueprintArtifactName(), blueprintArtifact);
122
123         // Build fake csarhandler
124         Mockito.when(csarHandler.getSdcNotification()).thenReturn(notificationData);
125         // Build fake csar Helper
126         ISdcCsarHelper csarHelper = Mockito.mock(ISdcCsarHelper.class);
127         Metadata data = Mockito.mock(Metadata.class);
128         Mockito.when(data.getValue("name")).thenReturn(generatedName);
129         Mockito.when(notificationData.getServiceName()).thenReturn(generatedName);
130         Mockito.when(csarHelper.getServiceMetadata()).thenReturn(data);
131
132         // Create helper based on real csar to test policy yaml and global properties
133         // set
134         SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
135         String path = Thread.currentThread().getContextClassLoader().getResource(CSAR_ARTIFACT_NAME).getFile();
136         ISdcCsarHelper sdcHelper = factory
137             .getSdcCsarHelper(path);
138         Mockito.when(csarHandler.getSdcCsarHelper()).thenReturn(sdcHelper);
139
140         // Mockito.when(csarHandler.getSdcCsarHelper()).thenReturn(csarHelper);
141         Mockito.when(csarHandler.getPolicyModelYaml())
142             .thenReturn(Optional.ofNullable(ResourceFileUtil.getResourceAsString("tosca/tosca_example.yaml")));
143         return csarHandler;
144     }
145
146     @Test
147     @Transactional
148     public void testGetPolicyModelYaml() throws IOException, SdcToscaParserException, CsarHandlerException {
149         INotificationData notificationData = Mockito.mock(INotificationData.class);
150         IArtifactInfo serviceArtifacts = Mockito.mock(IArtifactInfo.class);
151         Mockito.when(serviceArtifacts.getArtifactType()).thenReturn("TOSCA_CSAR");
152         List<IArtifactInfo> serviceArtifactsList = new ArrayList<>();
153         serviceArtifactsList.add(serviceArtifacts);
154         Mockito.when(notificationData.getServiceArtifacts()).thenReturn(serviceArtifactsList);
155
156         CsarHandler csarHandler = new CsarHandler(notificationData, "", "");
157         csarHandler.setFilePath(Thread.currentThread().getContextClassLoader()
158                 .getResource(CSAR_ARTIFACT_NAME).getFile());
159         Optional<String> testyaml = csarHandler.getPolicyModelYaml();
160         Assert.assertEquals(testyaml, Optional.ofNullable(ResourceFileUtil
161                 .getResourceAsString("example/sdc/expected-result/policy-data.yaml")));
162     }
163
164     @Test
165     @Transactional
166     public void testIsCsarAlreadyDeployedTca() throws SdcArtifactInstallerException, SdcToscaParserException,
167         CsarHandlerException, IOException, InterruptedException, PolicyModelException {
168         String generatedName = RandomStringUtils.randomAlphanumeric(5);
169         CsarHandler csarHandler = buildFakeCsarHandler(generatedName);
170         assertThat(csarInstaller.isCsarAlreadyDeployed(csarHandler)).isFalse();
171         csarInstaller.installTheCsar(csarHandler);
172         assertThat(csarInstaller.isCsarAlreadyDeployed(csarHandler)).isTrue();
173     }
174
175     @Test
176     @Transactional
177     public void testInstallTheCsarTca() throws SdcArtifactInstallerException, SdcToscaParserException,
178         CsarHandlerException, IOException, JSONException, InterruptedException, PolicyModelException {
179         String generatedName = RandomStringUtils.randomAlphanumeric(5);
180         CsarHandler csar = buildFakeCsarHandler(generatedName);
181         csarInstaller.installTheCsar(csar);
182         assertThat(loopsRepo
183             .existsById(Loop.generateLoopName(generatedName, "1.0", RESOURCE_INSTANCE_NAME_RESOURCE1, "tca.yaml")))
184                 .isTrue();
185         assertThat(loopsRepo
186             .existsById(Loop.generateLoopName(generatedName, "1.0", RESOURCE_INSTANCE_NAME_RESOURCE1, "tca_3.yaml")))
187                 .isTrue();
188         assertThat(loopsRepo
189             .existsById(Loop.generateLoopName(generatedName, "1.0", RESOURCE_INSTANCE_NAME_RESOURCE2, "tca_2.yaml")))
190                 .isTrue();
191         // Verify now that policy and json representation, global properties are well
192         // set
193         Loop loop = loopsRepo
194             .findById(Loop.generateLoopName(generatedName, "1.0", RESOURCE_INSTANCE_NAME_RESOURCE1, "tca.yaml")).get();
195         assertThat(loop.getSvgRepresentation()).startsWith("<svg ");
196         assertThat(loop.getGlobalPropertiesJson().get("dcaeDeployParameters")).isNotNull();
197         assertThat(loop.getMicroServicePolicies()).hasSize(1);
198         assertThat(loop.getOperationalPolicies()).hasSize(1);
199         assertThat(loop.getModelPropertiesJson().get("serviceDetails")).isNotNull();
200         assertThat(loop.getModelPropertiesJson().get("resourceDetails")).isNotNull();
201     }
202
203 }