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.ArgumentMatchers.isA;
26 import static org.mockito.Mockito.doReturn;
28 import java.io.IOException;
29 import java.util.HashMap;
31 import java.util.Optional;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.InjectMocks;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
38 import org.mockito.Spy;
39 import org.mockito.junit.MockitoJUnitRunner;
40 import org.onap.so.TestDataSetup;
41 import org.onap.so.cloud.CloudConfig;
42 import org.onap.so.db.catalog.beans.CloudSite;
43 import org.onap.so.openstack.beans.HeatStatus;
44 import org.onap.so.openstack.beans.StackInfo;
45 import org.onap.so.openstack.exceptions.MsoException;
46 import org.springframework.core.env.Environment;
47 import com.fasterxml.jackson.core.JsonParseException;
48 import com.fasterxml.jackson.databind.JsonMappingException;
49 import com.woorea.openstack.base.client.OpenStackRequest;
50 import com.woorea.openstack.heat.Heat;
51 import com.woorea.openstack.heat.model.Stack;
53 @RunWith(MockitoJUnitRunner.class)
54 public class MsoHeatUtilsWithUpdateTest extends TestDataSetup {
56 private CloudConfig cloudConfig;
59 private Environment environment;
63 private MsoHeatUtilsWithUpdate heatUtils;
65 private String cloudOwner;
66 private String cloudSiteId;
67 private String tenantId;
68 private String stackName;
69 private String heatTemplate;
70 private Map<String, Object> stackInputs;
71 private boolean pollForCompletion;
72 private int timeoutMinutes;
75 public void before() {
76 MockitoAnnotations.initMocks(this);
78 cloudOwner = "cloudOwner";
79 cloudSiteId = "cloudSiteId";
80 tenantId = "tenantId";
81 stackName = "stackName";
82 heatTemplate = "heatTemplate";
83 stackInputs = new HashMap<>();
84 pollForCompletion = true;
89 public void updateStackTest() throws MsoException, JsonParseException, JsonMappingException, IOException {
90 CloudSite cloudSite = new CloudSite();
91 Heat heatClient = new Heat("endpoint");
92 Stack heatStack = mapper.readValue(new File(RESOURCE_PATH + "HeatStack.json"), Stack.class);
93 Stack updateStack = mapper.readValue(new File(RESOURCE_PATH + "UpdateStack.json"), Stack.class);
95 StackInfo expectedStackInfo = new StackInfo("stackName", HeatStatus.UPDATED, "stackStatusReason", null);
96 expectedStackInfo.setCanonicalName("stackName/id");
98 doReturn(Optional.of(cloudSite)).when(cloudConfig).getCloudSite(isA(String.class));
99 doReturn(heatClient).when(heatUtils).getHeatClient(isA(String.class), isA(String.class));
100 doReturn(null).when(heatUtils).executeAndRecordOpenstackRequest(isA(OpenStackRequest.class));
101 doReturn("0").when(environment).getProperty(isA(String.class), isA(String.class));
102 doReturn(updateStack).when(heatUtils).queryHeatStack(isA(Heat.class), isA(String.class));
104 StackInfo actualStackInfo = heatUtils.updateStack(cloudSiteId, cloudOwner, tenantId, stackName, heatTemplate,
105 stackInputs, pollForCompletion, timeoutMinutes);
107 assertThat(actualStackInfo, sameBeanAs(expectedStackInfo));
111 public void updateStackWithEnvironmentTest()
112 throws JsonParseException, JsonMappingException, IOException, MsoException {
113 String environmentString = "environmentString";
115 CloudSite cloudSite = new CloudSite();
116 Heat heatClient = new Heat("endpoint");
117 Stack heatStack = mapper.readValue(new File(RESOURCE_PATH + "HeatStack.json"), Stack.class);
118 Stack updateStack = mapper.readValue(new File(RESOURCE_PATH + "UpdateStack.json"), Stack.class);
120 StackInfo expectedStackInfo = new StackInfo("stackName", HeatStatus.UPDATED, "stackStatusReason", null);
121 expectedStackInfo.setCanonicalName("stackName/id");
123 doReturn(Optional.of(cloudSite)).when(cloudConfig).getCloudSite(isA(String.class));
124 doReturn(heatClient).when(heatUtils).getHeatClient(isA(String.class), isA(String.class));
126 doReturn(null).when(heatUtils).executeAndRecordOpenstackRequest(isA(OpenStackRequest.class));
127 doReturn("0").when(environment).getProperty(isA(String.class), isA(String.class));
128 doReturn(updateStack).when(heatUtils).queryHeatStack(isA(Heat.class), isA(String.class));
130 StackInfo actualStackInfo = heatUtils.updateStack(cloudSiteId, cloudOwner, tenantId, stackName, heatTemplate,
131 stackInputs, pollForCompletion, timeoutMinutes, environmentString);
133 assertThat(actualStackInfo, sameBeanAs(expectedStackInfo));
137 public void updateStackWithFilesTest() throws MsoException, JsonParseException, JsonMappingException, IOException {
138 String environmentString = "environmentString";
139 Map<String, Object> files = new HashMap<>();
140 files.put("file1", new Object());
142 CloudSite cloudSite = new CloudSite();
143 Heat heatClient = new Heat("endpoint");
144 Stack heatStack = mapper.readValue(new File(RESOURCE_PATH + "HeatStack.json"), Stack.class);
145 Stack updateStack = mapper.readValue(new File(RESOURCE_PATH + "UpdateStack.json"), Stack.class);
147 StackInfo expectedStackInfo = new StackInfo("stackName", HeatStatus.UPDATED, "stackStatusReason", null);
148 expectedStackInfo.setCanonicalName("stackName/id");
150 doReturn(Optional.of(cloudSite)).when(cloudConfig).getCloudSite(isA(String.class));
151 doReturn(heatClient).when(heatUtils).getHeatClient(isA(String.class), isA(String.class));
152 doReturn(null).when(heatUtils).executeAndRecordOpenstackRequest(isA(OpenStackRequest.class));
153 doReturn("0").when(environment).getProperty(isA(String.class), isA(String.class));
154 doReturn(updateStack).when(heatUtils).queryHeatStack(isA(Heat.class), isA(String.class));
156 StackInfo actualStackInfo = heatUtils.updateStack(cloudSiteId, cloudOwner, tenantId, stackName, heatTemplate,
157 stackInputs, pollForCompletion, timeoutMinutes, environmentString, files);
159 assertThat(actualStackInfo, sameBeanAs(expectedStackInfo));