Containerization feature of SO
[so.git] / adapters / mso-vnf-adapter / src / test / java / org / onap / so / adapters / vdu / mapper / VfModuleCustomizationToVduMapperTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.so.adapters.vdu.mapper;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.when;
26
27 import java.util.HashMap;
28 import java.util.Map;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.onap.so.adapters.vdu.VduArtifact;
33 import org.onap.so.adapters.vdu.VduArtifact.ArtifactType;
34 import org.onap.so.adapters.vdu.VduModelInfo;
35 import org.onap.so.db.catalog.CatalogDatabase;
36 import org.onap.so.db.catalog.beans.HeatEnvironment;
37 import org.onap.so.db.catalog.beans.HeatFiles;
38 import org.onap.so.db.catalog.beans.HeatTemplate;
39 import org.onap.so.db.catalog.beans.VfModule;
40 import org.onap.so.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;
44
45 @RunWith(PowerMockRunner.class)
46 @PrepareForTest({CatalogDatabase.class})
47 public class VfModuleCustomizationToVduMapperTest {
48
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";
62
63     private VfModuleCustomizationToVduMapper testedObject;
64     private CatalogDatabase catalogDatabaseMock;
65
66     @Before
67     public void init() {
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());
74     }
75
76     @Test
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());
84     }
85
86     @Test
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());
94     }
95
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;
106     }
107
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);
113         return heatTemplate;
114     }
115
116     private Map<String, Object> createNestedTemplates() {
117         Map<String, Object> map = new HashMap<>();
118         map.put(NESTED_TEMPLATE_KEY, NESTED_TEMPLATE_VALUE);
119         return map;
120     }
121
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);
128         return map;
129     }
130
131     private HeatEnvironment createHeatEnvironment() {
132         HeatEnvironment heatEnvironment = new HeatEnvironment();
133         heatEnvironment.setName(HEAT_ENV_NAME);
134         heatEnvironment.setEnvironment(HEAT_ENV);
135         return heatEnvironment;
136     }
137
138
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;
148     }
149
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);
155         return vduArtifact;
156     }
157
158 }