74f0c0e5646829bdc3ebec8a3d24da65010ad71e
[aai/babel.git] / src / test / java / org / onap / aai / babel / util / ArtifactTestUtils.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 European Software Marketing Ltd.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.babel.util;
24
25 import static org.hamcrest.CoreMatchers.is;
26 import static org.junit.Assert.assertThat;
27
28 import com.google.common.base.Throwables;
29 import java.io.BufferedReader;
30 import java.io.ByteArrayInputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.io.InputStreamReader;
34 import java.util.Base64;
35 import java.util.List;
36 import java.util.Set;
37 import java.util.stream.Collectors;
38 import org.apache.commons.io.IOUtils;
39 import org.openecomp.sdc.generator.data.Artifact;
40
41 /**
42  * This class provides some utilities to assist with running tests.
43  */
44 public class ArtifactTestUtils {
45
46     public void performYmlAsserts(List<Artifact> toscaFiles, List<String> ymlPayloadsToLoad) {
47         assertThat("An unexpected number of yml files have been extracted", toscaFiles.size(),
48                 is(ymlPayloadsToLoad.size()));
49
50         Set<String> ymlPayloads = ymlPayloadsToLoad.stream().map(s -> {
51             try {
52                 return loadResourceAsString(s);
53             } catch (IOException e) {
54                 throw Throwables.propagate(e);
55             }
56         }).collect(Collectors.toSet());
57
58         toscaFiles.forEach(ts -> {
59             boolean payloadFound = false;
60
61             String s = bytesToString(ts.getPayload());
62
63             for (String ymlPayload : ymlPayloads) {
64                 String tscontent = ymlPayload;
65
66                 if (s.endsWith(tscontent)) {
67                     payloadFound = true;
68                     break;
69                 }
70             }
71             assertThat("The content of each yml file must match the actual content of the file extracted ("
72                     + ts.getName() + ")", payloadFound, is(true));
73         });
74     }
75
76     public byte[] loadResource(String resourceName) throws IOException {
77
78         return IOUtils.toByteArray(ArtifactTestUtils.class.getClassLoader().getResource(resourceName));
79     }
80
81     public String loadResourceAsString(String resourceName) throws IOException {
82
83         InputStream is = ArtifactTestUtils.class.getClassLoader().getResource(resourceName).openStream();
84
85         String result = new BufferedReader(new InputStreamReader(is)).lines().collect(Collectors.joining("\n"));
86
87         return result;
88
89     }
90
91     public String bytesToString(byte[] source) {
92         ByteArrayInputStream bis = new ByteArrayInputStream(Base64.getDecoder().decode(source));
93
94         String result = new BufferedReader(new InputStreamReader(bis)).lines().collect(Collectors.joining("\n"));
95
96         return result;
97
98     }
99
100 }