2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.sdnc.config.params.transformer.tosca;
27 import java.io.BufferedReader;
29 import java.io.FileInputStream;
30 import java.io.FileOutputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.io.InputStreamReader;
34 import java.io.OutputStream;
35 import org.junit.Assert;
36 import org.junit.Rule;
37 import org.junit.Test;
38 import org.junit.rules.TemporaryFolder;
39 import org.onap.sdnc.config.params.transformer.tosca.exceptions.ArtifactProcessorException;
41 public class TestGenerateArtifactString {
44 public TemporaryFolder temporaryFolder = new TemporaryFolder();
47 public void testStringArtifactGenerator() throws IOException, ArtifactProcessorException {
49 String pdString = getFileContent("tosca/ExamplePropertyDefinition.yml");
50 OutputStream outstream = null;
52 File tempFile = temporaryFolder.newFile("TestTosca.yml");
53 outstream = new FileOutputStream(tempFile);
54 ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
55 arp.generateArtifact(pdString, outstream);
59 String expectedTosca = getFileContent("tosca/ExpectedTosca.yml");
60 String toscaString = getFileContent(tempFile);
61 Assert.assertEquals(expectedTosca, toscaString);
66 public void testArtifactGeneratorWithParameterNameBlank()
67 throws IOException, ArtifactProcessorException {
69 String pdString = getFileContent("tosca/ExamplePropertyDefinition2.yml");
70 OutputStream outstream = null;
71 String expectedMsg = "Parameter name is empty,null or contains whitespace";
73 File tempFile = temporaryFolder.newFile("TestTosca.yml");
74 outstream = new FileOutputStream(tempFile);
75 ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
77 arp.generateArtifact(pdString, outstream);
78 } catch (ArtifactProcessorException e) {
79 Assert.assertEquals(expectedMsg, e.getMessage());
86 public void testArtifactGeneratorWithParameterNameNull()
87 throws IOException, ArtifactProcessorException {
89 String pdString = getFileContent("tosca/ExamplePropertyDefinition3.yml");
90 OutputStream outstream = null;
91 String expectedMsg = "Parameter name is empty,null or contains whitespace";
93 File tempFile = temporaryFolder.newFile("TestTosca.yml");
94 outstream = new FileOutputStream(tempFile);
95 ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
97 arp.generateArtifact(pdString, outstream);
98 } catch (ArtifactProcessorException e) {
99 Assert.assertEquals(expectedMsg, e.getMessage());
106 public void testArtifactGeneratorWithKindNull() throws IOException, ArtifactProcessorException {
108 String pdString = getFileContent("tosca/ExamplePropertyDefinition4.yml");
109 OutputStream outstream = null;
110 String expectedMsg = "Kind in PropertyDefinition is blank or null";
112 File tempFile = temporaryFolder.newFile("TestTosca.yml");
113 outstream = new FileOutputStream(tempFile);
114 ArtifactProcessorImpl arp = new ArtifactProcessorImpl();
116 arp.generateArtifact(pdString, outstream);
117 } catch (ArtifactProcessorException e) {
118 Assert.assertEquals(expectedMsg, e.getMessage());
124 private String getFileContent(String fileName) throws IOException {
125 ClassLoader classLoader = new TestGenerateArtifactString().getClass().getClassLoader();
126 InputStream is = new FileInputStream(classLoader.getResource(fileName).getFile());
127 BufferedReader buf = new BufferedReader(new InputStreamReader(is));
128 String line = buf.readLine();
129 StringBuilder sb = new StringBuilder();
131 while (line != null) {
132 sb.append(line).append("\n");
133 line = buf.readLine();
135 String fileString = sb.toString();
140 private String getFileContent(File file) throws IOException {
141 InputStream is = new FileInputStream(file);
142 BufferedReader buf = new BufferedReader(new InputStreamReader(is));
143 String line = buf.readLine();
144 StringBuilder sb = new StringBuilder();
146 while (line != null) {
147 sb.append(line).append("\n");
148 line = buf.readLine();
150 String fileString = sb.toString();