/**
- * ============LICENSE_START=======================================================
+ * ============LICENSE_START=======================================================
* org.onap.aai
* ================================================================================
* Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
import static org.junit.Assert.assertThat;
import java.io.IOException;
+import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.Charset;
+import java.nio.file.Files;
+import java.nio.file.Paths;
import java.util.Base64;
import java.util.List;
import java.util.Set;
+import java.util.function.Function;
import java.util.stream.Collectors;
import org.apache.commons.io.IOUtils;
import org.custommonkey.xmlunit.Diff;
*/
public class ArtifactTestUtils {
+ private static final String JSON_REQUESTS_FOLDER = "jsonFiles/";
+ private static final String JSON_RESPONSES_FOLDER = "response/";
+ private static final String CSAR_INPUTS_FOLDER = "compressedArtifacts/";
+
public void performYmlAsserts(List<Artifact> toscaFiles, List<String> ymlPayloadsToLoad) {
- assertThat("An unexpected number of yml files have been extracted", toscaFiles.size(),
+ assertThat("An unexpected number of YAML files have been extracted", toscaFiles.size(),
is(ymlPayloadsToLoad.size()));
- Set<String> ymlPayloads = ymlPayloadsToLoad.stream().map(s -> {
+ Function<? super String, ? extends String> loadResource = s -> {
try {
return loadResourceAsString(s);
} catch (IOException e) {
throw new RuntimeException(e);
}
- }).collect(Collectors.toSet());
-
- compareXMLPayloads(toscaFiles, ymlPayloads);
+ };
+ Set<String> ymlPayloads = ymlPayloadsToLoad.stream().map(loadResource).collect(Collectors.toSet());
+ compareXmlPayloads(toscaFiles, ymlPayloads);
}
/**
* @param string2
* @return true if similar
*/
- public boolean compareXMLStrings(String string1, String string2) {
+ public boolean compareXmlStrings(String string1, String string2) {
boolean similar = false;
try {
return similar;
}
- public byte[] loadResource(String resourceName) throws IOException {
+ public byte[] getCompressedArtifact(String resourceName) throws IOException {
+ return loadResourceBytes(CSAR_INPUTS_FOLDER + resourceName);
+ }
+
+ public byte[] loadResourceBytes(String resourceName) throws IOException {
return IOUtils.toByteArray(getResource(resourceName));
}
return IOUtils.toString(getResource(resourceName), Charset.defaultCharset());
}
- private void compareXMLPayloads(List<Artifact> toscaFiles, Set<String> ymlPayloads) {
+ public String getRequestJson(String resource) throws IOException {
+ return loadResourceAsString(JSON_REQUESTS_FOLDER + resource);
+ }
+
+ public String getResponseJson(String jsonResponse) throws IOException, URISyntaxException {
+ return readstringFromFile(JSON_RESPONSES_FOLDER + jsonResponse);
+ }
+
+ public String readstringFromFile(String resourceFile) throws IOException, URISyntaxException {
+ return Files.lines(Paths.get(getResource(resourceFile).toURI())).collect(Collectors.joining());
+ }
+
+ public String getResourcePath(String resourceName) {
+ return getResource(resourceName).getPath();
+ }
+
+ private URL getResource(String resourceName) {
+ return ArtifactTestUtils.class.getClassLoader().getResource(resourceName);
+ }
+
+ private void compareXmlPayloads(List<Artifact> toscaFiles, Set<String> ymlPayloads) {
for (Artifact artifact : toscaFiles) {
boolean payloadFound = false;
for (String ymlPayload : ymlPayloads) {
- if (compareXMLStrings(convertToString(artifact.getPayload()), ymlPayload)) {
+ if (compareXmlStrings(convertToString(artifact.getPayload()), ymlPayload)) {
payloadFound = true;
break;
}
}
- assertThat("The content of each yml file must match the actual content of the file extracted ("
+ assertThat("The content of each YAML file must match the actual content of the file extracted ("
+ artifact.getName() + ")", payloadFound, is(true));
}
}
- private URL getResource(String resourceName) {
- return ArtifactTestUtils.class.getClassLoader().getResource(resourceName);
- }
-
private String convertToString(byte[] byteArray) {
return new String(Base64.getDecoder().decode(byteArray), Charset.defaultCharset());
}
+
}