2  * ================================================================================
\r 
   4  * ================================================================================
\r 
   5  * Copyright (C) 2017 AT&T Intellectual Property
\r 
   6  * ================================================================================
\r 
   7  * Licensed under the Apache License, Version 2.0 (the "License");
\r 
   8  * you may not use this file except in compliance with the License.
\r 
   9  * You may obtain a copy of the License at
\r 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
\r 
  13  * Unless required by applicable law or agreed to in writing, software
\r 
  14  * distributed under the License is distributed on an "AS IS" BASIS,
\r 
  15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r 
  16  * See the License for the specific language governing permissions and
\r 
  17  * limitations under the License.
\r 
  18  * ================================================================================
\r 
  20 package org.openecomp.portalapp.portal.controller;
\r 
  22 import java.io.IOException;
\r 
  23 import java.util.HashMap;
\r 
  24 import java.util.Map;
\r 
  25 import java.util.UUID;
\r 
  27 import org.apache.commons.logging.Log;
\r 
  28 import org.apache.commons.logging.LogFactory;
\r 
  29 import org.junit.Assert;
\r 
  30 import com.fasterxml.jackson.databind.ObjectMapper;
\r 
  33  * Tests the endpoints exposed by the Shared Context REST controller in Portal
\r 
  38 public class SharedContextRestControllerTest {
\r 
  40         private final Log logger = LogFactory.getLog(getClass());
\r 
  42         private final SharedContextTestProperties properties;
\r 
  44         private final String ckey = "ckey";
\r 
  45         private final String cvalue = "cvalue";
\r 
  47         // Supposed to be a Portal session ID
\r 
  48         private final String cxid = UUID.randomUUID().toString();
\r 
  50         private final String key = "key123";
\r 
  51         private final String value1 = "first value";
\r 
  52         private final String value2 = "second value";
\r 
  54         public SharedContextRestControllerTest() throws IOException {
\r 
  55                 properties = new SharedContextTestProperties();
\r 
  58         @SuppressWarnings("unchecked")
\r 
  60         public void test() throws Exception {
\r 
  61                 String response = null, val = null;
\r 
  62                 ObjectMapper mapper = new ObjectMapper();
\r 
  63                 Map<String, Object> responseMap, jsonMap;
\r 
  65                 logger.info("Get on empty context");
\r 
  66                 response = SharedContextRestClient.getJson(properties, "get", cxid, key);
\r 
  67                 // Should not exist - just generated the UUID
\r 
  68                 Map<String, Object> responseMap1 = mapper.readValue(response, Map.class);
\r 
  69                 response = (String) responseMap1.get("response");
\r 
  70                 Assert.assertNull(response);
\r 
  72                 logger.info("Set a new context");
\r 
  73                 response = setContext(cxid, key, value1);
\r 
  74                 Assert.assertNotNull(response);
\r 
  75                 responseMap = mapper.readValue(response, Map.class);
\r 
  76                 String responseValue = (String) responseMap.get("response");
\r 
  77                 Assert.assertNotNull(responseValue);
\r 
  78                 Assert.assertEquals("added", responseValue);
\r 
  80                 logger.info("Get existing context");
\r 
  81                 response = SharedContextRestClient.getJson(properties, "get", cxid, key);
\r 
  82                 responseMap = mapper.readValue(response, Map.class);
\r 
  83                 jsonMap = (Map<String,Object>) responseMap.get("response");
\r 
  84                 Assert.assertNotNull(jsonMap);
\r 
  85                 val = (String) jsonMap.get(cvalue);
\r 
  86                 Assert.assertEquals(val, value1);
\r 
  88                 logger.info("Overwrite exiting context");
\r 
  89                 response = setContext(cxid, key, value2);
\r 
  90                 Assert.assertNotNull(response);
\r 
  91                 responseMap = mapper.readValue(response, Map.class);
\r 
  92                 response = (String) responseMap.get("response");
\r 
  93                 Assert.assertNotNull(responseValue);
\r 
  94                 // Assert.assertEquals("replaced", responseValue);
\r 
  96                 logger.info("Get existing context to verify overwrite");
\r 
  97                 response = SharedContextRestClient.getJson(properties, "get", cxid, key);
\r 
  98                 responseMap = mapper.readValue(response, Map.class);
\r 
  99                 jsonMap = (Map<String,Object>) responseMap.get("response");
\r 
 100                 Assert.assertNotNull(jsonMap);
\r 
 101                 val = (String) jsonMap.get(cvalue);
\r 
 102                 Assert.assertEquals(val, value2);
\r 
 104                 logger.info("Delete one context");
\r 
 105                 response = SharedContextRestClient.getJson(properties, "remove", cxid, key);
\r 
 106                 responseMap = mapper.readValue(response, Map.class);
\r 
 107                 response = (String) responseMap.get("response");
\r 
 108                 Assert.assertEquals(response, "removed");
\r 
 110                 logger.info("Clear the context");
\r 
 111                 response = SharedContextRestClient.getJson(properties, "clear", cxid, null);
\r 
 112                 Assert.assertEquals("", response);
\r 
 115         private String setContext(String context, String id, String value) throws Exception {
\r 
 116                 ObjectMapper mapper = new ObjectMapper();
\r 
 117                 HashMap<String,String> stringMap = new HashMap<String,String>();
\r 
 118                 stringMap.put("context_id", cxid);
\r 
 119                 stringMap.put(ckey, key);
\r 
 120                 stringMap.put(cvalue, value2);
\r 
 121                 String json = mapper.writeValueAsString(stringMap);
\r 
 122                 String response = SharedContextRestClient.postJson(properties, "set", json);
\r