Fix Code smells
[cps.git] / cps-rest / src / main / java / org / onap / cps / rest / controller / DataRestController.java
index f466ebc..c8b7412 100755 (executable)
 
 package org.onap.cps.rest.controller;
 
-import javax.validation.Valid;
-import javax.validation.constraints.NotNull;
 import org.onap.cps.api.CpsDataService;
 import org.onap.cps.rest.api.CpsDataApi;
 import org.onap.cps.spi.FetchDescendantsOption;
-import org.onap.cps.spi.model.DataNode;
 import org.onap.cps.utils.DataMapUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpStatus;
@@ -38,13 +35,19 @@ import org.springframework.web.bind.annotation.RestController;
 @RequestMapping("${rest.api.cps-base-path}")
 public class DataRestController implements CpsDataApi {
 
+    private static final String ROOT_XPATH = "/";
+
     @Autowired
     private CpsDataService cpsDataService;
 
     @Override
-    public ResponseEntity<String> createNode(@Valid final String jsonData, @NotNull final String dataspaceName,
-        @NotNull @Valid final String anchorName) {
-        cpsDataService.saveData(dataspaceName, anchorName, jsonData);
+    public ResponseEntity<String> createNode(final String jsonData, final String dataspaceName, final String anchorName,
+        final String parentNodeXpath) {
+        if (isRootXpath(parentNodeXpath)) {
+            cpsDataService.saveData(dataspaceName, anchorName, jsonData);
+        } else {
+            cpsDataService.saveData(dataspaceName, anchorName, parentNodeXpath, jsonData);
+        }
         return new ResponseEntity<>(HttpStatus.CREATED);
     }
 
@@ -56,13 +59,10 @@ public class DataRestController implements CpsDataApi {
     @Override
     public ResponseEntity<Object> getNodeByDataspaceAndAnchor(final String dataspaceName, final String anchorName,
         final String xpath, final Boolean includeDescendants) {
-        if ("/".equals(xpath)) {
-            return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
-        }
         final FetchDescendantsOption fetchDescendantsOption = Boolean.TRUE.equals(includeDescendants)
             ? FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS : FetchDescendantsOption.OMIT_DESCENDANTS;
-        final DataNode dataNode =
-            cpsDataService.getDataNode(dataspaceName, anchorName, xpath, fetchDescendantsOption);
+        final var dataNode = cpsDataService.getDataNode(dataspaceName, anchorName, xpath,
+                fetchDescendantsOption);
         return new ResponseEntity<>(DataMapUtils.toDataMap(dataNode), HttpStatus.OK);
     }
 
@@ -79,4 +79,8 @@ public class DataRestController implements CpsDataApi {
         cpsDataService.replaceNodeTree(dataspaceName, anchorName, parentNodeXpath, jsonData);
         return new ResponseEntity<>(HttpStatus.OK);
     }
+
+    private static boolean isRootXpath(final String xpath) {
+        return ROOT_XPATH.equals(xpath);
+    }
 }