blob: 4b7f56140bafcb7065e9fecad20ac71111baa566 [file] [log] [blame]
Thomas Vachuskab51b8bc2015-07-27 08:37:12 -07001/*
2 * Copyright 2015 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 */
16package org.onlab.stc;
17
18import org.junit.Test;
19import org.onlab.stc.MonitorLayout.Box;
20
21import java.io.IOException;
22
23import static org.junit.Assert.assertEquals;
24import static org.onlab.stc.CompilerTest.getStream;
25import static org.onlab.stc.CompilerTest.stageTestResource;
26import static org.onlab.stc.MonitorLayout.SLOT_WIDTH;
27import static org.onlab.stc.Scenario.loadScenario;
28
29/**
30 * Tests of the monitor layout functionality.
31 */
32public class MonitorLayoutTest {
33
34 private MonitorLayout layout;
35
36 private Compiler getCompiler(String name) throws IOException {
37 stageTestResource(name);
38 Scenario scenario = loadScenario(getStream(name));
39 Compiler compiler = new Compiler(scenario);
40 compiler.compile();
41 return compiler;
42 }
43
44 @Test
45 public void basic() throws IOException {
46 layout = new MonitorLayout(getCompiler("layout-basic.xml"));
47 validate(layout, null, 0, 1, 5, 2);
48 validate(layout, "a", 1, 1, 1, 1, 1, -SLOT_WIDTH / 2);
49 validate(layout, "b", 2, 2, 1, 1, 0, 0);
50 validate(layout, "f", 3, 3, 1);
51
52 validate(layout, "g", 1, 1, 4, 1, 1, SLOT_WIDTH / 2);
53 validate(layout, "c", 2, 1, 1);
54 validate(layout, "d", 3, 2, 1);
55 validate(layout, "e", 4, 3, 1);
56 }
57
58 @Test
59 public void basicNest() throws IOException {
60 layout = new MonitorLayout(getCompiler("layout-basic-nest.xml"));
61 validate(layout, null, 0, 1, 6, 2);
62 validate(layout, "a", 1, 1, 1, 1, 1, -SLOT_WIDTH / 2);
63 validate(layout, "b", 2, 2, 1);
64 validate(layout, "f", 3, 3, 1);
65
66 validate(layout, "g", 1, 1, 5, 1);
67 validate(layout, "c", 2, 1, 1);
68
69 validate(layout, "gg", 3, 2, 3, 1);
70 validate(layout, "d", 4, 1, 1);
71 validate(layout, "e", 5, 2, 1);
72 }
73
74 @Test
75 public void staggeredDependencies() throws IOException {
76 layout = new MonitorLayout(getCompiler("layout-staggered-dependencies.xml"));
77 validate(layout, null, 0, 1, 7, 4);
78 validate(layout, "a", 1, 1, 1, 1, 1, -SLOT_WIDTH - SLOT_WIDTH / 2);
79 validate(layout, "aa", 1, 1, 1, 1, 1, -SLOT_WIDTH / 2);
80 validate(layout, "b", 2, 2, 1);
81 validate(layout, "f", 3, 3, 1);
82
83 validate(layout, "g", 1, 1, 5, 2, 1, +SLOT_WIDTH / 2);
84 validate(layout, "c", 2, 1, 1);
85
86 validate(layout, "gg", 3, 2, 3, 2);
87 validate(layout, "d", 4, 1, 1);
88 validate(layout, "dd", 4, 1, 1);
89 validate(layout, "e", 5, 2, 1);
90
91 validate(layout, "i", 6, 6, 1);
92 }
93
94 @Test
95 public void deepNext() throws IOException {
96 layout = new MonitorLayout(getCompiler("layout-deep-nest.xml"));
97 validate(layout, null, 0, 1, 7, 6);
98 validate(layout, "a", 1, 1, 1);
99 validate(layout, "aa", 1, 1, 1);
100 validate(layout, "b", 2, 2, 1);
101 validate(layout, "f", 3, 3, 1);
102
103 validate(layout, "g", 1, 1, 5, 2);
104 validate(layout, "c", 2, 1, 1);
105
106 validate(layout, "gg", 3, 2, 3, 2);
107 validate(layout, "d", 4, 1, 1);
108 validate(layout, "dd", 4, 1, 1);
109 validate(layout, "e", 5, 2, 1);
110
111 validate(layout, "i", 6, 6, 1);
112
113 validate(layout, "g1", 1, 1, 6, 2);
114 validate(layout, "g2", 2, 1, 5, 2);
115 validate(layout, "g3", 3, 1, 4, 2);
116 validate(layout, "u", 4, 1, 1);
117 validate(layout, "v", 4, 1, 1);
118 validate(layout, "w", 5, 2, 1);
119 validate(layout, "z", 6, 3, 1);
120 }
121
122
123 private void validate(MonitorLayout layout, String name,
124 int absoluteTier, int tier, int depth, int breadth) {
125 Box b = layout.get(name);
126 assertEquals("incorrect absolute tier", absoluteTier, b.absoluteTier());
127 assertEquals("incorrect tier", tier, b.tier());
128 assertEquals("incorrect depth", depth, b.depth());
129 assertEquals("incorrect breadth", breadth, b.breadth());
130 }
131
132 private void validate(MonitorLayout layout, String name,
133 int absoluteTier, int tier, int depth, int breadth,
134 int top, int center) {
135 validate(layout, name, absoluteTier, tier, depth, breadth);
136 Box b = layout.get(name);
137 assertEquals("incorrect top", top, b.top());
138 assertEquals("incorrect center", center, b.center());
139 }
140
141 private void validate(MonitorLayout layout, String name,
142 int absoluteTier, int tier, int depth) {
143 validate(layout, name, absoluteTier, tier, depth, 1);
144 }
145
146}