Fix for APPC-1263 57/73957/9
authorJoss Armstrong <joss.armstrong@ericsson.com>
Thu, 29 Nov 2018 15:57:37 +0000 (15:57 +0000)
committerJoss Armstrong <joss.armstrong@ericsson.com>
Fri, 30 Nov 2018 15:04:39 +0000 (15:04 +0000)
Removal of unused code. Refactoring of duplicated code and
replacement with FileUtils library function and adding test
case for ArtifactTransformer

Issue-ID: APPC-1263
Change-Id: I4eab0cefa09620bae18c08ec2c4307d54dddd872
Signed-off-by: Joss Armstrong <joss.armstrong@ericsson.com>
appc-config/appc-config-params/provider/src/main/java/org/onap/sdnc/config/params/transformer/ArtificatTransformer.java
appc-config/appc-config-params/provider/src/test/java/org/onap/sdnc/config/params/transformer/TestArtifactTransformer.java [new file with mode: 0644]
appc-config/appc-config-params/provider/src/test/java/org/onap/sdnc/config/params/transformer/tosca/TestArtifactProcessor.java
appc-config/appc-config-params/provider/src/test/java/org/onap/sdnc/config/params/transformer/tosca/TestGenerateArtifactObject.java
appc-config/appc-config-params/provider/src/test/java/org/onap/sdnc/config/params/transformer/tosca/TestGenerateArtifactString.java
appc-config/appc-config-params/provider/src/test/java/org/onap/sdnc/config/params/transformer/tosca/TestReadArtifact.java

index dcd3884..87d66cd 100644 (file)
@@ -5,6 +5,8 @@
  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Copyright (C) 2017 Amdocs
+ * ================================================================================
+ * Modifications Copyright (C) 2018 Ericsson
  * =============================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -24,9 +26,7 @@
 package org.onap.sdnc.config.params.transformer;
 
 import java.io.IOException;
-import java.util.List;
 import org.apache.commons.lang3.StringUtils;
-import org.onap.sdnc.config.params.data.Parameter;
 import org.onap.sdnc.config.params.data.PropertyDefinition;
 import com.fasterxml.jackson.core.JsonParseException;
 import com.fasterxml.jackson.databind.JsonMappingException;
@@ -66,23 +66,4 @@ public class ArtificatTransformer {
         }
         return propertyDefinition;
     }
-
-    public String convertYAMLToParams(String pdContent)
-            throws JsonParseException, JsonMappingException, IOException {
-        String paramJson = null;
-        if (StringUtils.isNotBlank(pdContent)) {
-            paramJson = convertPdToParams(convertYAMLToPD(pdContent));
-        }
-        return paramJson;
-    }
-
-    public String convertPdToParams(PropertyDefinition propertyDefinition)
-            throws JsonParseException, JsonMappingException, IOException {
-        String paramJson = null;
-        if (propertyDefinition != null && propertyDefinition.getParameters() != null) {
-            List<Parameter> parameters = propertyDefinition.getParameters();
-
-        }
-        return paramJson;
-    }
 }
diff --git a/appc-config/appc-config-params/provider/src/test/java/org/onap/sdnc/config/params/transformer/TestArtifactTransformer.java b/appc-config/appc-config-params/provider/src/test/java/org/onap/sdnc/config/params/transformer/TestArtifactTransformer.java
new file mode 100644 (file)
index 0000000..003f7e8
--- /dev/null
@@ -0,0 +1,48 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP : APPC
+ * ================================================================================
+ * Copyright (C) 2018 Ericsson
+ * =============================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * 
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.sdnc.config.params.transformer;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.charset.Charset;
+import org.apache.commons.io.FileUtils;
+import org.hamcrest.CoreMatchers;
+import org.junit.Assert;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.onap.sdnc.config.params.transformer.ArtificatTransformer;
+
+public class TestArtifactTransformer {
+    @Rule
+    public TemporaryFolder temporaryFolder = new TemporaryFolder();
+
+    @Test
+    public void testArtifactTransformerYAMLtoJSON() throws IOException {
+        ArtificatTransformer transformer = new ArtificatTransformer();
+        String pdString = FileUtils.readFileToString(
+                new File("src/test/resources/tosca/ExamplePropertyDefinition.yml"), 
+                Charset.defaultCharset());
+        String output = transformer.transformYamlToJson(pdString);
+        Assert.assertThat(output, CoreMatchers.containsString("response-keys"));
+    }
+}
index d9b31e4..df2147c 100644 (file)
@@ -5,6 +5,8 @@
  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Copyright (C) 2017 Amdocs
