[PORTAL-16 PORTAL-18] Widget ms; staging
[portal.git] / ecomp-portal-BE-os / src / test / java / org / openecomp / portalapp / portal / controller / SharedContextRestControllerTest.java
1 /*-
2  * ================================================================================
3  * ECOMP Portal
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
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  * ================================================================================
19  */
20 package org.openecomp.portalapp.portal.controller;
21
22 import java.io.IOException;
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.UUID;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.junit.Assert;
30 import com.fasterxml.jackson.databind.ObjectMapper;
31
32 /**
33  * Tests the endpoints exposed by the Shared Context REST controller in Portal
34  * Core.
35  * 
36  * @author clott
37  */
38 public class SharedContextRestControllerTest {
39
40         private final Log logger = LogFactory.getLog(getClass());
41
42         private final SharedContextTestProperties properties;
43
44         private final String ckey = "ckey";
45         private final String cvalue = "cvalue";
46         
47         // Supposed to be a Portal session ID
48         private final String cxid = UUID.randomUUID().toString();
49
50         private final String key = "key123";
51         private final String value1 = "first value";
52         private final String value2 = "second value";
53
54         public SharedContextRestControllerTest() throws IOException {
55                 properties = new SharedContextTestProperties();
56         }
57
58         @SuppressWarnings("unchecked")
59         //@Test
60         public void test() throws Exception {
61                 String response = null, val = null;
62                 ObjectMapper mapper = new ObjectMapper();
63                 Map<String, Object> responseMap, jsonMap;
64
65                 logger.info("Get on empty context");
66                 response = SharedContextRestClient.getJson(properties, "get", cxid, key);
67                 // Should not exist - just generated the UUID
68                 Map<String, Object> responseMap1 = mapper.readValue(response, Map.class);
69                 response = (String) responseMap1.get("response");
70                 Assert.assertNull(response);
71
72                 logger.info("Set a new context");
73                 response = setContext(cxid, key, value1);
74                 Assert.assertNotNull(response);
75                 responseMap = mapper.readValue(response, Map.class);
76                 String responseValue = (String) responseMap.get("response");
77                 Assert.assertNotNull(responseValue);
78                 Assert.assertEquals("added", responseValue);
79
80                 logger.info("Get existing context");
81                 response = SharedContextRestClient.getJson(properties, "get", cxid, key);
82                 responseMap = mapper.readValue(response, Map.class);
83                 jsonMap = (Map<String,Object>) responseMap.get("response");
84                 Assert.assertNotNull(jsonMap);
85                 val = (String) jsonMap.get(cvalue);
86                 Assert.assertEquals(val, value1);
87
88                 logger.info("Overwrite exiting context");
89                 response = setContext(cxid, key, value2);
90                 Assert.assertNotNull(response);
91                 responseMap = mapper.readValue(response, Map.class);
92                 response = (String) responseMap.get("response");
93                 Assert.assertNotNull(responseValue);
94                 // Assert.assertEquals("replaced", responseValue);
95
96                 logger.info("Get existing context to verify overwrite");
97                 response = SharedContextRestClient.getJson(properties, "get", cxid, key);
98                 responseMap = mapper.readValue(response, Map.class);
99                 jsonMap = (Map<String,Object>) responseMap.get("response");
100                 Assert.assertNotNull(jsonMap);
101                 val = (String) jsonMap.get(cvalue);
102                 Assert.assertEquals(val, value2);
103
104                 logger.info("Delete one context");
105                 response = SharedContextRestClient.getJson(properties, "remove", cxid, key);
106                 responseMap = mapper.readValue(response, Map.class);
107                 response = (String) responseMap.get("response");
108                 Assert.assertEquals(response, "removed");
109
110                 logger.info("Clear the context");
111                 response = SharedContextRestClient.getJson(properties, "clear", cxid, null);
112                 Assert.assertEquals("", response);
113         }
114
115         private String setContext(String context, String id, String value) throws Exception {
116                 ObjectMapper mapper = new ObjectMapper();
117                 HashMap<String,String> stringMap = new HashMap<String,String>();
118                 stringMap.put("context_id", cxid);
119                 stringMap.put(ckey, key);
120                 stringMap.put(cvalue, value2);
121                 String json = mapper.writeValueAsString(stringMap);
122                 String response = SharedContextRestClient.postJson(properties, "set", json);
123                 return response;
124         }
125 }