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 * ===================================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
24 package org.onap.clamp.clds.it.sdc.controller.installer;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.fail;
30 import com.att.aft.dme2.internal.apache.commons.io.IOUtils;
31 import com.att.aft.dme2.internal.apache.commons.lang.RandomStringUtils;
33 import java.io.IOException;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mockito;
38 import org.onap.clamp.clds.dao.CldsDao;
39 import org.onap.clamp.clds.exception.sdc.controller.CsarHandlerException;
40 import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException;
41 import org.onap.clamp.clds.model.CldsModel;
42 import org.onap.clamp.clds.model.CldsTemplate;
43 import org.onap.clamp.clds.sdc.controller.installer.CsarHandler;
44 import org.onap.clamp.clds.sdc.controller.installer.CsarInstaller;
45 import org.onap.clamp.clds.sdc.controller.installer.CsarInstallerImpl;
46 import org.onap.clamp.clds.util.ResourceFileUtil;
47 import org.openecomp.sdc.tosca.parser.api.ISdcCsarHelper;
48 import org.openecomp.sdc.tosca.parser.exceptions.SdcToscaParserException;
49 import org.openecomp.sdc.toscaparser.api.elements.Metadata;
50 import org.skyscreamer.jsonassert.JSONAssert;
51 import org.springframework.beans.factory.annotation.Autowired;
52 import org.springframework.boot.test.context.SpringBootTest;
53 import org.springframework.test.context.junit4.SpringRunner;
54 import org.springframework.test.util.ReflectionTestUtils;
56 @RunWith(SpringRunner.class)
58 public class CsarInstallerItCase {
60 private static final String sdcFolder = "/tmp/csar-handler-tests";
61 private static final String csarArtifactName = "testArtifact.csar";
63 private CsarInstaller csarInstaller;
65 private CldsDao cldsDao;
67 private void loadFile(String fileName) throws IOException {
68 ReflectionTestUtils.setField(csarInstaller, "blueprintMappingFile", fileName);
69 ((CsarInstallerImpl) csarInstaller).loadConfiguration();
72 @Test(expected = SdcArtifactInstallerException.class)
73 public void testInstallTheCsarFail()
74 throws SdcArtifactInstallerException, SdcToscaParserException, CsarHandlerException, IOException {
75 CsarHandler csarHandler = Mockito.mock(CsarHandler.class);
76 Mockito.when(csarHandler.getDcaeBlueprint()).thenReturn(IOUtils
77 .toString(ResourceFileUtil.getResourceAsStream("example/sdc/blueprint-dcae/not-recognized.yaml")));
78 csarInstaller.installTheCsar(csarHandler);
79 fail("Should have raised an SdcArtifactInstallerException");
82 @Test(expected = SdcArtifactInstallerException.class)
83 public void testInstallTheCsarTca()
84 throws SdcArtifactInstallerException, SdcToscaParserException, CsarHandlerException, IOException {
85 String generatedName = RandomStringUtils.randomAlphanumeric(5);
86 CsarHandler csarHandler = Mockito.mock(CsarHandler.class);
87 Mockito.when(csarHandler.getDcaeBlueprint())
88 .thenReturn(ResourceFileUtil.getResourceAsString("example/sdc/blueprint-dcae/tca.yaml"));
89 ISdcCsarHelper csarHelper = Mockito.mock(ISdcCsarHelper.class);
90 Metadata data = Mockito.mock(Metadata.class);
91 Mockito.when(data.getValue("name")).thenReturn(generatedName);
92 Mockito.when(csarHelper.getServiceMetadata()).thenReturn(data);
93 Mockito.when(csarHandler.getSdcCsarHelper()).thenReturn(csarHelper);
94 csarInstaller.installTheCsar(csarHandler);
95 // Get the template back from DB
96 CldsTemplate templateFromDB = CldsTemplate.retrieve(cldsDao,
97 CsarInstallerImpl.TEMPLATE_NAME_PREFIX + generatedName, false);
98 assertNotNull(templateFromDB);
99 assertNotNull(templateFromDB.getBpmnText());
100 assertNotNull(templateFromDB.getImageText());
101 assertNotNull(templateFromDB.getPropText());
102 assertEquals(templateFromDB.getName(), CsarInstallerImpl.TEMPLATE_NAME_PREFIX + generatedName);
103 JSONAssert.assertEquals(templateFromDB.getPropText(),
104 ResourceFileUtil.getResourceAsString("example/dao/template-doc-content.json"), true);
105 // Get the Model back from DB
106 CldsModel modelFromDB = CldsModel.retrieve(cldsDao, generatedName, false);
107 assertNotNull(modelFromDB);
108 assertNotNull(modelFromDB.getBpmnText());
109 assertNotNull(modelFromDB.getImageText());
110 assertNotNull(modelFromDB.getPropText());
111 assertEquals(modelFromDB.getName(), generatedName);