2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.sdnc.config.params.transformer.tosca;
26 import java.io.BufferedReader;
28 import java.io.FileInputStream;
29 import java.io.FileOutputStream;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.InputStreamReader;
33 import java.io.OutputStream;
34 import org.junit.Assert;
35 import org.junit.Rule;
36 import org.junit.Test;
37 import org.junit.rules.TemporaryFolder;
38 import org.onap.sdnc.config.params.transformer.tosca.exceptions.ArtifactProcessorException;
40 public class TestGenerateArtifactString {
43 public TemporaryFolder temporaryFolder = new TemporaryFolder();
46 public void testStringArtifactGenerator() throws IOException, ArtifactProcessorException {
48 String pdString = getFileContent("tosca/ExamplePropertyDefinition.yml");
49 OutputStream outstream = null;
51 File tempFile = temporaryFolder.newFile("TestTosca.yml");
52 outstream = new FileOutputStream(tempFile);
53 ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
54 arp.generateArtifact(pdString, outstream);
58 String expectedTosca = getFileContent("tosca/ExpectedTosca.yml");
59 String toscaString = getFileContent(tempFile);
60 Assert.assertEquals(expectedTosca, toscaString);
65 public void testArtifactGeneratorWithParameterNameBlank()
66 throws IOException, ArtifactProcessorException {
68 String pdString = getFileContent("tosca/ExamplePropertyDefinition2.yml");
69 OutputStream outstream = null;
70 String expectedMsg = "Parameter name is empty,null or contains whitespace";
72 File tempFile = temporaryFolder.newFile("TestTosca.yml");
73 outstream = new FileOutputStream(tempFile);
74 ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
76 arp.generateArtifact(pdString, outstream);
77 } catch (ArtifactProcessorException e) {
78 Assert.assertEquals(expectedMsg, e.getMessage());
85 public void testArtifactGeneratorWithParameterNameNull()
86 throws IOException, ArtifactProcessorException {
88 String pdString = getFileContent("tosca/ExamplePropertyDefinition3.yml");
89 OutputStream outstream = null;
90 String expectedMsg = "Parameter name is empty,null or contains whitespace";
92 File tempFile = temporaryFolder.newFile("TestTosca.yml");
93 outstream = new FileOutputStream(tempFile);
94 ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
96 arp.generateArtifact(pdString, outstream);
97 } catch (ArtifactProcessorException e) {
98 Assert.assertEquals(expectedMsg, e.getMessage());
105 public void testArtifactGeneratorWithKindNull() throws IOException, ArtifactProcessorException {
107 String pdString = getFileContent("tosca/ExamplePropertyDefinition4.yml");
108 OutputStream outstream = null;
109 String expectedMsg = "Kind in PropertyDefinition is blank or null";
111 File tempFile = temporaryFolder.newFile("TestTosca.yml");
112 outstream = new FileOutputStream(tempFile);
113 ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
115 arp.generateArtifact(pdString, outstream);
116 } catch (ArtifactProcessorException e) {
117 Assert.assertEquals(expectedMsg, e.getMessage());
123 private String getFileContent(String fileName) throws IOException {
124 ClassLoader classLoader = new TestGenerateArtifactString().getClass().getClassLoader();
125 InputStream is = new FileInputStream(classLoader.getResource(fileName).getFile());
126 BufferedReader buf = new BufferedReader(new InputStreamReader(is));
127 String line = buf.readLine();
128 StringBuilder sb = new StringBuilder();
130 while (line != null) {
131 sb.append(line).append("\n");
132 line = buf.readLine();
134 String fileString = sb.toString();
139 private String getFileContent(File file) throws IOException {
140 InputStream is = new FileInputStream(file);
141 BufferedReader buf = new BufferedReader(new InputStreamReader(is));
142 String line = buf.readLine();
143 StringBuilder sb = new StringBuilder();
145 while (line != null) {
146 sb.append(line).append("\n");
147 line = buf.readLine();
149 String fileString = sb.toString();