Containerization feature of SO
[so.git] / adapters / mso-adapter-utils / src / test / java / org / onap / so / openstack / utils / MsoHeatEnvironmentEntryTest.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.openstack.utils;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertTrue;
28
29 import java.io.File;
30 import java.io.IOException;
31 import java.util.Arrays;
32 import java.util.HashSet;
33
34 import org.junit.Test;
35 import org.onap.so.TestDataSetup;
36 import org.onap.so.db.catalog.beans.HeatTemplateParam;
37
38 import com.fasterxml.jackson.core.JsonParseException;
39 import com.fasterxml.jackson.databind.JsonMappingException;
40
41 public class MsoHeatEnvironmentEntryTest extends TestDataSetup {
42
43     private static final String PARAMETER_NAME = "keyTest";
44     private static final String VALUE_NAME = "valueTest";
45     private static final String NOT_EXISTING_PARAM = "notExistingParam";
46     private static final String RAW_ENTRY_WITH_NO_RESOURCE_REGISTRY = "parameters: {"
47             + PARAMETER_NAME + ": " + VALUE_NAME + "}";
48     private static final String RAW_ENTRY_WITH_RESOURCE_REGISTRY = "resource_registry: resourceTest";
49     private static final String RAW_ENTRY_INVALID = "invalidRawEntry";
50
51     @Test
52     public void createObjectWithNullStringBuilder() {
53         MsoHeatEnvironmentEntry testedObject = new MsoHeatEnvironmentEntry(null);
54         assertThat(testedObject.isValid()).isTrue();
55         assertThat(testedObject.getRawEntry()).isNull();
56         assertThat(testedObject.containsParameter(PARAMETER_NAME)).isFalse();
57     }
58
59     @Test
60     public void toFullString_ResourceRegistryNotPresentInRawEntry() throws JsonParseException, JsonMappingException, IOException {
61         StringBuilder sb = new StringBuilder(RAW_ENTRY_WITH_NO_RESOURCE_REGISTRY);
62         
63         MsoHeatEnvironmentEntry testedObject = new MsoHeatEnvironmentEntry(sb);
64         
65         HeatTemplateParam heatTemplateParam = mapper.readValue(new File(RESOURCE_PATH + "HeatTemplateParam.json"), HeatTemplateParam.class);
66         
67         assertThat(testedObject.getRawEntry()).isEqualTo(sb);
68         assertThat(testedObject.isValid()).isTrue();
69         assertThat(testedObject.containsParameter(PARAMETER_NAME)).isTrue();
70         assertThat(testedObject.toString()).doesNotContain(RAW_ENTRY_WITH_RESOURCE_REGISTRY);
71         assertTrue(testedObject.containsParameter(PARAMETER_NAME, "dummyAlias"));
72         assertTrue(testedObject.containsParameter("dummyName", PARAMETER_NAME));
73         assertFalse(testedObject.containsParameter("dummyName", "dummyAlias"));
74         assertEquals("parameters:\n   " + PARAMETER_NAME + ":  " + VALUE_NAME + "\n\n\n", testedObject.toFullString().toString());
75         assertEquals("parameters:\n  " + PARAMETER_NAME + ": " + VALUE_NAME + "\n\n\n", testedObject.toFullStringExcludeNonParams(new HashSet<HeatTemplateParam>(Arrays.asList(heatTemplateParam))).toString());
76         assertEquals(1, testedObject.getNumberOfParameters());
77         assertFalse(testedObject.hasResources());
78         
79         MsoHeatEnvironmentResource heatResource = new MsoHeatEnvironmentResource("resourceName", "resourceValue");
80         MsoHeatEnvironmentParameter heatParameter = new MsoHeatEnvironmentParameter("parameterName", "parameterValue");
81         testedObject.addResource(heatResource);
82         testedObject.addParameter(heatParameter);
83         assertEquals(1, testedObject.getNumberOfResources());
84         assertEquals(2, testedObject.getNumberOfParameters());
85         
86         testedObject.setResources(null);
87         testedObject.setParameters(null);
88         assertNull(testedObject.getParameters());
89         assertNull(testedObject.getResources());
90     }
91
92     @Test
93     public void toFullString_ExceptionOccurred() {
94         StringBuilder sb = new StringBuilder(RAW_ENTRY_INVALID);
95         MsoHeatEnvironmentEntry testedObject = new MsoHeatEnvironmentEntry(sb);
96         assertThat(testedObject.getRawEntry()).isEqualTo(sb);
97         assertThat(testedObject.isValid()).isFalse();
98         assertThat(testedObject.getErrorString()).isNotNull().isNotEmpty();
99     }
100
101     @Test
102     public void checkIfContainsTheParameter() {
103         StringBuilder sb = new StringBuilder(RAW_ENTRY_WITH_NO_RESOURCE_REGISTRY);
104         MsoHeatEnvironmentEntry testedObject = new MsoHeatEnvironmentEntry(sb);
105         assertThat(testedObject.getRawEntry()).isEqualTo(sb);
106         assertThat(testedObject.isValid()).isTrue();
107         assertThat(testedObject.containsParameter(PARAMETER_NAME)).isTrue();
108         assertThat(testedObject.containsParameter(NOT_EXISTING_PARAM)).isFalse();
109     }
110 }