2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.aai.babel.util;
25 import static org.hamcrest.CoreMatchers.is;
26 import static org.junit.Assert.assertThat;
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;
37 import java.util.stream.Collectors;
38 import org.apache.commons.io.IOUtils;
39 import org.openecomp.sdc.generator.data.Artifact;
42 * This class provides some utilities to assist with running tests.
44 public class ArtifactTestUtils {
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()));
50 Set<String> ymlPayloads = ymlPayloadsToLoad.stream().map(s -> {
52 return loadResourceAsString(s);
53 } catch (IOException e) {
54 throw Throwables.propagate(e);
56 }).collect(Collectors.toSet());
58 toscaFiles.forEach(ts -> {
59 boolean payloadFound = false;
61 String s = bytesToString(ts.getPayload());
63 for (String ymlPayload : ymlPayloads) {
64 String tscontent = ymlPayload;
66 if (s.endsWith(tscontent)) {
71 assertThat("The content of each yml file must match the actual content of the file extracted ("
72 + ts.getName() + ")", payloadFound, is(true));
76 public byte[] loadResource(String resourceName) throws IOException {
78 return IOUtils.toByteArray(ArtifactTestUtils.class.getClassLoader().getResource(resourceName));
81 public String loadResourceAsString(String resourceName) throws IOException {
83 InputStream is = ArtifactTestUtils.class.getClassLoader().getResource(resourceName).openStream();
85 String result = new BufferedReader(new InputStreamReader(is)).lines().collect(Collectors.joining("\n"));
91 public String bytesToString(byte[] source) {
92 ByteArrayInputStream bis = new ByteArrayInputStream(Base64.getDecoder().decode(source));
94 String result = new BufferedReader(new InputStreamReader(bis)).lines().collect(Collectors.joining("\n"));