Remove extra brackets in the result of output transformation 53/127653/5
authorNiranjana <niranjana.y60@wipro.com>
Thu, 10 Mar 2022 11:52:19 +0000 (11:52 +0000)
committerNiranjana <niranjana.y60@wipro.com>
Mon, 14 Mar 2022 13:52:55 +0000 (13:52 +0000)
Issue-ID: CPS-920
Signed-off-by: Niranjana <niranjana.y60@wipro.com>
Change-Id: I130f80c687bb2a9ccdf0ed45c387d7cfc167face

cps-tbdmt-dependencies/pom.xml
cps-tbdmt-rest/pom.xml
cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/service/ExecutionBusinessLogic.java
cps-tbdmt-service/src/test/java/org/onap/cps/tbdmt/service/ExecutionBusinessLogicTest.java

index 3aa4b05..d9fc6b6 100644 (file)
                 <type>pom</type>
                 <scope>import</scope>
             </dependency>
-            <dependency>
-                <groupId>org.springframework.boot</groupId>
-                <artifactId>spring-boot-starter-jetty</artifactId>
-                <version>2.5.5</version>
-                <exclusions>
-                    <exclusion>
-                        <groupId>org.eclipse.jetty</groupId>
-                        <artifactId>jetty-io</artifactId>
-                    </exclusion>
-                    <exclusion>
-                        <groupId>org.eclipse.jetty</groupId>
-                        <artifactId>jetty-servlets</artifactId>
-                    </exclusion>
-                    <exclusion>
-                        <groupId>org.eclipse.jetty</groupId>
-                        <artifactId>jetty-http</artifactId>
-                    </exclusion>
-                    <exclusion>
-                        <groupId>org.glassfish</groupId>
-                        <artifactId>jakarta.el</artifactId>
-                    </exclusion>
-                </exclusions>
-            </dependency>
-            <dependency>
-                <groupId>org.eclipse.jetty</groupId>
-                <artifactId>jetty-io</artifactId>
-                <version>11.0.7</version>
-            </dependency>
-            <dependency>
-                <groupId>org.eclipse.jetty</groupId>
-                <artifactId>jetty-servlets</artifactId>
-                <version>11.0.7</version>
-            </dependency>
-            <dependency>
-                <groupId>org.eclipse.jetty</groupId>
-                <artifactId>jetty-http</artifactId>
-                <version>11.0.7</version>
-            </dependency>
             <dependency>
                 <groupId>org.bitbucket.dunglv90</groupId>
                 <artifactId>dp-utility</artifactId>
index f1e6cb8..2ff830b 100644 (file)
     <modelVersion>4.0.0</modelVersion>
 
     <artifactId>cps-tbdmt-rest</artifactId>
+
+    <properties>
+        <jetty.version>11.0.7</jetty.version>
+    </properties>
+
     <dependencies>
         <dependency>
             <groupId>${project.groupId}</groupId>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-jetty</artifactId>
             <version>2.5.5</version>
+            <exclusions>
+                <exclusion>
+                    <groupId>org.eclipse.jetty</groupId>
+                    <artifactId>jetty-io</artifactId>
+                </exclusion>
+                <exclusion>
+                    <groupId>org.eclipse.jetty</groupId>
+                    <artifactId>jetty-servlets</artifactId>
+                </exclusion>
+                <exclusion>
+                    <groupId>org.eclipse.jetty</groupId>
+                    <artifactId>jetty-http</artifactId>
+                </exclusion>
+               <exclusion>
+                    <groupId>org.glassfish</groupId>
+                    <artifactId>jakarta.el</artifactId>
+                </exclusion>
+            </exclusions>
         </dependency>
         <dependency>
             <groupId>org.eclipse.jetty</groupId>
             <artifactId>jetty-io</artifactId>
+            <version>${jetty.version}</version>
         </dependency>
         <dependency>
             <groupId>org.eclipse.jetty</groupId>
             <artifactId>jetty-servlets</artifactId>
+            <version>${jetty.version}</version>
         </dependency>
         <dependency>
             <groupId>org.eclipse.jetty</groupId>
             <artifactId>jetty-http</artifactId>
+            <version>${jetty.version}</version>
         </dependency>
         <dependency>
             <groupId>junit</groupId>
index 5b3bc60..1c0f285 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * ONAP
  * ================================================================================
