Null check for ClientResponse in PolicyUril.java
[portal.git] / ecomp-portal-BE-os / src / test / java / org / openecomp / portalapp / portal / controller / SharedContextRestControllerTest.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the “License”);
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the “License”);
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.openecomp.portalapp.portal.controller;
39
40 import java.io.IOException;
41 import java.util.HashMap;
42 import java.util.Map;
43 import java.util.UUID;
44
45 import org.apache.commons.logging.Log;
46 import org.apache.commons.logging.LogFactory;
47 import org.junit.Assert;
48 import com.fasterxml.jackson.databind.ObjectMapper;
49
50 /**
51  * Tests the endpoints exposed by the Shared Context REST controller in Portal
52  * Core.
53  * 
54  * @author clott
55  */
56 public class SharedContextRestControllerTest {
57
58         private final Log logger = LogFactory.getLog(getClass());
59
60         private final SharedContextTestProperties properties;
61
62         private final String ckey = "ckey";
63         private final String cvalue = "cvalue";
64         
65         // Supposed to be a Portal session ID
66         private final String cxid = UUID.randomUUID().toString();
67
68         private final String key = "key123";
69         private final String value1 = "first value";
70         private final String value2 = "second value";
71
72         public SharedContextRestControllerTest() throws IOException {
73                 properties = new SharedContextTestProperties();
74         }
75
76         @SuppressWarnings("unchecked")
77         //@Test
78         public void test() throws Exception {
79                 String response = null, val = null;
80                 ObjectMapper mapper = new ObjectMapper();
81                 Map<String, Object> responseMap, jsonMap;
82
83                 logger.info("Get on empty context");
84                 response = SharedContextRestClient.getJson(properties, "get", cxid, key);
85                 // Should not exist - just generated the UUID
86                 Map<String, Object> responseMap1 = mapper.readValue(response, Map.class);
87                 response = (String) responseMap1.get("response");
88                 Assert.assertNull(response);
89
90                 logger.info("Set a new context");
91                 response = setContext(cxid, key, value1);
92                 Assert.assertNotNull(response);
93                 responseMap = mapper.readValue(response, Map.class);
94                 String responseValue = (String) responseMap.get("response");
95                 Assert.assertNotNull(responseValue);
96                 Assert.assertEquals("added", responseValue);
97
98                 logger.info("Get existing context");
99                 response = SharedContextRestClient.getJson(properties, "get", cxid, key);
100                 responseMap = mapper.readValue(response, Map.class);
101                 jsonMap = (Map<String,Object>) responseMap.get("response");
102                 Assert.assertNotNull(jsonMap);
103                 val = (String) jsonMap.get(cvalue);
104                 Assert.assertEquals(val, value1);
105
106                 logger.info("Overwrite exiting context");
107                 response = setContext(cxid, key, value2);
108                 Assert.assertNotNull(response);
109                 responseMap = mapper.readValue(response, Map.class);
110                 response = (String) responseMap.get("response");
111                 Assert.assertNotNull(responseValue);
112                 // Assert.assertEquals("replaced", responseValue);
113
114                 logger.info("Get existing context to verify overwrite");
115                 response = SharedContextRestClient.getJson(properties, "get", cxid, key);
116                 responseMap = mapper.readValue(response, Map.class);
117                 jsonMap = (Map<String,Object>) responseMap.get("response");
118                 Assert.assertNotNull(jsonMap);
119                 val = (String) jsonMap.get(cvalue);
120                 Assert.assertEquals(val, value2);
121
122                 logger.info("Delete one context");
123                 response = SharedContextRestClient.getJson(properties, "remove", cxid, key);
124                 responseMap = mapper.readValue(response, Map.class);
125                 response = (String) responseMap.get("response");
126                 Assert.assertEquals(response, "removed");
127
128                 logger.info("Clear the context");
129                 response = SharedContextRestClient.getJson(properties, "clear", cxid, null);
130                 Assert.assertEquals("", response);
131         }
132
133         private String setContext(String context, String id, String value) throws Exception {
134                 ObjectMapper mapper = new ObjectMapper();
135                 HashMap<String,String> stringMap = new HashMap<String,String>();
136                 stringMap.put("context_id", cxid);
137                 stringMap.put(ckey, key);
138                 stringMap.put(cvalue, value2);
139                 String json = mapper.writeValueAsString(stringMap);
140                 String response = SharedContextRestClient.postJson(properties, "set", json);
141                 return response;
142         }
143 }