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