blob: 4eb00c28e70e650772c3c76c63ae64a156b8c679 [file] [log] [blame]
Simon Huntf59d36b2016-10-04 19:05:53 -07001/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.net.config.basics;
18
19import com.fasterxml.jackson.databind.JsonNode;
20import com.fasterxml.jackson.databind.ObjectMapper;
21import org.onosproject.net.DeviceId;
22import org.onosproject.net.config.ConfigApplyDelegate;
23
24import java.io.IOException;
25import java.io.InputStream;
26
27import static org.junit.Assert.fail;
28
29/**
30 * Abstract superclass for config tests.
31 */
32abstract class AbstractConfigTest {
33
34 private static final String D_PREFIX = "of:00000000000000";
35
36 /**
37 * Shared object mapper.
38 */
39 final ObjectMapper mapper = new ObjectMapper();
40
41 /**
42 * Shared null-delegate.
43 */
44 final ConfigApplyDelegate delegate = config -> {
45 };
46
47 /**
48 * Prints the given format string with parameter replacement to stdout.
49 *
50 * @param fmt format string
51 * @param params parameters
52 * @see String#format(String, Object...)
53 */
54 static void print(String fmt, Object... params) {
55 System.out.println(String.format(fmt, params));
56 }
57
58 /**
59 * Prints the given object's string representation to stdout.
60 *
61 * @param o the object to print
62 */
63 static void print(Object o) {
64 print("%s", o);
65 }
66
67 /**
68 * Reads in and parses the specified JSON file, returning a JSON node
69 * representation of the data. Note that if an error occurs while
70 * attempting to read the file, {@link org.junit.Assert#fail()} will be
71 * called.
72 *
73 * @param path JSON file path
74 * @return data represented as a JSON node
75 */
76 JsonNode getTestJson(String path) {
77 try {
78 InputStream is = AbstractConfigTest.class.getResourceAsStream(path);
79 return mapper.readTree(is);
80
81 } catch (IOException e) {
82 fail("Could not read json from: " + path + ": " + e.getMessage());
83 }
84 return null;
85 }
86
87 /**
88 * Returns a device identifier from a given string suffix.
89 *
90 * @param suffix two character suffix
91 * @return device identifier
92 */
93 static DeviceId dstr(String suffix) {
94 return DeviceId.deviceId(D_PREFIX + suffix);
95 }
96}