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(heatClient).when(heatUtils).getHeatClient(isA(String.class), isA(String.class));
99 doReturn(null).when(heatUtils).executeAndRecordOpenstackRequest(isA(OpenStackRequest.class));
100 doReturn("0").when(environment).getProperty(isA(String.class), isA(String.class));
101 doReturn(updateStack).when(heatUtils).queryHeatStack(isA(Heat.class), isA(String.class));
103 StackInfo actualStackInfo = heatUtils.updateStack(cloudSiteId, cloudOwner, tenantId, stackName, heatTemplate,
104 stackInputs, pollForCompletion, timeoutMinutes);
106 assertThat(actualStackInfo, sameBeanAs(expectedStackInfo));
110 public void updateStackWithEnvironmentTest()
111 throws JsonParseException, JsonMappingException, IOException, MsoException {
112 String environmentString = "environmentString";
114 CloudSite cloudSite = new CloudSite();
115 Heat heatClient = new Heat("endpoint");
116 Stack heatStack = mapper.readValue(new File(RESOURCE_PATH + "HeatStack.json"), Stack.class);
117 Stack updateStack = mapper.readValue(new File(RESOURCE_PATH + "UpdateStack.json"), Stack.class);
119 StackInfo expectedStackInfo = new StackInfo("stackName", HeatStatus.UPDATED, "stackStatusReason", null);
120 expectedStackInfo.setCanonicalName("stackName/id");
122 doReturn(heatClient).when(heatUtils).getHeatClient(isA(String.class), isA(String.class));
124 doReturn(null).when(heatUtils).executeAndRecordOpenstackRequest(isA(OpenStackRequest.class));
125 doReturn("0").when(environment).getProperty(isA(String.class), isA(String.class));
126 doReturn(updateStack).when(heatUtils).queryHeatStack(isA(Heat.class), isA(String.class));
128 StackInfo actualStackInfo = heatUtils.updateStack(cloudSiteId, cloudOwner, tenantId, stackName, heatTemplate,
129 stackInputs, pollForCompletion, timeoutMinutes, environmentString);
131 assertThat(actualStackInfo, sameBeanAs(expectedStackInfo));
135 public void updateStackWithFilesTest() throws MsoException, JsonParseException, JsonMappingException, IOException {
136 String environmentString = "environmentString";
137 Map<String, Object> files = new HashMap<>();
138 files.put("file1", new Object());
140 CloudSite cloudSite = new CloudSite();
141 Heat heatClient = new Heat("endpoint");
142 Stack heatStack = mapper.readValue(new File(RESOURCE_PATH + "HeatStack.json"), Stack.class);
143 Stack updateStack = mapper.readValue(new File(RESOURCE_PATH + "UpdateStack.json"), Stack.class);
145 StackInfo expectedStackInfo = new StackInfo("stackName", HeatStatus.UPDATED, "stackStatusReason", null);
146 expectedStackInfo.setCanonicalName("stackName/id");
148 doReturn(heatClient).when(heatUtils).getHeatClient(isA(String.class), isA(String.class));
149 doReturn(null).when(heatUtils).executeAndRecordOpenstackRequest(isA(OpenStackRequest.class));
150 doReturn("0").when(environment).getProperty(isA(String.class), isA(String.class));
151 doReturn(updateStack).when(heatUtils).queryHeatStack(isA(Heat.class), isA(String.class));
153 StackInfo actualStackInfo = heatUtils.updateStack(cloudSiteId, cloudOwner, tenantId, stackName, heatTemplate,
154 stackInputs, pollForCompletion, timeoutMinutes, environmentString, files);
156 assertThat(actualStackInfo, sameBeanAs(expectedStackInfo));