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