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