acd42dd8da347aec5ded697eae111e1c1da96ff5
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.openstack.utils;
22
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;
27 import java.io.File;
28 import java.io.IOException;
29 import java.util.HashMap;
30 import java.util.Map;
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;
52
53 @RunWith(MockitoJUnitRunner.class)
54 public class MsoHeatUtilsWithUpdateTest extends TestDataSetup {
55     @Mock
56     private CloudConfig cloudConfig;
57
58     @Mock
59     private Environment environment;
60
61     @Spy
62     @InjectMocks
63     private MsoHeatUtilsWithUpdate heatUtils;
64
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;
73
74     @Before
75     public void before() {
76         MockitoAnnotations.initMocks(this);
77
78         cloudOwner = "cloudOwner";
79         cloudSiteId = "cloudSiteId";
80         tenantId = "tenantId";
81         stackName = "stackName";
82         heatTemplate = "heatTemplate";
83         stackInputs = new HashMap<>();
84         pollForCompletion = true;
85         timeoutMinutes = 0;
86     }
87
88     @Test
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);
94
95         StackInfo expectedStackInfo = new StackInfo("stackName", HeatStatus.UPDATED, "stackStatusReason", null);
96         expectedStackInfo.setCanonicalName("stackName/id");
97
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));
103
104         StackInfo actualStackInfo = heatUtils.updateStack(cloudSiteId, cloudOwner, tenantId, stackName, heatTemplate,
105                 stackInputs, pollForCompletion, timeoutMinutes);
106
107         assertThat(actualStackInfo, sameBeanAs(expectedStackInfo));
108     }
109
110     @Test
111     public void updateStackWithEnvironmentTest()
112             throws JsonParseException, JsonMappingException, IOException, MsoException {
113         String environmentString = "environmentString";
114
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);
119
120         StackInfo expectedStackInfo = new StackInfo("stackName", HeatStatus.UPDATED, "stackStatusReason", null);
121         expectedStackInfo.setCanonicalName("stackName/id");
122
123         doReturn(Optional.of(cloudSite)).when(cloudConfig).getCloudSite(isA(String.class));
124         doReturn(heatClient).when(heatUtils).getHeatClient(isA(String.class), isA(String.class));
125
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));
129
130         StackInfo actualStackInfo = heatUtils.updateStack(cloudSiteId, cloudOwner, tenantId, stackName, heatTemplate,
131                 stackInputs, pollForCompletion, timeoutMinutes, environmentString);
132
133         assertThat(actualStackInfo, sameBeanAs(expectedStackInfo));
134     }
135
136     @Test
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());
141
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);
146
147         StackInfo expectedStackInfo = new StackInfo("stackName", HeatStatus.UPDATED, "stackStatusReason", null);
148         expectedStackInfo.setCanonicalName("stackName/id");
149
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));
155
156         StackInfo actualStackInfo = heatUtils.updateStack(cloudSiteId, cloudOwner, tenantId, stackName, heatTemplate,
157                 stackInputs, pollForCompletion, timeoutMinutes, environmentString, files);
158
159         assertThat(actualStackInfo, sameBeanAs(expectedStackInfo));
160     }
161 }