c476e3bf19615b0ac6b5d8c7c1c930c92c5c4647
[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 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;
49
50 @RunWith(MockitoJUnitRunner.class)
51 public class MsoHeatUtilsWithUpdateTest extends TestDataSetup {
52     @Mock
53     private CloudConfig cloudConfig;
54
55     @Mock
56     private Environment environment;
57
58     @Spy
59     @InjectMocks
60     private MsoHeatUtilsWithUpdate heatUtils;
61
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;
70
71     @Before
72     public void before() {
73         MockitoAnnotations.initMocks(this);
74
75         cloudOwner = "cloudOwner";
76         cloudSiteId = "cloudSiteId";
77         tenantId = "tenantId";
78         stackName = "stackName";
79         heatTemplate = "heatTemplate";
80         stackInputs = new HashMap<>();
81         pollForCompletion = true;
82         timeoutMinutes = 0;
83     }
84
85     @Test
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);
91
92         StackInfo expectedStackInfo = new StackInfo("stackName", HeatStatus.UPDATED, "stackStatusReason", null);
93         expectedStackInfo.setCanonicalName("stackName/id");
94
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));
99
100         StackInfo actualStackInfo = heatUtils.updateStack(cloudSiteId, cloudOwner, tenantId, stackName, heatTemplate,
101                 stackInputs, pollForCompletion, timeoutMinutes);
102
103         assertThat(actualStackInfo, sameBeanAs(expectedStackInfo));
104     }
105
106     @Test
107     public void updateStackWithEnvironmentTest() throws IOException, MsoException {
108         String environmentString = "environmentString";
109
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);
114
115         StackInfo expectedStackInfo = new StackInfo("stackName", HeatStatus.UPDATED, "stackStatusReason", null);
116         expectedStackInfo.setCanonicalName("stackName/id");
117
118         doReturn(heatClient).when(heatUtils).getHeatClient(isA(String.class), isA(String.class));
119
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));
123
124         StackInfo actualStackInfo = heatUtils.updateStack(cloudSiteId, cloudOwner, tenantId, stackName, heatTemplate,
125                 stackInputs, pollForCompletion, timeoutMinutes, environmentString);
126
127         assertThat(actualStackInfo, sameBeanAs(expectedStackInfo));
128     }
129
130     @Test
131     public void updateStackWithFilesTest() throws MsoException, IOException {
132         String environmentString = "environmentString";
133         Map<String, Object> files = new HashMap<>();
134         files.put("file1", new Object());
135
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);
140
141         StackInfo expectedStackInfo = new StackInfo("stackName", HeatStatus.UPDATED, "stackStatusReason", null);
142         expectedStackInfo.setCanonicalName("stackName/id");
143
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));
148
149         StackInfo actualStackInfo = heatUtils.updateStack(cloudSiteId, cloudOwner, tenantId, stackName, heatTemplate,
150                 stackInputs, pollForCompletion, timeoutMinutes, environmentString, files);
151
152         assertThat(actualStackInfo, sameBeanAs(expectedStackInfo));
153     }
154 }