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