Implement second part of dynamic DMaaP config
[dcaegen2/collectors/ves.git] / src / test / java / org / onap / dcae / TestingUtilities.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dcaegen2.collectors.ves
4  * ================================================================================
5  * Copyright (C) 2018 Nokia. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.dcae;
21
22 import static java.nio.file.Files.readAllBytes;
23 import static org.assertj.core.api.Assertions.assertThat;
24
25 import com.google.gson.JsonObject;
26 import com.google.gson.JsonParser;
27 import io.vavr.control.Try;
28 import java.io.File;
29 import java.nio.file.Files;
30 import java.nio.file.Path;
31 import java.nio.file.Paths;
32 import org.assertj.core.api.AbstractThrowableAssert;
33 import org.assertj.core.api.Java6Assertions;
34 import org.json.JSONObject;
35
36 /**
37  * @author Pawel Szalapski (pawel.szalapski@nokia.com)
38  */
39 public final class TestingUtilities {
40
41     private TestingUtilities() {
42         // utility class, no objects allowed
43     }
44
45     public static void assertJSONObjectsEqual(JSONObject o1, JSONObject o2) {
46         assertThat(o1.toString()).isEqualTo(o2.toString());
47     }
48
49     public static JSONObject readJSONFromFile(Path path) {
50         return rethrow(() -> new JSONObject(readFile(path)));
51     }
52
53     public static String readFile(Path path) {
54         return rethrow(() -> new String(readAllBytes(path)));
55     }
56
57     public static Path createTemporaryFile(String content) {
58         return rethrow(() -> {
59             File temp = File.createTempFile("ves-collector-tests-created-this-file", ".tmp");
60             temp.deleteOnExit();
61             Path filePath = Paths.get(temp.toString());
62             Files.write(filePath, content.getBytes());
63             return filePath;
64         });
65     }
66
67     /**
68      * Exception in test case usually means there is something wrong, it should never be catched, but rather thrown to
69      * be handled by JUnit framework.
70      */
71     private static <T> T rethrow(CheckedSupplier<T> supplier) {
72         try {
73             return supplier.get();
74         } catch (Exception e) {
75             throw new RuntimeException();
76         }
77     }
78
79     @FunctionalInterface
80     interface CheckedSupplier<T> {
81
82         T get() throws Exception;
83     }
84
85
86     public static void assertFailureHasInfo(Try any, String... msgPart) {
87         Java6Assertions.assertThat(any.isFailure()).isTrue();
88         AbstractThrowableAssert<?, ? extends Throwable> o = Java6Assertions.assertThat(any.getCause())
89             .hasCauseInstanceOf(Exception.class);
90         for (String s : msgPart) {
91             o.hasStackTraceContaining(s);
92         }
93     }
94 }