2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2018 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 com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
24 import static org.junit.Assert.assertThat;
25 import static org.mockito.Matchers.any;
26 import static org.mockito.Matchers.isA;
27 import static org.mockito.Mockito.doReturn;
28 import static org.mockito.Mockito.verify;
31 import java.io.IOException;
32 import java.util.HashMap;
34 import java.util.Optional;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.InjectMocks;
40 import org.mockito.Mock;
41 import org.mockito.MockitoAnnotations;
42 import org.mockito.Spy;
43 import org.mockito.runners.MockitoJUnitRunner;
44 import org.onap.so.TestDataSetup;
45 import org.onap.so.cloud.CloudConfig;
46 import org.onap.so.db.catalog.beans.CloudSite;
47 import org.onap.so.openstack.beans.HeatStatus;
48 import org.onap.so.openstack.beans.StackInfo;
49 import org.onap.so.openstack.exceptions.MsoException;
50 import org.springframework.core.env.Environment;
52 import com.fasterxml.jackson.core.JsonParseException;
53 import com.fasterxml.jackson.databind.JsonMappingException;
54 import com.woorea.openstack.base.client.OpenStackRequest;
55 import com.woorea.openstack.heat.Heat;
56 import com.woorea.openstack.heat.model.Stack;
58 @RunWith(MockitoJUnitRunner.class)
59 public class MsoHeatUtilsWithUpdateTest extends TestDataSetup {
61 private CloudConfig cloudConfig;
64 private Environment environment;
68 private MsoHeatUtilsWithUpdate heatUtils;
70 private String cloudSiteId;
71 private String tenantId;
72 private String stackName;
73 private String heatTemplate;
74 private Map<String, Object> stackInputs;
75 private boolean pollForCompletion;
76 private int timeoutMinutes;
79 public void before() {
80 MockitoAnnotations.initMocks(this);
82 cloudSiteId = "cloudSiteId";
83 tenantId = "tenantId";
84 stackName = "stackName";
85 heatTemplate = "heatTemplate";
86 stackInputs = new HashMap<>();
87 pollForCompletion = true;
92 public void updateStackTest() throws MsoException, JsonParseException, JsonMappingException, IOException {
93 CloudSite cloudSite = new CloudSite();
94 Heat heatClient = new Heat("endpoint");
95 Stack heatStack = mapper.readValue(new File(RESOURCE_PATH + "HeatStack.json"), Stack.class);
96 Stack updateStack = mapper.readValue(new File(RESOURCE_PATH + "UpdateStack.json"), Stack.class);
98 StackInfo expectedStackInfo = new StackInfo("stackName", HeatStatus.UPDATED, "stackStatusReason", null);
99 expectedStackInfo.setCanonicalName("stackName/id");
101 doReturn(Optional.of(cloudSite)).when(cloudConfig).getCloudSite(isA(String.class));
102 doReturn(heatClient).when(heatUtils).getHeatClient(isA(CloudSite.class), isA(String.class));
103 doReturn(heatStack).when(heatUtils).queryHeatStack(isA(Heat.class), isA(String.class));
104 doReturn(null).when(heatUtils).executeAndRecordOpenstackRequest(isA(OpenStackRequest.class));
105 doReturn("0").when(environment).getProperty(isA(String.class), isA(String.class));
106 doReturn(updateStack).when(heatUtils).queryHeatStack(isA(Heat.class), isA(String.class));
108 StackInfo actualStackInfo = heatUtils.updateStack(cloudSiteId, tenantId, stackName,
109 heatTemplate, stackInputs, pollForCompletion, timeoutMinutes);
111 assertThat(actualStackInfo, sameBeanAs(expectedStackInfo));
115 public void updateStackWithEnvironmentTest() throws JsonParseException, JsonMappingException, IOException, MsoException {
116 String environmentString = "environmentString";
118 CloudSite cloudSite = new CloudSite();
119 Heat heatClient = new Heat("endpoint");
120 Stack heatStack = mapper.readValue(new File(RESOURCE_PATH + "HeatStack.json"), Stack.class);
121 Stack updateStack = mapper.readValue(new File(RESOURCE_PATH + "UpdateStack.json"), Stack.class);
123 StackInfo expectedStackInfo = new StackInfo("stackName", HeatStatus.UPDATED, "stackStatusReason", null);
124 expectedStackInfo.setCanonicalName("stackName/id");
126 doReturn(Optional.of(cloudSite)).when(cloudConfig).getCloudSite(isA(String.class));
127 doReturn(heatClient).when(heatUtils).getHeatClient(isA(CloudSite.class), isA(String.class));
128 doReturn(heatStack).when(heatUtils).queryHeatStack(isA(Heat.class), isA(String.class));
129 doReturn(null).when(heatUtils).executeAndRecordOpenstackRequest(isA(OpenStackRequest.class));
130 doReturn("0").when(environment).getProperty(isA(String.class), isA(String.class));
131 doReturn(updateStack).when(heatUtils).queryHeatStack(isA(Heat.class), isA(String.class));
133 StackInfo actualStackInfo = heatUtils.updateStack(cloudSiteId, tenantId, stackName,
134 heatTemplate, stackInputs, pollForCompletion, timeoutMinutes, environmentString);
136 assertThat(actualStackInfo, sameBeanAs(expectedStackInfo));
140 public void updateStackWithFilesTest() throws MsoException, JsonParseException, JsonMappingException, IOException {
141 String environmentString = "environmentString";
142 Map<String, Object> files = new HashMap<>();
143 files.put("file1", new Object());
145 CloudSite cloudSite = new CloudSite();
146 Heat heatClient = new Heat("endpoint");
147 Stack heatStack = mapper.readValue(new File(RESOURCE_PATH + "HeatStack.json"), Stack.class);
148 Stack updateStack = mapper.readValue(new File(RESOURCE_PATH + "UpdateStack.json"), Stack.class);
150 StackInfo expectedStackInfo = new StackInfo("stackName", HeatStatus.UPDATED, "stackStatusReason", null);
151 expectedStackInfo.setCanonicalName("stackName/id");
153 doReturn(Optional.of(cloudSite)).when(cloudConfig).getCloudSite(isA(String.class));
154 doReturn(heatClient).when(heatUtils).getHeatClient(isA(CloudSite.class), isA(String.class));
155 doReturn(heatStack).when(heatUtils).queryHeatStack(isA(Heat.class), isA(String.class));
156 doReturn(null).when(heatUtils).executeAndRecordOpenstackRequest(isA(OpenStackRequest.class));
157 doReturn("0").when(environment).getProperty(isA(String.class), isA(String.class));
158 doReturn(updateStack).when(heatUtils).queryHeatStack(isA(Heat.class), isA(String.class));
160 StackInfo actualStackInfo = heatUtils.updateStack(cloudSiteId, tenantId, stackName,
161 heatTemplate, stackInputs, pollForCompletion, timeoutMinutes , environmentString, files);
163 assertThat(actualStackInfo, sameBeanAs(expectedStackInfo));