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.times;
29 import static org.mockito.Mockito.verify;
32 import java.io.IOException;
33 import java.util.HashMap;
35 import java.util.Optional;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.InjectMocks;
41 import org.mockito.Mock;
42 import org.mockito.Mockito;
43 import org.mockito.MockitoAnnotations;
44 import org.mockito.Spy;
45 import org.mockito.runners.MockitoJUnitRunner;
46 import org.onap.so.TestDataSetup;
47 import org.onap.so.cloud.CloudConfig;
48 import org.onap.so.cloud.CloudSite;
49 import org.onap.so.openstack.beans.HeatStatus;
50 import org.onap.so.openstack.beans.StackInfo;
51 import org.onap.so.openstack.exceptions.MsoException;
52 import org.springframework.core.env.Environment;
54 import com.fasterxml.jackson.core.JsonParseException;
55 import com.fasterxml.jackson.databind.JsonMappingException;
56 import com.woorea.openstack.base.client.OpenStackRequest;
57 import com.woorea.openstack.heat.Heat;
58 import com.woorea.openstack.heat.StackResource;
59 import com.woorea.openstack.heat.StackResource.UpdateStack;
60 import com.woorea.openstack.heat.model.Stack;
61 import com.woorea.openstack.heat.model.UpdateStackParam;
63 @RunWith(MockitoJUnitRunner.class)
64 public class MsoHeatUtilsWithUpdateTest extends TestDataSetup {
66 private CloudConfig cloudConfig;
69 private Environment environment;
73 private MsoHeatUtilsWithUpdate heatUtils;
75 private String cloudSiteId;
76 private String tenantId;
77 private String stackName;
78 private String heatTemplate;
79 private Map<String, Object> stackInputs;
80 private boolean pollForCompletion;
81 private int timeoutMinutes;
84 public void before() {
85 MockitoAnnotations.initMocks(this);
87 cloudSiteId = "cloudSiteId";
88 tenantId = "tenantId";
89 stackName = "stackName";
90 heatTemplate = "heatTemplate";
91 stackInputs = new HashMap<>();
92 pollForCompletion = true;
97 public void updateStackTest() throws MsoException, JsonParseException, JsonMappingException, IOException {
98 CloudSite cloudSite = mapper.readValue(new File(RESOURCE_PATH + "CloudSite.json"), CloudSite.class);
99 Heat heatClient = new Heat("endpoint");
100 Stack heatStack = mapper.readValue(new File(RESOURCE_PATH + "HeatStack.json"), Stack.class);
101 Stack updateStack = mapper.readValue(new File(RESOURCE_PATH + "UpdateStack.json"), Stack.class);
103 StackInfo expectedStackInfo = new StackInfo("stackName", HeatStatus.UPDATED, "stackStatusReason", null);
104 expectedStackInfo.setCanonicalName("stackName/id");
106 doReturn(Optional.of(cloudSite)).when(cloudConfig).getCloudSite(isA(String.class));
107 doReturn(heatClient).when(heatUtils).getHeatClient(isA(CloudSite.class), isA(String.class));
108 doReturn(heatStack).when(heatUtils).queryHeatStack(isA(Heat.class), isA(String.class));
109 doReturn(null).when(heatUtils).executeAndRecordOpenstackRequest(isA(OpenStackRequest.class));
110 doReturn("0").when(environment).getProperty(isA(String.class), isA(String.class));
111 doReturn(updateStack).when(heatUtils).queryHeatStack(isA(Heat.class), isA(String.class));
113 StackInfo actualStackInfo = heatUtils.updateStack(cloudSiteId, tenantId, stackName,
114 heatTemplate, stackInputs, pollForCompletion, timeoutMinutes);
116 assertThat(actualStackInfo, sameBeanAs(expectedStackInfo));
120 public void updateStackWithEnvironmentTest() throws JsonParseException, JsonMappingException, IOException, MsoException {
121 String environmentString = "environmentString";
123 CloudSite cloudSite = mapper.readValue(new File(RESOURCE_PATH + "CloudSite.json"), CloudSite.class);
124 Heat heatClient = new Heat("endpoint");
125 Stack heatStack = mapper.readValue(new File(RESOURCE_PATH + "HeatStack.json"), Stack.class);
126 Stack updateStack = mapper.readValue(new File(RESOURCE_PATH + "UpdateStack.json"), Stack.class);
128 StackInfo expectedStackInfo = new StackInfo("stackName", HeatStatus.UPDATED, "stackStatusReason", null);
129 expectedStackInfo.setCanonicalName("stackName/id");
131 doReturn(Optional.of(cloudSite)).when(cloudConfig).getCloudSite(isA(String.class));
132 doReturn(heatClient).when(heatUtils).getHeatClient(isA(CloudSite.class), isA(String.class));
133 doReturn(heatStack).when(heatUtils).queryHeatStack(isA(Heat.class), isA(String.class));
134 doReturn(null).when(heatUtils).executeAndRecordOpenstackRequest(isA(OpenStackRequest.class));
135 doReturn("0").when(environment).getProperty(isA(String.class), isA(String.class));
136 doReturn(updateStack).when(heatUtils).queryHeatStack(isA(Heat.class), isA(String.class));
138 StackInfo actualStackInfo = heatUtils.updateStack(cloudSiteId, tenantId, stackName,
139 heatTemplate, stackInputs, pollForCompletion, timeoutMinutes, environmentString);
141 assertThat(actualStackInfo, sameBeanAs(expectedStackInfo));
145 public void updateStackWithFilesTest() throws MsoException, JsonParseException, JsonMappingException, IOException {
146 String environmentString = "environmentString";
147 Map<String, Object> files = new HashMap<>();
148 files.put("file1", new Object());
150 CloudSite cloudSite = mapper.readValue(new File(RESOURCE_PATH + "CloudSite.json"), CloudSite.class);
151 Heat heatClient = new Heat("endpoint");
152 Stack heatStack = mapper.readValue(new File(RESOURCE_PATH + "HeatStack.json"), Stack.class);
153 Stack updateStack = mapper.readValue(new File(RESOURCE_PATH + "UpdateStack.json"), Stack.class);
155 StackInfo expectedStackInfo = new StackInfo("stackName", HeatStatus.UPDATED, "stackStatusReason", null);
156 expectedStackInfo.setCanonicalName("stackName/id");
158 doReturn(Optional.of(cloudSite)).when(cloudConfig).getCloudSite(isA(String.class));
159 doReturn(heatClient).when(heatUtils).getHeatClient(isA(CloudSite.class), isA(String.class));
160 doReturn(heatStack).when(heatUtils).queryHeatStack(isA(Heat.class), isA(String.class));
161 doReturn(null).when(heatUtils).executeAndRecordOpenstackRequest(isA(OpenStackRequest.class));
162 doReturn("0").when(environment).getProperty(isA(String.class), isA(String.class));
163 doReturn(updateStack).when(heatUtils).queryHeatStack(isA(Heat.class), isA(String.class));
165 StackInfo actualStackInfo = heatUtils.updateStack(cloudSiteId, tenantId, stackName,
166 heatTemplate, stackInputs, pollForCompletion, timeoutMinutes , environmentString, files);
168 assertThat(actualStackInfo, sameBeanAs(expectedStackInfo));