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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.dcae;
22 import static java.nio.file.Files.readAllBytes;
23 import static org.assertj.core.api.Assertions.assertThat;
25 import com.google.gson.JsonObject;
26 import com.google.gson.JsonParser;
27 import io.vavr.control.Try;
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;
37 * @author Pawel Szalapski (pawel.szalapski@nokia.com)
39 public final class TestingUtilities {
41 private TestingUtilities() {
42 // utility class, no objects allowed
45 public static void assertJSONObjectsEqual(JSONObject o1, JSONObject o2) {
46 assertThat(o1.toString()).isEqualTo(o2.toString());
49 public static JSONObject readJSONFromFile(Path path) {
50 return rethrow(() -> new JSONObject(readFile(path)));
53 public static String readFile(Path path) {
54 return rethrow(() -> new String(readAllBytes(path)));
57 public static Path createTemporaryFile(String content) {
58 return rethrow(() -> {
59 File temp = File.createTempFile("ves-collector-tests-created-this-file", ".tmp");
61 Path filePath = Paths.get(temp.toString());
62 Files.write(filePath, content.getBytes());
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.
71 private static <T> T rethrow(CheckedSupplier<T> supplier) {
73 return supplier.get();
74 } catch (Exception e) {
75 throw new RuntimeException();
80 interface CheckedSupplier<T> {
82 T get() throws Exception;
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);