blob: 377890435e124a9334962306ddcc3fd0f0499dac [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;
Simon Hunt4f3a4072016-10-17 17:52:11 -070021import com.fasterxml.jackson.databind.node.ObjectNode;
Simon Huntf59d36b2016-10-04 19:05:53 -070022import org.onosproject.net.DeviceId;
23import org.onosproject.net.config.ConfigApplyDelegate;
24
25import java.io.IOException;
26import java.io.InputStream;
27
28import static org.junit.Assert.fail;
29
30/**
31 * Abstract superclass for config tests.
32 */
33abstract class AbstractConfigTest {
34
35 private static final String D_PREFIX = "of:00000000000000";
36
Simon Hunt4f3a4072016-10-17 17:52:11 -070037 static final String BASIC = "basic";
38 static final String JSON_LOADED = "%nJSON loaded: %s";
39 static final String CHECKING_S = " checking: %s";
40
41 // Shared object mapper.
Simon Huntf59d36b2016-10-04 19:05:53 -070042 final ObjectMapper mapper = new ObjectMapper();
43
Simon Hunt4f3a4072016-10-17 17:52:11 -070044 // Shared null-delegate.
Simon Huntf59d36b2016-10-04 19:05:53 -070045 final ConfigApplyDelegate delegate = config -> {
46 };
47
48 /**
49 * Prints the given format string with parameter replacement to stdout.
50 *
51 * @param fmt format string
52 * @param params parameters
53 * @see String#format(String, Object...)
54 */
55 static void print(String fmt, Object... params) {
56 System.out.println(String.format(fmt, params));
57 }
58
59 /**
60 * Prints the given object's string representation to stdout.
61 *
62 * @param o the object to print
63 */
64 static void print(Object o) {
65 print("%s", o);
66 }
67
68 /**
69 * Reads in and parses the specified JSON file, returning a JSON node
70 * representation of the data. Note that if an error occurs while
71 * attempting to read the file, {@link org.junit.Assert#fail()} will be
72 * called.
73 *
74 * @param path JSON file path
75 * @return data represented as a JSON node
76 */
77 JsonNode getTestJson(String path) {
78 try {
79 InputStream is = AbstractConfigTest.class.getResourceAsStream(path);
80 return mapper.readTree(is);
81
82 } catch (IOException e) {
83 fail("Could not read json from: " + path + ": " + e.getMessage());
84 }
85 return null;
86 }
87
88 /**
89 * Returns a device identifier from a given string suffix.
90 *
91 * @param suffix two character suffix
92 * @return device identifier
93 */
94 static DeviceId dstr(String suffix) {
95 return DeviceId.deviceId(D_PREFIX + suffix);
96 }
Simon Hunt4f3a4072016-10-17 17:52:11 -070097
98 /**
99 * Utility class to build quick JSON structures.
100 */
101 final class TmpJson {
102
103 final ObjectNode root = mapper.createObjectNode();
104
105 TmpJson props(String... keys) {
106 for (String k : keys) {
107 root.put(k, k);
108 }
109 return this;
110 }
111
112 TmpJson arrays(String... keys) {
113 for (String k : keys) {
114 root.set(k, mapper.createArrayNode());
115 }
116 return this;
117 }
118
119 TmpJson objects(String... keys) {
120 for (String k : keys) {
121 root.set(k, mapper.createObjectNode());
122 }
123 return this;
124 }
125
126 ObjectNode node() {
127 return root;
128 }
129 }
Simon Huntf59d36b2016-10-04 19:05:53 -0700130}