Merge "JUnit additions for XACML,REST,POLICY-SDK"
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / onap / policy / controller / CreateDcaeMicroServiceController.java
index 5d626ae..ab4ea6d 100644 (file)
@@ -433,7 +433,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                return settings; 
        } 
 
-       public Map<String, String> load(byte[] source) throws IOException 
+       public Map<String, String> load(byte[] source) { 
                Yaml yaml = new Yaml(); 
                @SuppressWarnings("unchecked")
                Map<Object, Object> yamlMap = (Map<Object, Object>) yaml.load(Arrays.toString(source)); 
@@ -701,6 +701,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                                //List Datatype
                                Set<String> keys= keyValues.keySet();
                                Iterator<String> itr=keys.iterator();
+                               boolean isDefinedType = false;
                                while(itr.hasNext()){
                                        String key= itr.next();
                                        if(!("type").equals(key) ||("required").equals(key))
@@ -713,18 +714,19 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                                                            constraints.add(keyValues.get(key));
                                                        }
                                                }else{
-                                                       //This is user defined string
+                                                       //This is user defined type
                                                        String trimValue=value.substring(value.lastIndexOf('.')+1);
                                                        StringBuilder referenceIndividualStringBuilder= new StringBuilder();
                                                        referenceIndividualStringBuilder.append(keySetString+"="+trimValue+":MANY-true");
                                                        referenceStringBuilder.append(referenceIndividualStringBuilder+",");
+                                                       isDefinedType = true;
                                                }
                                        }                               
 
                                }
 