+ * ================================================================================
+ * Modifications Copyright (C) 2018 Ericsson
  * =============================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 package org.onap.sdnc.config.params.transformer.tosca;
 
-import java.io.BufferedReader;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
 import java.io.OutputStream;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import org.apache.commons.io.FileUtils;
 import org.junit.Assert;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.TemporaryFolder;
-import org.onap.sdnc.config.params.data.PropertyDefinition;
-import org.onap.sdnc.config.params.transformer.ArtificatTransformer;
 import org.onap.sdnc.config.params.transformer.tosca.exceptions.ArtifactProcessorException;
 
 public class TestArtifactProcessor {
@@ -46,10 +45,10 @@ public class TestArtifactProcessor {
 
     @Test
     public void testArtifactProcessor() throws IOException, ArtifactProcessorException {
-
         ArtifactProcessor arp = ArtifactProcessorFactory.getArtifactProcessor();
-
-        String pdString = getFileContent("tosca/ExamplePropertyDefinition.yml");
+        String pdString = FileUtils.readFileToString(
+                new File("src/test/resources/tosca/ExamplePropertyDefinition.yml"),
+                Charset.defaultCharset());
         OutputStream outstream = null;
 
         File tempFile = temporaryFolder.newFile("TestTosca.yml");
@@ -58,9 +57,11 @@ public class TestArtifactProcessor {
         outstream.flush();
         outstream.close();
 
-        String expectedTosca = getFileContent("tosca/ExpectedTosca.yml");
-        String toscaString = getFileContent(tempFile);
-        Assert.assertEquals(expectedTosca, toscaString);
+        String expectedTosca = FileUtils.readFileToString(
+                new File("src/test/resources/tosca/ExpectedTosca.yml"),
+                StandardCharsets.UTF_8);
+        String toscaString = FileUtils.readFileToString(tempFile, StandardCharsets.UTF_8);
+        Assert.assertEquals(expectedTosca.replaceAll("\\r\\n", "\n"), toscaString.replaceAll("\\r\\n", "\n"));
     }
 
     @Test
@@ -69,7 +70,9 @@ public class TestArtifactProcessor {
 
         ArtifactProcessor arp = ArtifactProcessorFactory.getArtifactProcessor();
 
-        String pdString = getFileContent("tosca/ExamplePropertyDefinition.yml");
+        String pdString = FileUtils.readFileToString(
+                new File("src/test/resources/tosca/ExamplePropertyDefinition.yml"),
+                Charset.defaultCharset());
         OutputStream outstream = null;
 
         outstream = new ByteArrayOutputStream();
@@ -77,46 +80,10 @@ public class TestArtifactProcessor {
         outstream.flush();
         outstream.close();
 
-        String expectedTosca = getFileContent("tosca/ExpectedTosca.yml");
+        String expectedTosca = FileUtils.readFileToString(
+                new File("src/test/resources/tosca/ExpectedTosca.yml"),
+                Charset.defaultCharset());
         String toscaString = outstream.toString();
-    }
-
-    @Test
-    public void testReadArtifact() throws IOException, ArtifactProcessorException {
-        ArtifactProcessor arp = ArtifactProcessorFactory.getArtifactProcessor();
-        String pdString = getFileContent("tosca/ExpectedTosca.yml");
-        PropertyDefinition propertyDefinitionObj = arp.readArtifact(pdString);
-        Assert.assertEquals(7, propertyDefinitionObj.getParameters().size());
-    }
-
-    private String getFileContent(String fileName) throws IOException {
-        ClassLoader classLoader = new TestArtifactProcessor().getClass().getClassLoader();
-        InputStream is = new FileInputStream(classLoader.getResource(fileName).getFile());
-        BufferedReader buf = new BufferedReader(new InputStreamReader(is));
-        String line = buf.readLine();
-        StringBuilder sb = new StringBuilder();
-
-        while (line != null) {
-            sb.append(line).append("\n");
-            line = buf.readLine();
-        }
-        String fileString = sb.toString();
-        is.close();
-        return fileString;
-    }
-
-    private String getFileContent(File file) throws IOException {
-        InputStream is = new FileInputStream(file);
-        BufferedReader buf = new BufferedReader(new InputStreamReader(is));
-        String line = buf.readLine();
-        StringBuilder sb = new StringBuilder();
-
-        while (line != null) {
-            sb.append(line).append("\n");
-            line = buf.readLine();
-        }
-        String fileString = sb.toString();
-        is.close();
-        return fileString;
+        Assert.assertEquals(expectedTosca.replaceAll("\\r\\n", "\n"), toscaString.replaceAll("\\r\\n", "\n"));
     }
 }
index f570b4b..766f0d9 100644 (file)
@@ -5,6 +5,8 @@
  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Copyright (C) 2017 Amdocs
+ * ================================================================================
+ * Modifications Copyright (C) 2018 Ericsson
  * =============================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 package org.onap.sdnc.config.params.transformer.tosca;
 
-import java.io.BufferedReader;
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
 import java.io.OutputStream;
+import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.List;
+import org.apache.commons.io.FileUtils;
 import org.junit.Assert;
 import org.junit.Rule;
 import org.junit.Test;
@@ -77,7 +77,7 @@ public class TestGenerateArtifactObject {
         outstream.flush();
         outstream.close();
 
-        String toscaString = getFileContent(toscaFile);
+        String toscaString = FileUtils.readFileToString(toscaFile, StandardCharsets.UTF_8);
         Assert.assertEquals(expectedTosca, toscaString);
 
     }
@@ -102,7 +102,8 @@ public class TestGenerateArtifactObject {
         outstream.flush();
         outstream.close();
 
-        String toscaString = getFileContent(toscaFile);
+        String toscaString = FileUtils.readFileToString(toscaFile, StandardCharsets.UTF_8);
+
         Assert.assertEquals(expectedTosca, toscaString);
     }
 
@@ -194,17 +195,6 @@ public class TestGenerateArtifactObject {
         return responseKeyList;
     }
 
-    private Parameter createParameter() {
-        Parameter singleParameter1 = new Parameter();
-        singleParameter1.setName("LOCAL_ACCESS_IP_ADDR");
-        singleParameter1.setRequired(false);
-        singleParameter1.setSource("source");
-        singleParameter1.setDefaultValue("0.0.0.0");
-        singleParameter1.setRequestKeys(createRequestKeys());
-        singleParameter1.setResponseKeys(createResponseKeys());
-        return singleParameter1;
-    }
-
     @Test(expected = Exception.class)
     public void testPDnull() throws IOException, ArtifactProcessorException {
         PropertyDefinition pd = null;
@@ -216,19 +206,4 @@ public class TestGenerateArtifactObject {
         outstream.flush();
         outstream.close();
     }
-
-    private String getFileContent(File file) throws IOException {
-        InputStream is = new FileInputStream(file);
-        BufferedReader buf = new BufferedReader(new InputStreamReader(is));
-        String line = buf.readLine();
-        StringBuilder sb = new StringBuilder();
-
-        while (line != null) {
-            sb.append(line).append("\n");
-            line = buf.readLine();
-        }
-        String fileString = sb.toString();
-        is.close();
-        return fileString;
-    }
 }
index 76eddb0..79e9dac 100644 (file)
@@ -5,6 +5,8 @@
  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Copyright (C) 2017 Amdocs
+ * ================================================================================
+ * Modifications Copyright (C) 2018 Ericsson
  * =============================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 package org.onap.sdnc.config.params.transformer.tosca;
 
-import java.io.BufferedReader;
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
 import java.io.OutputStream;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import org.apache.commons.io.FileUtils;
 import org.junit.Assert;
 import org.junit.Rule;
 import org.junit.Test;
@@ -45,7 +46,9 @@ public class TestGenerateArtifactString {
     @Test
     public void testStringArtifactGenerator() throws IOException, ArtifactProcessorException {
 
-        String pdString = getFileContent("tosca/ExamplePropertyDefinition.yml");
+        String pdString = FileUtils.readFileToString(
+                new File("src/test/resources/tosca/ExamplePropertyDefinition.yml"),
+                Charset.defaultCharset());
         OutputStream outstream = null;
 
         File tempFile = temporaryFolder.newFile("TestTosca.yml");
@@ -55,9 +58,11 @@ public class TestGenerateArtifactString {
         outstream.flush();
         outstream.close();
 
-        String expectedTosca = getFileContent("tosca/ExpectedTosca.yml");
-        String toscaString = getFileContent(tempFile);
-        Assert.assertEquals(expectedTosca, toscaString);
+        String expectedTosca = FileUtils.readFileToString(
+                new File("src/test/resources/tosca/ExpectedTosca.yml"),
+                StandardCharsets.UTF_8);
+        String toscaString = FileUtils.readFileToString(tempFile, StandardCharsets.UTF_8);
+        Assert.assertEquals(expectedTosca.replaceAll("\\r\\n", "\n"), toscaString.replaceAll("\\r\\n", "\n"));
 
     }
 
@@ -65,7 +70,9 @@ public class TestGenerateArtifactString {
     public void testArtifactGeneratorWithParameterNameBlank()
             throws IOException, ArtifactProcessorException {
 
-        String pdString = getFileContent("tosca/ExamplePropertyDefinition2.yml");
+        String pdString = FileUtils.readFileToString(
+                new File("src/test/resources/tosca/ExamplePropertyDefinition2.yml"),
+                Charset.defaultCharset());
         OutputStream outstream = null;
         String expectedMsg = "Parameter name is empty,null or contains whitespace";
 
@@ -85,7 +92,9 @@ public class TestGenerateArtifactString {
     public void testArtifactGeneratorWithParameterNameNull()
             throws IOException, ArtifactProcessorException {
 
-        String pdString = getFileContent("tosca/ExamplePropertyDefinition3.yml");
+        String pdString = FileUtils.readFileToString(
+                new File("src/test/resources/tosca/ExamplePropertyDefinition3.yml"),
+                Charset.defaultCharset());
         OutputStream outstream = null;
         String expectedMsg = "Parameter name is empty,null or contains whitespace";
 
@@ -103,8 +112,9 @@ public class TestGenerateArtifactString {
 
     @Test
     public void testArtifactGeneratorWithKindNull() throws IOException, ArtifactProcessorException {
-
-        String pdString = getFileContent("tosca/ExamplePropertyDefinition4.yml");
+        String pdString = FileUtils.readFileToString(
+                new File("src/test/resources/tosca/ExamplePropertyDefinition4.yml"),
+                Charset.defaultCharset());
         OutputStream outstream = null;
         String expectedMsg = "Kind in PropertyDefinition is blank or null";
 
@@ -119,35 +129,4 @@ public class TestGenerateArtifactString {
         outstream.flush();
         outstream.close();
     }
-
-    private String getFileContent(String fileName) throws IOException {
-        ClassLoader classLoader = new TestGenerateArtifactString().getClass().getClassLoader();
-        InputStream is = new FileInputStream(classLoader.getResource(fileName).getFile());
-        BufferedReader buf = new BufferedReader(new InputStreamReader(is));
-        String line = buf.readLine();
-        StringBuilder sb = new StringBuilder();
-
-        while (line != null) {
-            sb.append(line).append("\n");
-            line = buf.readLine();
-        }
-        String fileString = sb.toString();
-        is.close();
-        return fileString;
-    }
-
-    private String getFileContent(File file) throws IOException {
-        InputStream is = new FileInputStream(file);
-        BufferedReader buf = new BufferedReader(new InputStreamReader(is));
-        String line = buf.readLine();
-        StringBuilder sb = new StringBuilder();
-
-        while (line != null) {
-            sb.append(line).append("\n");
-            line = buf.readLine();
-        }
-        String fileString = sb.toString();
-        is.close();
-        return fileString;
-    }
 }
index 97a2458..697b90b 100644 (file)
@@ -5,6 +5,8 @@
  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Copyright (C) 2017 Amdocs
+ * ================================================================================
+ * Modifications Copyright (C) 2018 Ericsson
  * =============================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 package org.onap.sdnc.config.params.transformer.tosca;
 
-import java.io.BufferedReader;
-import java.io.FileInputStream;
+import java.io.File;
 import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
+import java.nio.charset.StandardCharsets;
+import org.apache.commons.io.FileUtils;
 import org.junit.Assert;
 import org.junit.Test;
 import org.onap.sdnc.config.params.data.PropertyDefinition;
 import org.onap.sdnc.config.params.transformer.tosca.exceptions.ArtifactProcessorException;
 
 public class TestReadArtifact {
+
     @Test
     public void testReadArtifactPositive() throws ArtifactProcessorException, IOException {
-
-        String toscaArtifact = getFileContent("tosca/ReadArtifactPositiveInputTosca.yml");
+        String toscaArtifact = FileUtils.readFileToString(
+                new File("src/test/resources/tosca/ReadArtifactPositiveInputTosca.yml"),
+                StandardCharsets.UTF_8);
         ArtifactProcessorImpl artifact = new ArtifactProcessorImpl();
         PropertyDefinition ouptPD = artifact.readArtifact(toscaArtifact);
         Assert.assertEquals(ouptPD.getKind(), "Property Definition");
         Assert.assertEquals(ouptPD.getVersion(), "V1");
-
         Assert.assertEquals(ouptPD.getParameters().get(0).getDefaultValue(), "0.0.0.0");
         Assert.assertEquals(ouptPD.getParameters().get(0).getName(), "abc");
         Assert.assertEquals(ouptPD.getParameters().get(0).getSource(), "source");
@@ -62,7 +64,6 @@ public class TestReadArtifact {
                 "address-0");
         Assert.assertEquals(
                 ouptPD.getParameters().get(0).getResponseKeys().get(0).getFieldKeyName(), "0");
-
         Assert.assertEquals(ouptPD.getParameters().get(1).getDefaultValue(), "value");
         Assert.assertEquals(ouptPD.getParameters().get(1).getName(), "param 2");
         Assert.assertEquals(ouptPD.getParameters().get(1).getSource(), "source");
@@ -84,13 +85,13 @@ public class TestReadArtifact {
                 ouptPD.getParameters().get(1).getResponseKeys().get(0).getUniqueKeyValue(), "0");
         Assert.assertEquals(
                 ouptPD.getParameters().get(1).getResponseKeys().get(0).getFieldKeyName(), "0");
-
     }
 
     @Test
     public void testReadArtifactNegetive() throws IOException {
-
-        String toscaArtifact = getFileContent("tosca/ReadArtifactNegetiveInputTosca.yml");
+        String toscaArtifact = FileUtils.readFileToString(
+                new File("src/test/resources/tosca/ReadArtifactNegetiveInputTosca.yml"),
+                StandardCharsets.UTF_8);
         ArtifactProcessorImpl artifact = new ArtifactProcessorImpl();
         try {
             PropertyDefinition ouptPD = artifact.readArtifact(toscaArtifact);
@@ -101,20 +102,4 @@ public class TestReadArtifact {
         }
     }
 
-    private String getFileContent(String fileName) throws IOException {
-        ClassLoader classLoader = new TestReadArtifact().getClass().getClassLoader();
-        InputStream is = new FileInputStream(classLoader.getResource(fileName).getFile());
-        BufferedReader buf = new BufferedReader(new InputStreamReader(is));
-        String line = buf.readLine();
-        StringBuilder sb = new StringBuilder();
-
-        while (line != null) {
-            sb.append(line).append("\n");
-            line = buf.readLine();
-        }
-        String fileString = sb.toString();
-        is.close();
-        return fileString;
-    }
-
 }