1224898b9df7d54a4bd52d98df6e459078e915c7
[portal.git] / ecomp-portal-BE-common / 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
31 import com.fasterxml.jackson.databind.ObjectMapper;
32
33 *//**
34  * Tests the endpoints exposed by the Shared Context REST controller in Portal
35  * Core.
36  * 
37  * @author clott
38  *//*
39 public class SharedContextRestControllerTest {
40
41         private final Log logger = LogFactory.getLog(getClass());
42
43         private final SharedContextTestProperties properties;
44
45         private final String ckey = "ckey";
46         private final String cvalue = "cvalue";
47         
48         // Supposed to be a Portal session ID
49         private final String cxid = UUID.randomUUID().toString();
50
51         private final String key = "key123";
52         private final String value1 = "first value";
53         private final String value2 = "second value";
54
55         public SharedContextRestControllerTest() throws IOException {
56                 properties = new SharedContextTestProperties();
57         }
58
59         @SuppressWarnings("unchecked")
60         //@Test
61         public void test() throws Exception {
62                 String response = null, val = null;
63                 ObjectMapper mapper = new ObjectMapper();
64                 Map<String, Object> responseMap, jsonMap;
65
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);
72
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);
80
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);
88
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);
96
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);
104
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");
110
111                 logger.info("Clear the context");
112                 response = SharedContextRestClient.getJson(properties, "clear", cxid, null);
113                 Assert.assertEquals("", response);
114         }
115
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);
124                 return response;
125         }
126 }
127 */