-                               if(keyValues.get("type").equalsIgnoreCase(LIST) &&
-                                       (constraints == null || constraints.isEmpty()) ) {
+                               if(!isDefinedType && keyValues.get("type").equalsIgnoreCase(LIST) &&
+                                       (constraints == null || constraints.isEmpty()) ) {   //type is list but no constraints defined.
                                                referenceStringBuilder.append(keySetString+"=MANY-true"+",");
                                }
                        }else{
@@ -831,7 +833,9 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                ObjectNode node = nodeFactory.objectNode();
                String prevKey = null;
                String presKey;
-               for(String key: element.keySet()){
+               for(Entry<String, String> entry: element.entrySet()){
+                       String key = entry.getKey();
+                       String value = entry.getValue();
                        if(key.contains(".")){
                                presKey = key.substring(0,key.indexOf('.'));
                        }else if(key.contains("@")){
@@ -859,7 +863,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                                        nodeKey = key.substring(0,key.indexOf('.'));
                                }
                                if(nodeKey.equals(key.substring(0,key.indexOf('.')))){
-                                       node.put(key.substring(key.indexOf('.')+1), element.get(key));
+                                       node.put(key.substring(key.indexOf('.')+1), value);
                                }else{
                                        if(node.size()!=0){
                                                if(nodeKey.contains("@")){
@@ -888,54 +892,30 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                                        if(nodeKey.contains("@")){
                                                arryKey = nodeKey.substring(0,nodeKey.indexOf('@'));
                                        }
-                                       node.put(key.substring(key.indexOf('.')+1), element.get(key));
+                                       node.put(key.substring(key.indexOf('.')+1), value);
                                }
-                       }else if(node.size()!=0){
-                               if(nodeKey.contains("@")){
-                                       if(arryKey==null){
-                                               arryKey = nodeKey.substring(0,nodeKey.indexOf('@'));
-                                       }
-                                       if(nodeKey.endsWith("@0")){
-                                               isArray = true;
-                                               jsonArray = new JSONArray();
-                                       }
-                                       if(jsonArray != null && arryKey.equals(nodeKey.substring(0,nodeKey.indexOf('@')))){
-                                               jsonArray.put(decodeContent(node));
-                                       }
-                                       jsonResult.put(arryKey, jsonArray);
-                                       jsonArray = new JSONArray();
-                                       arryKey = nodeKey.substring(0,nodeKey.indexOf('@'));
-                               }else{
-                                       isArray = false;
-                                       jsonResult.put(nodeKey, decodeContent(node));
-                               }
-                               node = nodeFactory.objectNode();
-                               if(key.contains("@")){
-                                       isArray = true;
-                                       if(key.endsWith("@0")|| jsonArray==null){
+                       }else {
+                               if(node.size()!=0){
+                                       if(nodeKey.contains("@")){
+                                               if(arryKey==null){
+                                                       arryKey = nodeKey.substring(0,nodeKey.indexOf('@'));
+                                               }
+                                               if(nodeKey.endsWith("@0")){
+                                                       isArray = true;
+                                                       jsonArray = new JSONArray();
+                                               }
+                                               if(jsonArray != null && arryKey.equals(nodeKey.substring(0,nodeKey.indexOf('@')))){
+                                                       jsonArray.put(decodeContent(node));
+                                               }
+                                               jsonResult.put(arryKey, jsonArray);
                                                jsonArray = new JSONArray();
-                                       }
-                               }else if(!key.contains("@")){
-                                       isArray = false;
-                               }
-                               if(isArray){
-                                       if(oldValue==null){
-                                               oldValue = key.substring(0,key.indexOf('@'));
-                                       }
-                                       if(oldValue!=prevKey){
-                                               oldValue = key.substring(0,key.indexOf('@'));
-                                       }
-                                       if(oldValue.equals(key.substring(0,key.indexOf('@')))){
-                                               jsonArray.put(element.get(key));
+                                               arryKey = nodeKey.substring(0,nodeKey.indexOf('@'));
                                        }else{
-                                               jsonResult.put(oldValue, jsonArray);
-                                               jsonArray = new JSONArray();
+                                               isArray = false;
+                                               jsonResult.put(nodeKey, decodeContent(node));
                                        }
-                                       oldValue = key.substring(0,key.indexOf('@'));
-                               }else{
-                                       jsonResult.put(key, element.get(key));
+                                       node = nodeFactory.objectNode();
                                }
-                       }else{
                                if(key.contains("@")){
                                        isArray = true;
                                        if(key.endsWith("@0")|| jsonArray==null){
@@ -952,14 +932,14 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                                                oldValue = key.substring(0,key.indexOf('@'));
                                        }
                                        if(oldValue.equals(key.substring(0,key.indexOf('@')))){
-                                               jsonArray.put(element.get(key));
+                                               jsonArray.put(value);
                                        }else{
                                                jsonResult.put(oldValue, jsonArray);
                                                jsonArray = new JSONArray();
                                        }
                                        oldValue = key.substring(0,key.indexOf('@'));
                                }else{
-                                       jsonResult.put(key, element.get(key));
+                                       jsonResult.put(key, value);
                                }
                        }
                }
@@ -1009,9 +989,9 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                        LOGGER.info("allkeys : " + allkeys);
                }
                
-               String allManyTrueKeys = "";
+               String nameOfTrueKey = "";
                if(allkeys != null){
-                       allManyTrueKeys = allkeys.toString();
+                       nameOfTrueKey = allkeys.toString();
                }
                
                String jsonModel = createMicroSeriveJson(returnModel, allkeys);
@@ -1055,10 +1035,10 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                PrintWriter out = response.getWriter();
                String responseString = mapper.writeValueAsString(returnModel);
                JSONObject j;
-               if("".equals(allManyTrueKeys)){
+               if("".equals(nameOfTrueKey)){
                        j = new JSONObject("{dcaeModelData: " + responseString + ",jsonValue: " + jsonModel + "}");     
                }else{
-                       j = new JSONObject("{dcaeModelData: " + responseString + ",jsonValue: " + jsonModel + ",allManyTrueKeys: " + allManyTrueKeys+ "}");     
+                       j = new JSONObject("{dcaeModelData: " + responseString + ",jsonValue: " + jsonModel + ",allManyTrueKeys: " + nameOfTrueKey+ "}");       
                }
                list.add(j);
                out.write(list.toString());
@@ -1459,8 +1439,8 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                        if(value instanceof LinkedHashMap<?, ?>){
                                LinkedHashMap<String, Object> secondObjec = new LinkedHashMap<>(); 
                                readRecursivlyJSONContent((LinkedHashMap<String, ?>) value, secondObjec);
-                               for(String objKey: secondObjec.keySet()){
-                                       data.put(key+"." +objKey, secondObjec.get(objKey));
+                               for( Entry<String, Object> entry : secondObjec.entrySet()){
+                                       data.put(key+"." + entry.getKey(), entry.getValue());
                                }
                        }else if(value instanceof ArrayList){
                                ArrayList<?> jsonArrayVal = (ArrayList<?>)value;
@@ -1469,8 +1449,8 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                                        if(arrayvalue instanceof LinkedHashMap<?, ?>){
                                                LinkedHashMap<String, Object> newData = new LinkedHashMap<>();   
                                                readRecursivlyJSONContent((LinkedHashMap<String, ?>) arrayvalue, newData);
-                                               for(String objKey: newData.keySet()){
-                                                       data.put(key+"@"+i+"." +objKey, newData.get(objKey));
+                                               for(Entry<String, Object> entry: newData.entrySet()){
+                                                       data.put(key+"@"+i+"." +entry.getKey(), entry.getValue());
                                                }
                                        }else if(arrayvalue instanceof ArrayList){
                                                ArrayList<?> jsonArrayVal1 = (ArrayList<?>)value;
@@ -1724,7 +1704,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
        private void retreiveDependency(String workingFile, Boolean modelClass) {
                
                MSModelUtils utils = new MSModelUtils(PolicyController.getMsOnapName(), PolicyController.getMsPolicyName());
-           HashMap<String, MSAttributeObject> tempMap;
+           Map<String, MSAttributeObject> tempMap;
            
            tempMap = utils.processEpackage(workingFile, MODEL_TYPE.XMI);
            
@@ -1736,9 +1716,9 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
        }
                
        private List<File> listModelFiles(String directoryName) {
-               File directory = new File(directoryName);
+               File fileDirectory = new File(directoryName);
                List<File> resultList = new ArrayList<>();
-               File[] fList = directory.listFiles();
+               File[] fList = fileDirectory.listFiles();
                for (File file : fList) {
                        if (file.isFile()) {
                                resultList.add(file);