blob: e78d3cd3e149f718ed91548303bbf47563cf6a31 [file] [log] [blame]
Jian Li80c12702016-02-20 08:58:19 +09001/*
2 * Copyright 2016 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.onosproject.cpman.rest;
17
Ray Milkey7c251822016-04-06 17:38:25 -070018import java.util.Set;
19import java.util.concurrent.TimeUnit;
20
21import javax.ws.rs.client.WebTarget;
22
Jian Li80c12702016-02-20 08:58:19 +090023import org.glassfish.jersey.server.ResourceConfig;
Jian Li80c12702016-02-20 08:58:19 +090024import org.junit.Before;
25import org.junit.Test;
26import org.onlab.osgi.ServiceDirectory;
27import org.onlab.osgi.TestServiceDirectory;
28import org.onlab.packet.IpAddress;
29import org.onlab.rest.BaseResource;
30import org.onosproject.cluster.ClusterService;
31import org.onosproject.cluster.ControllerNode;
32import org.onosproject.cluster.NodeId;
33import org.onosproject.codec.CodecService;
34import org.onosproject.codec.impl.CodecManager;
35import org.onosproject.cpman.ControlLoad;
36import org.onosproject.cpman.ControlPlaneMonitorService;
37import org.onosproject.cpman.codec.ControlLoadCodec;
Ray Milkey7c251822016-04-06 17:38:25 -070038import org.onosproject.rest.resources.ResourceTest;
Jian Li80c12702016-02-20 08:58:19 +090039
Ray Milkey7c251822016-04-06 17:38:25 -070040import com.google.common.collect.ImmutableSet;
Jian Li80c12702016-02-20 08:58:19 +090041
42import static org.easymock.EasyMock.anyObject;
43import static org.easymock.EasyMock.anyString;
44import static org.easymock.EasyMock.createMock;
45import static org.easymock.EasyMock.expect;
46import static org.easymock.EasyMock.replay;
47import static org.easymock.EasyMock.verify;
48import static org.hamcrest.Matchers.is;
49import static org.junit.Assert.assertThat;
50
51/**
52 * Unit test for ControlMetrics REST API.
53 */
Ray Milkey7c251822016-04-06 17:38:25 -070054public class ControlMetricsResourceTest extends ResourceTest {
Jian Li80c12702016-02-20 08:58:19 +090055
56 final ControlPlaneMonitorService mockControlPlaneMonitorService =
57 createMock(ControlPlaneMonitorService.class);
58 final ClusterService mockClusterService = createMock(ClusterService.class);
59 Set<String> resourceSet = ImmutableSet.of("resource1", "resource2");
60 NodeId nodeId;
61 ControlLoad mockControlLoad;
62
63 private static final String PREFIX = "metrics";
64
65 /**
66 * Constructs a control metrics resource test instance.
67 */
68 public ControlMetricsResourceTest() {
69 super(ResourceConfig.forApplicationClass(CPManWebApplication.class));
70 }
71
72 /**
73 * Mock class for a controller node.
74 */
75 private static class MockControllerNode implements ControllerNode {
76 final NodeId id;
77
78 public MockControllerNode(NodeId id) {
79 this.id = id;
80 }
81
82 @Override
83 public NodeId id() {
84 return this.id;
85 }
86
87 @Override
88 public IpAddress ip() {
89 return null;
90 }
91
92 @Override
93 public int tcpPort() {
94 return 0;
95 }
96 }
97
98 private static class MockControlLoad implements ControlLoad {
99
100 @Override
101 public long average(int duration, TimeUnit unit) {
102 return 0;
103 }
104
105 @Override
106 public long average() {
107 return 10L;
108 }
109
110 @Override
111 public long[] recent(int duration, TimeUnit unit) {
112 return new long[0];
113 }
114
115 @Override
116 public long[] all() {
117 return new long[0];
118 }
119
120 @Override
121 public long rate() {
122 return 0;
123 }
124
125 @Override
126 public long latest() {
127 return 10L;
128 }
129
130 @Override
131 public boolean isValid() {
132 return false;
133 }
134
135 @Override
136 public long time() {
137 return 20L;
138 }
139 }
140
141 /**
142 * Sets up the global values for all the tests.
143 */
144 @Before
145 public void setUpTest() {
146 final CodecManager codecService = new CodecManager();
147 codecService.activate();
148 codecService.registerCodec(ControlLoad.class, new ControlLoadCodec());
149 ServiceDirectory testDirectory =
150 new TestServiceDirectory()
151 .add(ControlPlaneMonitorService.class,
152 mockControlPlaneMonitorService)
153 .add(ClusterService.class, mockClusterService)
154 .add(CodecService.class, codecService);
155 BaseResource.setServiceDirectory(testDirectory);
156
157 nodeId = new NodeId("1");
158 mockControlLoad = new MockControlLoad();
159 ControllerNode mockControllerNode = new MockControllerNode(nodeId);
160
161 expect(mockClusterService.getLocalNode()).andReturn(mockControllerNode).anyTimes();
162 replay(mockClusterService);
163 }
164
165 /**
166 * Tests the results of the REST API GET when there are no active entries.
167 */
168 @Test
169 public void testResourceEmptyArray() {
170 expect(mockControlPlaneMonitorService.availableResources(anyObject()))
171 .andReturn(ImmutableSet.of()).once();
172 replay(mockControlPlaneMonitorService);
173 final WebTarget wt = target();
174 final String response = wt.path(PREFIX + "/disk_metrics").request().get(String.class);
175 assertThat(response, is("{\"disks\":[]}"));
176
177 verify(mockControlPlaneMonitorService);
178 }
179
180 /**
181 * Tests the results of the rest api GET when there are active metrics.
182 */
183 @Test
184 public void testResourcePopulatedArray() {
185 expect(mockControlPlaneMonitorService.availableResources(anyObject()))
186 .andReturn(resourceSet).once();
Jian Li23906cc2016-03-31 11:16:44 -0700187 expect(mockControlPlaneMonitorService.getLocalLoad(anyObject(),
188 anyString())).andReturn(null).times(4);
Jian Li80c12702016-02-20 08:58:19 +0900189 replay(mockControlPlaneMonitorService);
190
191 final WebTarget wt = target();
192 final String response = wt.path(PREFIX + "/disk_metrics").request().get(String.class);
193 assertThat(response, is("{\"disks\":[{\"name\":\"resource1\",\"value\":{\"metrics\":[]}}," +
194 "{\"name\":\"resource2\",\"value\":{\"metrics\":[]}}]}"));
195 }
196}