1 package org.onap.portalapp.portal.controller;
 
   3  * ============LICENSE_START==========================================
 
   5  * ===================================================================
 
   6  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 
   7  * ===================================================================
 
   9  * Unless otherwise specified, all software contained herein is licensed
 
  10  * under the Apache License, Version 2.0 (the "License");
 
  11  * you may not use this software except in compliance with the License.
 
  12  * You may obtain a copy of the License at
 
  14  *             http://www.apache.org/licenses/LICENSE-2.0
 
  16  * Unless required by applicable law or agreed to in writing, software
 
  17  * distributed under the License is distributed on an "AS IS" BASIS,
 
  18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  19  * See the License for the specific language governing permissions and
 
  20  * limitations under the License.
 
  22  * Unless otherwise specified, all documentation contained herein is licensed
 
  23  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
 
  24  * you may not use this documentation except in compliance with the License.
 
  25  * You may obtain a copy of the License at
 
  27  *             https://creativecommons.org/licenses/by/4.0/
 
  29  * Unless required by applicable law or agreed to in writing, documentation
 
  30  * distributed under the License is distributed on an "AS IS" BASIS,
 
  31  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  32  * See the License for the specific language governing permissions and
 
  33  * limitations under the License.
 
  35  * ============LICENSE_END============================================
 
  37  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 
  41 import java.io.IOException;
 
  42 import java.util.HashMap;
 
  44 import java.util.UUID;
 
  46 import org.apache.commons.logging.Log;
 
  47 import org.apache.commons.logging.LogFactory;
 
  48 import org.junit.Assert;
 
  49 import org.onap.portalapp.portal.controller.SharedContextRestClient;
 
  50 import org.onap.portalapp.portal.controller.SharedContextTestProperties;
 
  52 import com.fasterxml.jackson.databind.ObjectMapper;
 
  55  * Tests the endpoints exposed by the Shared Context controller in Portal.
 
  57 public class SharedContextRestControllerTest {
 
  59         private final Log logger = LogFactory.getLog(getClass());
 
  61         private final SharedContextTestProperties properties;
 
  63         private final String ckey = "ckey";
 
  64         private final String cvalue = "cvalue";
 
  66         // Supposed to be a Portal session ID
 
  67         private final String cxid = UUID.randomUUID().toString();
 
  69         private final String key = "key123";
 
  70         private final String value1 = "first value";
 
  71         private final String value2 = "second value";
 
  73         public SharedContextRestControllerTest() throws IOException {
 
  74                 properties = new SharedContextTestProperties();
 
  77         @SuppressWarnings("unchecked")
 
  79         public void test() throws Exception {
 
  80                 String response = null, val = null;
 
  81                 ObjectMapper mapper = new ObjectMapper();
 
  82                 Map<String, Object> responseMap, jsonMap;
 
  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);
 
  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);
 
  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);
 
 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);
 
 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);
 
 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");
 
 129                 logger.info("Clear the context");
 
 130                 response = SharedContextRestClient.getJson(properties, "clear", cxid, null);
 
 131                 Assert.assertEquals("", response);
 
 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);