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.woorea.openstack.base.client.OpenStackRequest;
47 import com.woorea.openstack.heat.Heat;
48 import com.woorea.openstack.heat.model.Stack;
50 @RunWith(MockitoJUnitRunner.class)
51 public class MsoHeatUtilsWithUpdateTest extends TestDataSetup {
53 private CloudConfig cloudConfig;
56 private Environment environment;
60 private MsoHeatUtilsWithUpdate heatUtils;
62 private String cloudOwner;
63 private String cloudSiteId;
64 private String tenantId;
65 private String stackName;
66 private String heatTemplate;
67 private Map<String, Object> stackInputs;
68 private boolean pollForCompletion;
69 private int timeoutMinutes;
72 public void before() {
73 MockitoAnnotations.initMocks(this);
75 cloudOwner = "cloudOwner";
76 cloudSiteId = "cloudSiteId";
77 tenantId = "tenantId";
78 stackName = "stackName";
79 heatTemplate = "heatTemplate";
80 stackInputs = new HashMap<>();
81 pollForCompletion = true;
86 public void updateStackTest() throws MsoException, IOException {
87 CloudSite cloudSite = new CloudSite();
88 Heat heatClient = new Heat("endpoint");
89 Stack heatStack = mapper.readValue(new File(RESOURCE_PATH + "HeatStack.json"), Stack.class);
90 Stack updateStack = mapper.readValue(new File(RESOURCE_PATH + "UpdateStack.json"), Stack.class);
92 StackInfo expectedStackInfo = new StackInfo("stackName", HeatStatus.UPDATED, "stackStatusReason", null);
93 expectedStackInfo.setCanonicalName("stackName/id");
95 doReturn(heatClient).when(heatUtils).getHeatClient(isA(String.class), isA(String.class));
96 doReturn(null).when(heatUtils).executeAndRecordOpenstackRequest(isA(OpenStackRequest.class));
97 doReturn("0").when(environment).getProperty(isA(String.class), isA(String.class));
98 doReturn(updateStack).when(heatUtils).queryHeatStack(isA(Heat.class), isA(String.class));
100 StackInfo actualStackInfo = heatUtils.updateStack(cloudSiteId, cloudOwner, tenantId, stackName, heatTemplate,
101 stackInputs, pollForCompletion, timeoutMinutes);
103 assertThat(actualStackInfo, sameBeanAs(expectedStackInfo));
107 public void updateStackWithEnvironmentTest() throws IOException, MsoException {
108 String environmentString = "environmentString";
110 CloudSite cloudSite = new CloudSite();
111 Heat heatClient = new Heat("endpoint");
112 Stack heatStack = mapper.readValue(new File(RESOURCE_PATH + "HeatStack.json"), Stack.class);
113 Stack updateStack = mapper.readValue(new File(RESOURCE_PATH + "UpdateStack.json"), Stack.class);
115 StackInfo expectedStackInfo = new StackInfo("stackName", HeatStatus.UPDATED, "stackStatusReason", null);
116 expectedStackInfo.setCanonicalName("stackName/id");
118 doReturn(heatClient).when(heatUtils).getHeatClient(isA(String.class), isA(String.class));
120 doReturn(null).when(heatUtils).executeAndRecordOpenstackRequest(isA(OpenStackRequest.class));
121 doReturn("0").when(environment).getProperty(isA(String.class), isA(String.class));
122 doReturn(updateStack).when(heatUtils).queryHeatStack(isA(Heat.class), isA(String.class));
124 StackInfo actualStackInfo = heatUtils.updateStack(cloudSiteId, cloudOwner, tenantId, stackName, heatTemplate,
125 stackInputs, pollForCompletion, timeoutMinutes, environmentString);
127 assertThat(actualStackInfo, sameBeanAs(expectedStackInfo));
131 public void updateStackWithFilesTest() throws MsoException, IOException {
132 String environmentString = "environmentString";
133 Map<String, Object> files = new HashMap<>();
134 files.put("file1", new Object());
136 CloudSite cloudSite = new CloudSite();
137 Heat heatClient = new Heat("endpoint");
138 Stack heatStack = mapper.readValue(new File(RESOURCE_PATH + "HeatStack.json"), Stack.class);
139 Stack updateStack = mapper.readValue(new File(RESOURCE_PATH + "UpdateStack.json"), Stack.class);
141 StackInfo expectedStackInfo = new StackInfo("stackName", HeatStatus.UPDATED, "stackStatusReason", null);
142 expectedStackInfo.setCanonicalName("stackName/id");
144 doReturn(heatClient).when(heatUtils).getHeatClient(isA(String.class), isA(String.class));
145 doReturn(null).when(heatUtils).executeAndRecordOpenstackRequest(isA(OpenStackRequest.class));
146 doReturn("0").when(environment).getProperty(isA(String.class), isA(String.class));
147 doReturn(updateStack).when(heatUtils).queryHeatStack(isA(Heat.class), isA(String.class));
149 StackInfo actualStackInfo = heatUtils.updateStack(cloudSiteId, cloudOwner, tenantId, stackName, heatTemplate,
150 stackInputs, pollForCompletion, timeoutMinutes, environmentString, files);
152 assertThat(actualStackInfo, sameBeanAs(expectedStackInfo));