blob: 6b6bee7d597394c710b99be0d2ea7d44edeb38e1 [file] [log] [blame]
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -08003 *
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.onosproject.ui;
17
18import com.google.common.collect.ImmutableList;
19import org.junit.Test;
20
21import java.io.IOException;
Simon Hunte05cae42015-07-23 17:35:24 -070022import java.util.List;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080023
24import static com.google.common.io.ByteStreams.toByteArray;
Thomas Vachuska3553b302015-03-07 14:49:43 -080025import static org.junit.Assert.*;
Thomas Vachuska8b91f4f2015-04-23 17:55:36 -070026import static org.onosproject.ui.UiView.Category.OTHER;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080027
28/**
29 * Tests the default user interface extension descriptor.
30 */
31public class UiExtensionTest {
32
Simon Hunte05cae42015-07-23 17:35:24 -070033 private static final String FOO_ID = "foo";
34 private static final String FOO_LABEL = "Foo View";
35 private static final String BAR_ID = "bar";
36 private static final String BAR_LABEL = "Bar View";
37 private static final String CUSTOM = "custom";
38
39
40 private static final UiView FOO_VIEW = new UiView(OTHER, FOO_ID, FOO_LABEL);
41 private static final UiView BAR_VIEW = new UiView(OTHER, BAR_ID, BAR_LABEL);
42 private static final UiView HIDDEN_VIEW = new UiViewHidden(FOO_ID);
43
44 private static final UiMessageHandlerFactory MH_FACTORY = () -> null;
45 private static final UiTopoOverlayFactory TO_FACTORY = () -> null;
46
47 private final ClassLoader cl = getClass().getClassLoader();
48
49 private List<UiView> viewList;
50 private UiExtension ext;
51 private String css;
52 private String js;
53 private UiView view;
54
55
56
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080057 @Test
58 public void basics() throws IOException {
Simon Hunte05cae42015-07-23 17:35:24 -070059 viewList = ImmutableList.of(FOO_VIEW);
60 ext = new UiExtension.Builder(cl, viewList).build();
61
62 css = new String(toByteArray(ext.css()));
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080063 assertTrue("incorrect css stream", css.contains("foo-css"));
Simon Hunte05cae42015-07-23 17:35:24 -070064 js = new String(toByteArray(ext.js()));
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080065 assertTrue("incorrect js stream", js.contains("foo-js"));
Simon Hunte05cae42015-07-23 17:35:24 -070066
67 assertEquals("expected 1 view", 1, ext.views().size());
68 view = ext.views().get(0);
69 assertEquals("wrong view category", OTHER, view.category());
70 assertEquals("wrong view id", FOO_ID, view.id());
71 assertEquals("wrong view label", FOO_LABEL, view.label());
72
73 assertNull("unexpected message handler factory", ext.messageHandlerFactory());
74 assertNull("unexpected topo overlay factory", ext.topoOverlayFactory());
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080075 }
76
77 @Test
78 public void withPath() throws IOException {
Simon Hunte05cae42015-07-23 17:35:24 -070079 viewList = ImmutableList.of(FOO_VIEW);
80 ext = new UiExtension.Builder(cl, viewList)
81 .resourcePath(CUSTOM)
82 .build();
83
84 css = new String(toByteArray(ext.css()));
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080085 assertTrue("incorrect css stream", css.contains("custom-css"));
Simon Hunte05cae42015-07-23 17:35:24 -070086 js = new String(toByteArray(ext.js()));
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080087 assertTrue("incorrect js stream", js.contains("custom-js"));
Simon Hunte05cae42015-07-23 17:35:24 -070088
89 assertEquals("expected 1 view", 1, ext.views().size());
90 view = ext.views().get(0);
91 assertEquals("wrong view category", OTHER, view.category());
92 assertEquals("wrong view id", FOO_ID, view.id());
93 assertEquals("wrong view label", FOO_LABEL, view.label());
94
95 assertNull("unexpected message handler factory", ext.messageHandlerFactory());
96 assertNull("unexpected topo overlay factory", ext.topoOverlayFactory());
97 }
98
99 @Test
100 public void messageHandlerFactory() {
101 viewList = ImmutableList.of(FOO_VIEW);
102 ext = new UiExtension.Builder(cl, viewList)
103 .messageHandlerFactory(MH_FACTORY)
104 .build();
105
106 assertEquals("wrong message handler factory", MH_FACTORY,
107 ext.messageHandlerFactory());
108 assertNull("unexpected topo overlay factory", ext.topoOverlayFactory());
109 }
110
111 @Test
112 public void topoOverlayFactory() {
113 viewList = ImmutableList.of(HIDDEN_VIEW);
114 ext = new UiExtension.Builder(cl, viewList)
115 .topoOverlayFactory(TO_FACTORY)
116 .build();
117
118 assertNull("unexpected message handler factory", ext.messageHandlerFactory());
119 assertEquals("wrong topo overlay factory", TO_FACTORY,
120 ext.topoOverlayFactory());
121 }
122
123 @Test
124 public void twoViews() {
125 viewList = ImmutableList.of(FOO_VIEW, BAR_VIEW);
126 ext = new UiExtension.Builder(cl, viewList).build();
127
128 assertEquals("expected 2 views", 2, ext.views().size());
129
130 view = ext.views().get(0);
131 assertEquals("wrong view category", OTHER, view.category());
132 assertEquals("wrong view id", FOO_ID, view.id());
133 assertEquals("wrong view label", FOO_LABEL, view.label());
134
135 view = ext.views().get(1);
136 assertEquals("wrong view category", OTHER, view.category());
137 assertEquals("wrong view id", BAR_ID, view.id());
138 assertEquals("wrong view label", BAR_LABEL, view.label());
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800139 }
140}