Containerization feature of SO
[so.git] / adapters / mso-adapters-rest-interface / src / test / java / org / onap / so / openstack / mappers / StackInfoMapperTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 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.mappers;
22
23 import static org.junit.Assert.assertEquals;
24
25 import java.io.IOException;
26 import java.nio.file.Files;
27 import java.nio.file.Paths;
28 import java.util.HashMap;
29 import java.util.Map;
30
31 import org.junit.Test;
32 import org.onap.so.openstack.beans.HeatStatus;
33 import org.onap.so.openstack.beans.StackInfo;
34
35 import com.fasterxml.jackson.core.JsonParseException;
36 import com.fasterxml.jackson.databind.JsonMappingException;
37 import com.fasterxml.jackson.databind.ObjectMapper;
38 import com.woorea.openstack.heat.model.Stack;
39
40 public class StackInfoMapperTest {
41
42         private static final String PATH = "src/test/resources/";
43         @Test
44         public void nullStack() {
45                 StackInfoMapper mapper = new StackInfoMapper(null);
46                 assertEquals(HeatStatus.NOTFOUND, mapper.map().getStatus());
47         }
48         
49         @Test
50         public void checkHeatStatusMap() {
51                 StackInfoMapper mapper = new StackInfoMapper(null);
52                 assertEquals(HeatStatus.BUILDING, mapper.mapStatus("CREATE_IN_PROGRESS"));
53                 assertEquals(HeatStatus.CREATED, mapper.mapStatus("CREATE_COMPLETE"));
54                 assertEquals(HeatStatus.FAILED, mapper.mapStatus("CREATE_FAILED"));
55                 assertEquals(HeatStatus.DELETING, mapper.mapStatus("DELETE_IN_PROGRESS"));
56                 assertEquals(HeatStatus.NOTFOUND, mapper.mapStatus("DELETE_COMPLETE"));
57                 assertEquals(HeatStatus.FAILED, mapper.mapStatus("DELETE_FAILED"));
58                 assertEquals(HeatStatus.UPDATING, mapper.mapStatus("UPDATE_IN_PROGRESS"));
59                 assertEquals(HeatStatus.FAILED, mapper.mapStatus("UPDATE_FAILED"));
60                 assertEquals(HeatStatus.UPDATED, mapper.mapStatus("UPDATE_COMPLETE"));
61                 assertEquals(HeatStatus.INIT, mapper.mapStatus(null));
62                 assertEquals(HeatStatus.UNKNOWN, mapper.mapStatus("status-not-there"));
63         }
64         
65         @Test
66         public void checkOutputToMap() throws JsonParseException, JsonMappingException, IOException {
67                 ObjectMapper jacksonMapper = new ObjectMapper();
68                 Stack sample = jacksonMapper.readValue(this.getJson("stack-example.json"), Stack.class);
69                 StackInfoMapper mapper = new StackInfoMapper(sample);
70                 StackInfo result = mapper.map();
71                 Map<String, Object> map = result.getOutputs();
72                 assertEquals(true, map.containsKey("key2"));
73                 assertEquals("value1", map.get("key1"));
74         }
75         
76         @Test
77         public void mapRemainingFields() {
78                 Stack stack = new Stack();
79                 stack.setStackName("name");
80                 stack.setId("id");
81                 stack.setStackStatusReason("message");
82                 stack.setParameters(new HashMap<String, Object>());
83                 StackInfoMapper mapper = new StackInfoMapper(stack);
84                 StackInfo info = mapper.map();
85                 assertEquals("name", info.getName());
86                 assertEquals("name/id", info.getCanonicalName());
87                 assertEquals("message", info.getStatusMessage());
88                 assertEquals(stack.getParameters(), info.getParameters());
89         }
90         
91         private String getJson(String filename) throws IOException {
92                  return new String(Files.readAllBytes(Paths.get(PATH + filename)));
93         }
94 }