- * Copyright (C) 2021 Wipro Limited.
+ * Copyright (C) 2021-2022 Wipro Limited.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -150,7 +150,7 @@ public class ExecutionBusinessLogic {
                     return result;
                 } else {
                     final List<JsonElement> json = transform(template, result);
-                    return new Gson().toJson(json);
+                    return removeExtraBracketsIfAny(json, result);
                 }
             }
         } catch (final CpsClientException e) {
@@ -209,6 +209,30 @@ public class ExecutionBusinessLogic {
 
     }
 
+    private static String removeExtraBracketsIfAny(final List<JsonElement> jsonElementList, final String result) {
+
+        final Gson gson = new Gson();
+        final List<JsonElement> updatedResult = new ArrayList<>();
+        if (jsonElementList.size() == 1) {
+            if (gson.fromJson(result, JsonElement.class).isJsonArray()) {
+                return gson.toJson(jsonElementList);
+            }
+            return gson.toJson(jsonElementList.get(0));
+        }
+        if (jsonElementList.size() > 1) {
+            jsonElementList.forEach(jsonElement -> {
+                if (jsonElement.isJsonArray() && jsonElement.getAsJsonArray().size() == 1) {
+                    updatedResult.add(jsonElement.getAsJsonArray().get(0));
+                } else {
+                    updatedResult.add(jsonElement);
+                }
+            });
+            return gson.toJson(updatedResult);
+        }
+        return gson.toJson(jsonElementList);
+
+    }
+
     private String generateXpath(final String xpathTemplate, final Map<String, String> templateParameters) {
         return new Jinjava().render(xpathTemplate, templateParameters);
     }
index 56c55c0..9cee472 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * ONAP
  * ================================================================================
- * Copyright (C) 2021 Wipro Limited.
+ * Copyright (C) 2021-2022 Wipro Limited.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -196,7 +196,7 @@ public class ExecutionBusinessLogicTest {
                 new Template("get-tree", "ran-network", "/test-tree", "get", true, null, transformParam1);
         final Template template2 = new Template("get-branch", "ran-network", "/test-tree/branch[@name='{{name}}']/nest",
                 "get", true, "get-tree", transformParam2);
-        final String transformedResult = "[[\"Big\"], [\"Small\"]]";
+        final String transformedResult = "[\"Big\", \"Small\"]";
 
         try {
             final String result1 = readFromFile("sample_multiple_query_data_1.json");
@@ -254,6 +254,39 @@ public class ExecutionBusinessLogicTest {
         executionBusinessLogic.executeTemplate("ran-net", "deleteNbr", request);
     }
 
+    @Test
+    public void testRemoveExtraBracketsIfAny() {
+        final Map<String, String> input = new HashMap<>();
+        input.put("idNearRTRIC", "11");
+        final String transformParam1 = "branch, nest, birds";
+        final Template template1 =
+                new Template("get-tree", "ran-network", "/test-tree", "get", true, null, transformParam1);
+        final String transformParam2 = "GNBDUFunction, NRCellDU, attributes, nRSectorCarrierRef";
+        final Template template2 = new Template("get-nrcelldu-data", "ran-network", "/NearRTRIC/[@idNearRTRIC='11']",
+                "get", true, null, transformParam2);
+        final String transformedResult1 = "[[\"Owl\",\"Raven\",\"Crow\"],[\"Robin\",\"Sparrow\",\"Finch\"]]";
+        final String transformedResult2 = "[\"OU\\u003dSales\",\"OU\\u003dSales\"]";
+
+        try {
+            final String result1 = readFromFile("sample_multiple_query_data_1.json");
+            Mockito.when(cpsRestClient.fetchNode("ran-network", "/test-tree", "get", true)).thenReturn(result1);
+            Mockito.when(templateRepository.findById(ArgumentMatchers.any())).thenReturn(Optional.of(template1));
+            final String result2 = readFromFile("sample_transform_query_data.json");
+            Mockito.when(cpsRestClient.fetchNode("ran-network", "/NearRTRIC/[@idNearRTRIC='11']", "get", true))
+                    .thenReturn(result2);
+            final TemplateKey key = new TemplateKey("get-nrcelldu-data");
+            Mockito.when(templateRepository.findById(key)).thenReturn(Optional.of(template2));
+            assertEquals(transformedResult1,
+                    executionBusinessLogic.executeTemplate("ran-network", "get-tree", request));
+            assertEquals(transformedResult2,
+                    executionBusinessLogic.executeTemplate("ran-network", "get-nrcelldu-data", request));
+        } catch (final CpsClientException e) {
+            e.printStackTrace();
+        } catch (final Exception e) {
+            e.printStackTrace();
+        }
+    }
+
     /**
      * Reads a file from classpath.
      *