blob: 0d682d8a83480053b2983a9df653d2c47cf9791d [file] [log] [blame]
Ray Milkeyb9af9462015-07-17 11:12:09 -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.onosproject.rest;
17
Jian Li80cfe452016-01-14 16:04:58 -080018import com.eclipsesource.json.Json;
Jian Li9d616492016-03-09 10:52:49 -080019import com.eclipsesource.json.JsonArray;
20import com.eclipsesource.json.JsonObject;
21import com.google.common.collect.ImmutableList;
22import com.google.common.collect.ImmutableSet;
Ray Milkeyb9af9462015-07-17 11:12:09 -070023import org.junit.Before;
24import org.junit.Test;
25import org.onlab.osgi.ServiceDirectory;
26import org.onlab.osgi.TestServiceDirectory;
27import org.onlab.rest.BaseResource;
28import org.onosproject.codec.CodecService;
29import org.onosproject.codec.impl.CodecManager;
30import org.onosproject.net.Link;
31import org.onosproject.net.link.LinkService;
32import org.onosproject.net.statistic.DefaultLoad;
33import org.onosproject.net.statistic.StatisticService;
34
Jian Li9d616492016-03-09 10:52:49 -080035import javax.ws.rs.client.WebTarget;
36import java.io.UnsupportedEncodingException;
37import java.net.URLDecoder;
38import java.util.HashMap;
39import java.util.stream.IntStream;
Ray Milkeyb9af9462015-07-17 11:12:09 -070040
41import static org.easymock.EasyMock.createMock;
42import static org.easymock.EasyMock.expect;
43import static org.easymock.EasyMock.replay;
44import static org.hamcrest.Matchers.containsString;
45import static org.hamcrest.Matchers.hasSize;
46import static org.hamcrest.Matchers.is;
47import static org.hamcrest.Matchers.lessThanOrEqualTo;
48import static org.hamcrest.Matchers.notNullValue;
49import static org.junit.Assert.assertThat;
50import static org.onosproject.net.NetTestTools.connectPoint;
51import static org.onosproject.net.NetTestTools.link;
52
53/**
54 * Unit tests for statistics REST APIs.
55 */
56public class StatisticsResourceTest extends ResourceTest {
57
58 Link link1 = link("src1", 1, "dst1", 1);
59 Link link2 = link("src2", 2, "dst2", 2);
60 Link link3 = link("src3", 3, "dst3", 3);
61
62 LinkService mockLinkService;
63 StatisticService mockStatisticService;
64
65 /**
66 * Initializes test mocks and environment.
67 */
68 @Before
69 public void setUpTest() {
70 mockLinkService = createMock(LinkService.class);
71 expect(mockLinkService.getLinks())
72 .andReturn(ImmutableList.of(link1, link2, link3));
73 expect(mockLinkService.getLinks(connectPoint("0000000000000001", 2)))
74 .andReturn(ImmutableSet.of(link3));
75
76 mockStatisticService = createMock(StatisticService.class);
77 expect(mockStatisticService.load(link1))
78 .andReturn(new DefaultLoad(2, 1, 1));
79 expect(mockStatisticService.load(link2))
80 .andReturn(new DefaultLoad(22, 11, 1));
81 expect(mockStatisticService.load(link3))
82 .andReturn(new DefaultLoad(222, 111, 1));
83
84 replay(mockLinkService, mockStatisticService);
85
86 // Register the services needed for the test
87 CodecManager codecService = new CodecManager();
88 codecService.activate();
89 ServiceDirectory testDirectory =
90 new TestServiceDirectory()
91 .add(LinkService.class, mockLinkService)
92 .add(StatisticService.class, mockStatisticService)
93 .add(CodecService.class, codecService);
94
95 BaseResource.setServiceDirectory(testDirectory);
96 }
97
98 /**
99 * Checks that the values in a JSON representation of a Load are
100 * correct.
101 *
102 * @param load JSON for the Loan object
103 * @param rate expected vale fo rate
104 * @param latest expected value for latest
105 * @param valid expected value for valid flag
106 * @param device expected device ID
107 */
108 private void checkValues(JsonObject load, int rate, int latest,
Jian Li9d616492016-03-09 10:52:49 -0800109 boolean valid, String device) throws UnsupportedEncodingException {
Ray Milkeyb9af9462015-07-17 11:12:09 -0700110 assertThat(load, notNullValue());
111 assertThat(load.get("rate").asInt(), is(rate));
112 assertThat(load.get("latest").asInt(), is(latest));
113 assertThat(load.get("valid").asBoolean(), is(valid));
114 assertThat(load.get("time").asLong(),
115 lessThanOrEqualTo((System.currentTimeMillis())));
Jian Li9d616492016-03-09 10:52:49 -0800116 assertThat(URLDecoder.decode(load.get("link").asString(), "UTF-8"),
Ray Milkeyb9af9462015-07-17 11:12:09 -0700117 containsString("device=of:" + device));
118 }
119
120 /**
121 * Tests GET of a single Load statistics object.
122 */
123 @Test
Jian Li9d616492016-03-09 10:52:49 -0800124 public void testSingleLoadGet() throws UnsupportedEncodingException {
125 final WebTarget wt = target();
126 final String response = wt.path("statistics/flows/link")
Ray Milkeyb9af9462015-07-17 11:12:09 -0700127 .queryParam("device", "of:0000000000000001")
128 .queryParam("port", "2")
Jian Li9d616492016-03-09 10:52:49 -0800129 .request()
Ray Milkeyb9af9462015-07-17 11:12:09 -0700130 .get(String.class);
131
Jian Li80cfe452016-01-14 16:04:58 -0800132 final JsonObject result = Json.parse(response).asObject();
Ray Milkeyb9af9462015-07-17 11:12:09 -0700133 assertThat(result, notNullValue());
134
135 assertThat(result.names(), hasSize(1));
136 assertThat(result.names().get(0), is("loads"));
137
138 final JsonArray jsonLoads = result.get("loads").asArray();
139 assertThat(jsonLoads, notNullValue());
140 assertThat(jsonLoads.size(), is(1));
141
142 JsonObject load1 = jsonLoads.get(0).asObject();
143 checkValues(load1, 111, 222, true, "src3");
144 }
145
146 /**
147 * Tests GET of all Load statistics objects.
148 */
149 @Test
Jian Li9d616492016-03-09 10:52:49 -0800150 public void testLoadsGet() throws UnsupportedEncodingException {
151 final WebTarget wt = target();
152 final String response = wt.path("statistics/flows/link/").request().get(String.class);
Ray Milkeyb9af9462015-07-17 11:12:09 -0700153
Jian Li80cfe452016-01-14 16:04:58 -0800154 final JsonObject result = Json.parse(response).asObject();
Ray Milkeyb9af9462015-07-17 11:12:09 -0700155 assertThat(result, notNullValue());
156
157 assertThat(result.names(), hasSize(1));
158 assertThat(result.names().get(0), is("loads"));
159
160 final JsonArray jsonLoads = result.get("loads").asArray();
161 assertThat(jsonLoads, notNullValue());
162 assertThat(jsonLoads.size(), is(3));
163
164 // Hash the loads by the current field to allow easy lookup if the
165 // order changes.
166 HashMap<Integer, JsonObject> currentMap = new HashMap<>();
167 IntStream.range(0, jsonLoads.size())
168 .forEach(index -> currentMap.put(
169 jsonLoads.get(index).asObject().get("latest").asInt(),
170 jsonLoads.get(index).asObject()));
171
172 JsonObject load1 = currentMap.get(2);
173 checkValues(load1, 1, 2, true, "src1");
174
175 JsonObject load2 = currentMap.get(22);
176 checkValues(load2, 11, 22, true, "src2");
177
178 JsonObject load3 = currentMap.get(222);
179 checkValues(load3, 111, 222, true, "src3");
180
181 }
182}