2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.mso.adapters.vdu.mapper;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.when;
27 import java.util.HashMap;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.openecomp.mso.adapters.vdu.VduArtifact;
33 import org.openecomp.mso.adapters.vdu.VduArtifact.ArtifactType;
34 import org.openecomp.mso.adapters.vdu.VduModelInfo;
35 import org.openecomp.mso.db.catalog.CatalogDatabase;
36 import org.openecomp.mso.db.catalog.beans.HeatEnvironment;
37 import org.openecomp.mso.db.catalog.beans.HeatFiles;
38 import org.openecomp.mso.db.catalog.beans.HeatTemplate;
39 import org.openecomp.mso.db.catalog.beans.VfModule;
40 import org.openecomp.mso.db.catalog.beans.VfModuleCustomization;
41 import org.powermock.api.mockito.PowerMockito;
42 import org.powermock.core.classloader.annotations.PrepareForTest;
43 import org.powermock.modules.junit4.PowerMockRunner;
45 @RunWith(PowerMockRunner.class)
46 @PrepareForTest({CatalogDatabase.class})
47 public class VfModuleCustomizationToVduMapperTest {
49 private static final String HEAT_TEMPLATE_ARTIFACT_UUID = "testHeatTemplate";
50 private static final String VF_MODULE_MODEL_UUID = "vfModuleUuid";
51 private static final String HEAT_ENV_ARTIFACT_UUID = "heatEnvArtifactUuid";
52 private static final String MAIN_TEMPLATE_NAME = "testTempName";
53 private static final String MAIN_TEMPLATE_BODY = "testTempBody";
54 private static final String NESTED_TEMPLATE_KEY = "testKey";
55 private static final String NESTED_TEMPLATE_VALUE = "nestedTemplateTest";
56 private static final String HEAT_FILE_NAME = "heatFileName";
57 private static final String HEAT_FILE_BODY = "heatFileBodyTest";
58 private static final String HEAT_ENV_NAME = "heatEnvName";
59 private static final String HEAT_ENV = "heatEnv";
60 private static final String VOL_HEAT_TEMPLATE_ARTIFACT = "volEnvArtifact";
61 private static final String VOL_ENV_ARTIFACT_UUID = "volEnvArtifactUuid";
63 private VfModuleCustomizationToVduMapper testedObject;
64 private CatalogDatabase catalogDatabaseMock;
68 PowerMockito.mockStatic(CatalogDatabase.class);
69 catalogDatabaseMock = mock(CatalogDatabase.class);
70 testedObject = new VfModuleCustomizationToVduMapper();
71 PowerMockito.when(CatalogDatabase.getInstance()).thenReturn(catalogDatabaseMock);
72 when(catalogDatabaseMock.getNestedTemplates(HEAT_TEMPLATE_ARTIFACT_UUID)).thenReturn(createNestedTemplates());
73 when(catalogDatabaseMock.getHeatFilesForVfModule(VF_MODULE_MODEL_UUID)).thenReturn(createHeatFileMap());
77 public void mapVfModuleCustomizationToVdu_successful() {
78 when(catalogDatabaseMock.getHeatTemplateByArtifactUuid(HEAT_TEMPLATE_ARTIFACT_UUID))
79 .thenReturn(createHeatTemplate());
80 when(catalogDatabaseMock.getHeatEnvironmentByArtifactUuid(HEAT_ENV_ARTIFACT_UUID))
81 .thenReturn(createHeatEnvironment());
82 VduModelInfo result = testedObject.mapVfModuleCustomizationToVdu(createVfModuleCustomization());
83 assertThat(result.getArtifacts()).containsExactly(createExpectedResultArray());
87 public void mapVfModuleCustVolumeToVdu_successful() {
88 when(catalogDatabaseMock.getHeatTemplateByArtifactUuid(VOL_HEAT_TEMPLATE_ARTIFACT))
89 .thenReturn(createHeatTemplate());
90 when(catalogDatabaseMock.getHeatEnvironmentByArtifactUuid(VOL_ENV_ARTIFACT_UUID))
91 .thenReturn(createHeatEnvironment());
92 VduModelInfo result = testedObject.mapVfModuleCustVolumeToVdu(createVfModuleCustomization());
93 assertThat(result.getArtifacts()).containsExactly(createExpectedResultArray());
96 private VfModuleCustomization createVfModuleCustomization() {
97 VfModuleCustomization vfModuleCustomization = new VfModuleCustomization();
98 VfModule vfModule = new VfModule();
99 vfModule.setHeatTemplateArtifactUUId(HEAT_TEMPLATE_ARTIFACT_UUID);
100 vfModule.setVolHeatTemplateArtifactUUId(VOL_HEAT_TEMPLATE_ARTIFACT);
101 vfModuleCustomization.setVfModule(vfModule);
102 vfModuleCustomization.setVfModuleModelUuid(VF_MODULE_MODEL_UUID);
103 vfModuleCustomization.setHeatEnvironmentArtifactUuid(HEAT_ENV_ARTIFACT_UUID);
104 vfModuleCustomization.setVolEnvironmentArtifactUuid(VOL_ENV_ARTIFACT_UUID);
105 return vfModuleCustomization;
108 private HeatTemplate createHeatTemplate() {
109 HeatTemplate heatTemplate = new HeatTemplate();
110 heatTemplate.setTemplateName(MAIN_TEMPLATE_NAME);
111 heatTemplate.setTemplateBody(MAIN_TEMPLATE_BODY);
112 heatTemplate.setArtifactUuid(HEAT_TEMPLATE_ARTIFACT_UUID);
116 private Map<String, Object> createNestedTemplates() {
117 Map<String, Object> map = new HashMap<>();
118 map.put(NESTED_TEMPLATE_KEY, NESTED_TEMPLATE_VALUE);
122 private Map<String, HeatFiles> createHeatFileMap() {
123 HeatFiles heatFiles = new HeatFiles();
124 heatFiles.setFileName(HEAT_FILE_NAME);
125 heatFiles.setFileBody(HEAT_FILE_BODY);
126 Map<String, HeatFiles> map = new HashMap<>();
127 map.put("heatFileKey", heatFiles);
131 private HeatEnvironment createHeatEnvironment() {
132 HeatEnvironment heatEnvironment = new HeatEnvironment();
133 heatEnvironment.setName(HEAT_ENV_NAME);
134 heatEnvironment.setEnvironment(HEAT_ENV);
135 return heatEnvironment;
139 private VduArtifact[] createExpectedResultArray() {
140 VduArtifact[] vduArtifactsArray = new VduArtifact[4];
141 vduArtifactsArray[0] = new VduArtifact(MAIN_TEMPLATE_NAME, MAIN_TEMPLATE_BODY.getBytes(),
142 ArtifactType.MAIN_TEMPLATE);
143 vduArtifactsArray[1] = new VduArtifact(NESTED_TEMPLATE_KEY, NESTED_TEMPLATE_VALUE.getBytes(),
144 ArtifactType.NESTED_TEMPLATE);
145 vduArtifactsArray[2] = createVduArtifact(HEAT_FILE_NAME, HEAT_FILE_BODY, ArtifactType.TEXT_FILE);
146 vduArtifactsArray[3] = createVduArtifact(HEAT_ENV_NAME, HEAT_ENV, ArtifactType.ENVIRONMENT);
147 return vduArtifactsArray;
150 private VduArtifact createVduArtifact(String name, String content, ArtifactType artifactType) {
151 VduArtifact vduArtifact = new VduArtifact();
152 vduArtifact.setName(name);
153 vduArtifact.setContent(content.getBytes());
154 vduArtifact.setType(artifactType);