2 * ================================================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ================================================================================
20 /*package org.openecomp.portalapp.portal.controller;
22 import java.io.IOException;
23 import java.util.HashMap;
25 import java.util.UUID;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.junit.Assert;
31 import com.fasterxml.jackson.databind.ObjectMapper;
34 * Tests the endpoints exposed by the Shared Context REST controller in Portal
39 public class SharedContextRestControllerTest {
41 private final Log logger = LogFactory.getLog(getClass());
43 private final SharedContextTestProperties properties;
45 private final String ckey = "ckey";
46 private final String cvalue = "cvalue";
48 // Supposed to be a Portal session ID
49 private final String cxid = UUID.randomUUID().toString();
51 private final String key = "key123";
52 private final String value1 = "first value";
53 private final String value2 = "second value";
55 public SharedContextRestControllerTest() throws IOException {
56 properties = new SharedContextTestProperties();
59 @SuppressWarnings("unchecked")
61 public void test() throws Exception {
62 String response = null, val = null;
63 ObjectMapper mapper = new ObjectMapper();
64 Map<String, Object> responseMap, jsonMap;
66 logger.info("Get on empty context");
67 response = SharedContextRestClient.getJson(properties, "get", cxid, key);
68 // Should not exist - just generated the UUID
69 Map<String, Object> responseMap1 = mapper.readValue(response, Map.class);
70 response = (String) responseMap1.get("response");
71 Assert.assertNull(response);
73 logger.info("Set a new context");
74 response = setContext(cxid, key, value1);
75 Assert.assertNotNull(response);
76 responseMap = mapper.readValue(response, Map.class);
77 String responseValue = (String) responseMap.get("response");
78 Assert.assertNotNull(responseValue);
79 Assert.assertEquals("added", responseValue);
81 logger.info("Get existing context");
82 response = SharedContextRestClient.getJson(properties, "get", cxid, key);
83 responseMap = mapper.readValue(response, Map.class);
84 jsonMap = (Map<String,Object>) responseMap.get("response");
85 Assert.assertNotNull(jsonMap);
86 val = (String) jsonMap.get(cvalue);
87 Assert.assertEquals(val, value1);
89 logger.info("Overwrite exiting context");
90 response = setContext(cxid, key, value2);
91 Assert.assertNotNull(response);
92 responseMap = mapper.readValue(response, Map.class);
93 response = (String) responseMap.get("response");
94 Assert.assertNotNull(responseValue);
95 // Assert.assertEquals("replaced", responseValue);
97 logger.info("Get existing context to verify overwrite");
98 response = SharedContextRestClient.getJson(properties, "get", cxid, key);
99 responseMap = mapper.readValue(response, Map.class);
100 jsonMap = (Map<String,Object>) responseMap.get("response");
101 Assert.assertNotNull(jsonMap);
102 val = (String) jsonMap.get(cvalue);
103 Assert.assertEquals(val, value2);
105 logger.info("Delete one context");
106 response = SharedContextRestClient.getJson(properties, "remove", cxid, key);
107 responseMap = mapper.readValue(response, Map.class);
108 response = (String) responseMap.get("response");
109 Assert.assertEquals(response, "removed");
111 logger.info("Clear the context");
112 response = SharedContextRestClient.getJson(properties, "clear", cxid, null);
113 Assert.assertEquals("", response);
116 private String setContext(String context, String id, String value) throws Exception {
117 ObjectMapper mapper = new ObjectMapper();
118 HashMap<String,String> stringMap = new HashMap<String,String>();
119 stringMap.put("context_id", cxid);
120 stringMap.put(ckey, key);
121 stringMap.put(cvalue, value2);
122 String json = mapper.writeValueAsString(stringMap);
123 String response = SharedContextRestClient.postJson(properties, "set", json);