Remove code smells in dashboard
[nonrtric.git] / dashboard / webapp-backend / src / test / java / org / oransc / ric / portal / dashboard / config / PolicyControllerMockConfiguration.java
index 41b9ee0..9c6d40c 100644 (file)
@@ -19,6 +19,8 @@
  */
 package org.oransc.ric.portal.dashboard.config;
 
+import com.google.gson.GsonBuilder;
+
 import java.io.BufferedReader;
 import java.io.InputStream;
 import java.io.InputStreamReader;
@@ -54,6 +56,10 @@ public class PolicyControllerMockConfiguration {
 
     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
 
+    private static com.google.gson.Gson gson = new GsonBuilder() //
+        .serializeNulls() //
+        .create(); //
+
     @Bean
     public PolicyAgentApi policyAgentApi() {
         MockPolicyAgentApi apiClient = new MockPolicyAgentApi();
@@ -64,70 +70,62 @@ public class PolicyControllerMockConfiguration {
         private final Database database = new Database();
 
         @Override
-        public ResponseEntity<String> getPolicyInstance(String id) throws RestClientException {
+        public ResponseEntity<Object> getPolicyInstance(String id) {
             return new ResponseEntity<>(database.getInstance(id), HttpStatus.OK);
         }
 
         @Override
-        public ResponseEntity<String> putPolicy(String policyTypeIdString, String policyInstanceId, String json,
-            String ric) throws RestClientException {
+        public ResponseEntity<String> putPolicy(String policyTypeIdString, String policyInstanceId, Object json,
+            String ric) {
             database.putInstance(policyTypeIdString, policyInstanceId, json, ric);
-            return new ResponseEntity<>(HttpStatus.OK);
+            return new ResponseEntity<>("Policy was put successfully", HttpStatus.OK);
         }
 
         @Override
-        public void deletePolicy(String policyInstanceId) throws RestClientException {
+        public ResponseEntity<String> deletePolicy(String policyInstanceId) {
             database.deleteInstance(policyInstanceId);
+            return new ResponseEntity<>("Policy was deleted successfully", HttpStatus.NO_CONTENT);
         }
 
         @Override
-        public ResponseEntity<PolicyTypes> getAllPolicyTypes() throws RestClientException {
+        public ResponseEntity<String> getAllPolicyTypes() {
             PolicyTypes result = new PolicyTypes();
             result.addAll(database.getTypes());
-            return new ResponseEntity<>(result, HttpStatus.OK);
+            return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
         }
 
         @Override
-        public ResponseEntity<PolicyInstances> getPolicyInstancesForType(String type) {
+        public ResponseEntity<String> getPolicyInstancesForType(String type) {
             PolicyInstances result = new PolicyInstances();
             List<PolicyInfo> inst = database.getInstances(Optional.of(type));
             result.addAll(inst);
-            return new ResponseEntity<>(result, HttpStatus.OK);
+            return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
         }
 
         @Override
-        public ResponseEntity<Collection<String>> getRicsSupportingType(String typeName) {
+        public ResponseEntity<String> getRicsSupportingType(String typeName) {
             Vector<String> res = new Vector<>();
             res.add("ric_1");
             res.add("ric_2");
             res.add("ric_3");
-            return new ResponseEntity<>(res, HttpStatus.OK);
+            return new ResponseEntity<>(gson.toJson(res), HttpStatus.OK);
         }
     }
 
     class Database {
 
         Database() {
-            String schema = getStringFromFile("anr-policy-schema.json");
-            PolicyType policy = new PolicyType("ANR", schema);
-            types.put("ANR", policy);
-
-            schema = getStringFromFile("demo-policy-schema-1.json");
-            policy = new PolicyType("type2", schema);
-            types.put("type2", policy);
+            String schema = getStringFromFile("demo-policy-schema-1.json");
+            PolicyType policyType = new PolicyType("type2", schema);
+            types.put("type2", policyType);
 
             schema = getStringFromFile("demo-policy-schema-2.json");
-            policy = new PolicyType("type3", schema);
-            types.put("type3", policy);
+            policyType = new PolicyType("type3", schema);
+            types.put("type3", policyType);
 
             schema = getStringFromFile("demo-policy-schema-3.json");
-            policy = new PolicyType("type4", schema);
-            types.put("type4", policy);
-            try {
-                putInstance("ANR", "ANR-1", getStringFromFile("anr-policy-instance.json"), "ric_1");
-            } catch (Exception e) {
-                // Nothing
-            }
+            policyType = new PolicyType("type4", schema);
+            types.put("type4", policyType);
         }
 
         private String getStringFromFile(String path) {
@@ -149,7 +147,7 @@ public class PolicyControllerMockConfiguration {
             return java.time.Instant.now().toString();
         }
 
-        void putInstance(String typeId, String instanceId, String instanceData, String ric) {
+        void putInstance(String typeId, String instanceId, Object instanceData, String ric) {
             PolicyInfo i = ImmutablePolicyInfo.builder().json(instanceData).lastModified(getTimeStampUTC())
                 .id(instanceId).ric(ric).service("service").type(typeId).build();
             instances.put(instanceId, i);
@@ -159,7 +157,7 @@ public class PolicyControllerMockConfiguration {
             instances.remove(instanceId);
         }
 
-        String getInstance(String id) throws RestClientException {
+        Object getInstance(String id) throws RestClientException {
             PolicyInfo i = instances.get(id);
             if (i == null) {
                 throw new RestClientException("Type not found: " + id);