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 org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.InjectMocks;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.mockito.Spy;
38 import org.mockito.junit.MockitoJUnitRunner;
39 import org.onap.so.TestDataSetup;
40 import org.onap.so.cloud.CloudConfig;
41 import org.onap.so.db.catalog.beans.CloudSite;
42 import org.onap.so.openstack.beans.HeatStatus;
43 import org.onap.so.openstack.beans.StackInfo;
44 import org.onap.so.openstack.exceptions.MsoException;
45 import org.springframework.core.env.Environment;
46 import com.fasterxml.jackson.core.JsonParseException;
47 import com.fasterxml.jackson.databind.JsonMappingException;
48 import com.woorea.openstack.base.client.OpenStackRequest;
49 import com.woorea.openstack.heat.Heat;
50 import com.woorea.openstack.heat.model.Stack;
52 @RunWith(MockitoJUnitRunner.class)
53 public class MsoHeatUtilsWithUpdateTest extends TestDataSetup {
55 private CloudConfig cloudConfig;
58 private Environment environment;
62 private MsoHeatUtilsWithUpdate heatUtils;
64 private String cloudOwner;
65 private String cloudSiteId;
66 private String tenantId;
67 private String stackName;
68 private String heatTemplate;
69 private Map<String, Object> stackInputs;
70 private boolean pollForCompletion;
71 private int timeoutMinutes;
74 public void before() {
75 MockitoAnnotations.initMocks(this);
77 cloudOwner = "cloudOwner";
78 cloudSiteId = "cloudSiteId";
79 tenantId = "tenantId";
80 stackName = "stackName";
81 heatTemplate = "heatTemplate";
82 stackInputs = new HashMap<>();
83 pollForCompletion = true;
88 public void updateStackTest() throws MsoException, JsonParseException, JsonMappingException, IOException {
89 CloudSite cloudSite = new CloudSite();
90 Heat heatClient = new Heat("endpoint");
91 Stack heatStack = mapper.readValue(new File(RESOURCE_PATH + "HeatStack.json"), Stack.class);
92 Stack updateStack = mapper.readValue(new File(RESOURCE_PATH + "UpdateStack.json"), Stack.class);
94 StackInfo expectedStackInfo = new StackInfo("stackName", HeatStatus.UPDATED, "stackStatusReason", null);
95 expectedStackInfo.setCanonicalName("stackName/id");
97 doReturn(heatClient).when(heatUtils).getHeatClient(isA(String.class), isA(String.class));
98 doReturn(null).when(heatUtils).executeAndRecordOpenstackRequest(isA(OpenStackRequest.class));
99 doReturn("0").when(environment).getProperty(isA(String.class), isA(String.class));
100 doReturn(updateStack).when(heatUtils).queryHeatStack(isA(Heat.class), isA(String.class));
102 StackInfo actualStackInfo = heatUtils.updateStack(cloudSiteId, cloudOwner, tenantId, stackName, heatTemplate,
103 stackInputs, pollForCompletion, timeoutMinutes);
105 assertThat(actualStackInfo, sameBeanAs(expectedStackInfo));
109 public void updateStackWithEnvironmentTest()
110 throws JsonParseException, JsonMappingException, IOException, MsoException {
111 String environmentString = "environmentString";
113 CloudSite cloudSite = new CloudSite();
114 Heat heatClient = new Heat("endpoint");
115 Stack heatStack = mapper.readValue(new File(RESOURCE_PATH + "HeatStack.json"), Stack.class);
116 Stack updateStack = mapper.readValue(new File(RESOURCE_PATH + "UpdateStack.json"), Stack.class);
118 StackInfo expectedStackInfo = new StackInfo("stackName", HeatStatus.UPDATED, "stackStatusReason", null);
119 expectedStackInfo.setCanonicalName("stackName/id");
121 doReturn(heatClient).when(heatUtils).getHeatClient(isA(String.class), isA(String.class));
123 doReturn(null).when(heatUtils).executeAndRecordOpenstackRequest(isA(OpenStackRequest.class));
124 doReturn("0").when(environment).getProperty(isA(String.class), isA(String.class));
125 doReturn(updateStack).when(heatUtils).queryHeatStack(isA(Heat.class), isA(String.class));
127 StackInfo actualStackInfo = heatUtils.updateStack(cloudSiteId, cloudOwner, tenantId, stackName, heatTemplate,
128 stackInputs, pollForCompletion, timeoutMinutes, environmentString);
130 assertThat(actualStackInfo, sameBeanAs(expectedStackInfo));
134 public void updateStackWithFilesTest() throws MsoException, JsonParseException, JsonMappingException, IOException {
135 String environmentString = "environmentString";
136 Map<String, Object> files = new HashMap<>();
137 files.put("file1", new Object());
139 CloudSite cloudSite = new CloudSite();
140 Heat heatClient = new Heat("endpoint");
141 Stack heatStack = mapper.readValue(new File(RESOURCE_PATH + "HeatStack.json"), Stack.class);
142 Stack updateStack = mapper.readValue(new File(RESOURCE_PATH + "UpdateStack.json"), Stack.class);
144 StackInfo expectedStackInfo = new StackInfo("stackName", HeatStatus.UPDATED, "stackStatusReason", null);
145 expectedStackInfo.setCanonicalName("stackName/id");
147 doReturn(heatClient).when(heatUtils).getHeatClient(isA(String.class), isA(String.class));
148 doReturn(null).when(heatUtils).executeAndRecordOpenstackRequest(isA(OpenStackRequest.class));
149 doReturn("0").when(environment).getProperty(isA(String.class), isA(String.class));
150 doReturn(updateStack).when(heatUtils).queryHeatStack(isA(Heat.class), isA(String.class));
152 StackInfo actualStackInfo = heatUtils.updateStack(cloudSiteId, cloudOwner, tenantId, stackName, heatTemplate,
153 stackInputs, pollForCompletion, timeoutMinutes, environmentString, files);
155 assertThat(actualStackInfo, sameBeanAs(expectedStackInfo));