[PORTAL-7] Rebase
[portal.git] / ecomp-portal-BE-os / src / test / java / org / openecomp / portalapp / portal / controller / SharedContextRestControllerTest.java
diff --git a/ecomp-portal-BE-os/src/test/java/org/openecomp/portalapp/portal/controller/SharedContextRestControllerTest.java b/ecomp-portal-BE-os/src/test/java/org/openecomp/portalapp/portal/controller/SharedContextRestControllerTest.java
new file mode 100644 (file)
index 0000000..a7a86e8
--- /dev/null
@@ -0,0 +1,125 @@
+/*-\r
+ * ================================================================================\r
+ * ECOMP Portal\r
+ * ================================================================================\r
+ * Copyright (C) 2017 AT&T Intellectual Property\r
+ * ================================================================================\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ * \r
+ *      http://www.apache.org/licenses/LICENSE-2.0\r
+ * \r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ * ================================================================================\r
+ */\r
+package org.openecomp.portalapp.portal.controller;\r
+\r
+import java.io.IOException;\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+import java.util.UUID;\r
+\r
+import org.apache.commons.logging.Log;\r
+import org.apache.commons.logging.LogFactory;\r
+import org.junit.Assert;\r
+import com.fasterxml.jackson.databind.ObjectMapper;\r
+\r
+/**\r
+ * Tests the endpoints exposed by the Shared Context REST controller in Portal\r
+ * Core.\r
+ * \r
+ * @author clott\r
+ */\r
+public class SharedContextRestControllerTest {\r
+\r
+       private final Log logger = LogFactory.getLog(getClass());\r
+\r
+       private final SharedContextTestProperties properties;\r
+\r
+       private final String ckey = "ckey";\r
+       private final String cvalue = "cvalue";\r
+       \r
+       // Supposed to be a Portal session ID\r
+       private final String cxid = UUID.randomUUID().toString();\r
+\r
+       private final String key = "key123";\r
+       private final String value1 = "first value";\r
+       private final String value2 = "second value";\r
+\r
+       public SharedContextRestControllerTest() throws IOException {\r
+               properties = new SharedContextTestProperties();\r
+       }\r
+\r
+       @SuppressWarnings("unchecked")\r
+       //@Test\r
+       public void test() throws Exception {\r
+               String response = null, val = null;\r
+               ObjectMapper mapper = new ObjectMapper();\r
+               Map<String, Object> responseMap, jsonMap;\r
+\r
+               logger.info("Get on empty context");\r
+               response = SharedContextRestClient.getJson(properties, "get", cxid, key);\r
+               // Should not exist - just generated the UUID\r
+               Map<String, Object> responseMap1 = mapper.readValue(response, Map.class);\r
+               response = (String) responseMap1.get("response");\r
+               Assert.assertNull(response);\r
+\r
+               logger.info("Set a new context");\r
+               response = setContext(cxid, key, value1);\r
+               Assert.assertNotNull(response);\r
+               responseMap = mapper.readValue(response, Map.class);\r
+               String responseValue = (String) responseMap.get("response");\r
+               Assert.assertNotNull(responseValue);\r
+               Assert.assertEquals("added", responseValue);\r
+\r
+               logger.info("Get existing context");\r
+               response = SharedContextRestClient.getJson(properties, "get", cxid, key);\r
+               responseMap = mapper.readValue(response, Map.class);\r
+               jsonMap = (Map<String,Object>) responseMap.get("response");\r
+               Assert.assertNotNull(jsonMap);\r
+               val = (String) jsonMap.get(cvalue);\r
+               Assert.assertEquals(val, value1);\r
+\r
+               logger.info("Overwrite exiting context");\r
+               response = setContext(cxid, key, value2);\r
+               Assert.assertNotNull(response);\r
+               responseMap = mapper.readValue(response, Map.class);\r
+               response = (String) responseMap.get("response");\r
+               Assert.assertNotNull(responseValue);\r
+               // Assert.assertEquals("replaced", responseValue);\r
+\r
+               logger.info("Get existing context to verify overwrite");\r
+               response = SharedContextRestClient.getJson(properties, "get", cxid, key);\r
+               responseMap = mapper.readValue(response, Map.class);\r
+               jsonMap = (Map<String,Object>) responseMap.get("response");\r
+               Assert.assertNotNull(jsonMap);\r
+               val = (String) jsonMap.get(cvalue);\r
+               Assert.assertEquals(val, value2);\r
+\r
+               logger.info("Delete one context");\r
+               response = SharedContextRestClient.getJson(properties, "remove", cxid, key);\r
+               responseMap = mapper.readValue(response, Map.class);\r
+               response = (String) responseMap.get("response");\r
+               Assert.assertEquals(response, "removed");\r
+\r
+               logger.info("Clear the context");\r
+               response = SharedContextRestClient.getJson(properties, "clear", cxid, null);\r
+               Assert.assertEquals("", response);\r
+       }\r
+\r
+       private String setContext(String context, String id, String value) throws Exception {\r
+               ObjectMapper mapper = new ObjectMapper();\r
+               HashMap<String,String> stringMap = new HashMap<String,String>();\r
+               stringMap.put("context_id", cxid);\r
+               stringMap.put(ckey, key);\r
+               stringMap.put(cvalue, value2);\r
+               String json = mapper.writeValueAsString(stringMap);\r
+               String response = SharedContextRestClient.postJson(properties, "set", json);\r
+               return response;\r
+       }\r
+}\r