bdede7d1d4330633e97495cc40fffbb8b3b9c8fd
[portal.git] / ecomp-portal-BE-common / 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
49 import com.fasterxml.jackson.databind.ObjectMapper;
50
51 *//**
52  * Tests the endpoints exposed by the Shared Context REST controller in Portal
53  * Core.
54  * 
55  * @author clott
56  *//*
57 public class SharedContextRestControllerTest {
58
59         private final Log logger = LogFactory.getLog(getClass());
60
61         private final SharedContextTestProperties properties;
62
63         private final String ckey = "ckey";
64         private final String cvalue = "cvalue";
65         
66         // Supposed to be a Portal session ID
67         private final String cxid = UUID.randomUUID().toString();
68
69         private final String key = "key123";
70         private final String value1 = "first value";
71         private final String value2 = "second value";
72
73         public SharedContextRestControllerTest() throws IOException {
74                 properties = new SharedContextTestProperties();
75         }
76
77         @SuppressWarnings("unchecked")
78         //@Test
79         public void test() throws Exception {
80                 String response = null, val = null;
81                 ObjectMapper mapper = new ObjectMapper();
82                 Map<String, Object> responseMap, jsonMap;
83
84                 logger.info("Get on empty context");
85                 response = SharedContextRestClient.getJson(properties, "get", cxid, key);
86                 // Should not exist - just generated the UUID
87                 Map<String, Object> responseMap1 = mapper.readValue(response, Map.class);
88                 response = (String) responseMap1.get("response");
89                 Assert.assertNull(response);
90
91                 logger.info("Set a new context");
92                 response = setContext(cxid, key, value1);
93                 Assert.assertNotNull(response);
94                 responseMap = mapper.readValue(response, Map.class);
95                 String responseValue = (String) responseMap.get("response");
96                 Assert.assertNotNull(responseValue);
97                 Assert.assertEquals("added", responseValue);
98
99                 logger.info("Get existing context");
100                 response = SharedContextRestClient.getJson(properties, "get", cxid, key);
101                 responseMap = mapper.readValue(response, Map.class);
102                 jsonMap = (Map<String,Object>) responseMap.get("response");
103                 Assert.assertNotNull(jsonMap);
104                 val = (String) jsonMap.get(cvalue);
105                 Assert.assertEquals(val, value1);
106
107                 logger.info("Overwrite exiting context");
108                 response = setContext(cxid, key, value2);
109                 Assert.assertNotNull(response);
110                 responseMap = mapper.readValue(response, Map.class);
111                 response = (String) responseMap.get("response");
112                 Assert.assertNotNull(responseValue);
113                 // Assert.assertEquals("replaced", responseValue);
114
115                 logger.info("Get existing context to verify overwrite");
116                 response = SharedContextRestClient.getJson(properties, "get", cxid, key);
117                 responseMap = mapper.readValue(response, Map.class);
118                 jsonMap = (Map<String,Object>) responseMap.get("response");
119                 Assert.assertNotNull(jsonMap);
120                 val = (String) jsonMap.get(cvalue);
121                 Assert.assertEquals(val, value2);
122
123                 logger.info("Delete one context");
124                 response = SharedContextRestClient.getJson(properties, "remove", cxid, key);
125                 responseMap = mapper.readValue(response, Map.class);
126                 response = (String) responseMap.get("response");
127                 Assert.assertEquals(response, "removed");
128
129                 logger.info("Clear the context");
130                 response = SharedContextRestClient.getJson(properties, "clear", cxid, null);
131                 Assert.assertEquals("", response);
132         }
133
134         private String setContext(String context, String id, String value) throws Exception {
135                 ObjectMapper mapper = new ObjectMapper();
136                 HashMap<String,String> stringMap = new HashMap<String,String>();
137                 stringMap.put("context_id", cxid);
138                 stringMap.put(ckey, key);
139                 stringMap.put(cvalue, value2);
140                 String json = mapper.writeValueAsString(stringMap);
141                 String response = SharedContextRestClient.postJson(properties, "set", json);
142                 return response;
143         }
144 }
145 */