Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [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.sun.jersey.api.client.ClientResponse; |
| 19 | import com.sun.jersey.api.client.WebResource; |
| 20 | import com.sun.jersey.spi.container.servlet.ServletContainer; |
| 21 | import com.sun.jersey.test.framework.AppDescriptor; |
| 22 | import com.sun.jersey.test.framework.JerseyTest; |
| 23 | import com.sun.jersey.test.framework.WebAppDescriptor; |
| 24 | import org.junit.Before; |
| 25 | import org.junit.Test; |
| 26 | import org.onlab.osgi.ServiceDirectory; |
| 27 | import org.onlab.osgi.TestServiceDirectory; |
| 28 | import org.onlab.rest.BaseResource; |
| 29 | import org.onosproject.cpman.ControlPlaneMonitorService; |
Jian Li | 1a42469 | 2016-02-03 16:21:18 -0800 | [diff] [blame^] | 30 | import org.onosproject.cpman.SystemInfo; |
| 31 | import org.onosproject.cpman.impl.SystemInfoFactory; |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 32 | import org.onosproject.net.DeviceId; |
| 33 | |
| 34 | import javax.ws.rs.core.MediaType; |
| 35 | import java.io.IOException; |
| 36 | import java.io.InputStream; |
| 37 | import java.net.HttpURLConnection; |
| 38 | import java.net.ServerSocket; |
| 39 | import java.util.Optional; |
| 40 | |
Jian Li | c5cb4a1 | 2016-02-03 23:24:42 -0800 | [diff] [blame] | 41 | import static org.easymock.EasyMock.anyInt; |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 42 | import static org.easymock.EasyMock.anyObject; |
Jian Li | ba6b117 | 2016-02-01 22:40:42 -0800 | [diff] [blame] | 43 | import static org.easymock.EasyMock.anyString; |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 44 | import static org.easymock.EasyMock.createMock; |
| 45 | import static org.easymock.EasyMock.expectLastCall; |
| 46 | import static org.easymock.EasyMock.replay; |
| 47 | import static org.hamcrest.Matchers.is; |
| 48 | import static org.hamcrest.Matchers.notNullValue; |
| 49 | import static org.junit.Assert.assertThat; |
| 50 | |
| 51 | /** |
| 52 | * Unit test for ControlMetricsCollector. |
| 53 | */ |
| 54 | public class ControlMetricsCollectorResourceTest extends JerseyTest { |
| 55 | |
| 56 | final ControlPlaneMonitorService mockControlPlaneMonitorService = |
| 57 | createMock(ControlPlaneMonitorService.class); |
| 58 | |
Jian Li | 54df73e | 2016-02-01 17:09:03 -0800 | [diff] [blame] | 59 | private static final String PREFIX = "collector"; |
| 60 | |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 61 | /** |
Jian Li | 1fdd224 | 2016-02-05 10:01:19 -0800 | [diff] [blame] | 62 | * Constructs a control metrics collector resource test instance. |
| 63 | */ |
| 64 | public ControlMetricsCollectorResourceTest() { |
| 65 | super(new WebAppDescriptor.Builder("javax.ws.rs.Application", |
| 66 | CPManWebApplication.class.getCanonicalName()) |
| 67 | .servletClass(ServletContainer.class).build()); |
| 68 | } |
| 69 | |
| 70 | /** |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 71 | * Sets up the global values for all the tests. |
| 72 | */ |
| 73 | @Before |
| 74 | public void setUpTest() { |
| 75 | ServiceDirectory testDirectory = |
| 76 | new TestServiceDirectory() |
| 77 | .add(ControlPlaneMonitorService.class, mockControlPlaneMonitorService); |
| 78 | BaseResource.setServiceDirectory(testDirectory); |
| 79 | } |
| 80 | |
Jian Li | 1fdd224 | 2016-02-05 10:01:19 -0800 | [diff] [blame] | 81 | /** |
| 82 | * Tests CPU metrics POST through REST API. |
| 83 | */ |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 84 | @Test |
| 85 | public void testCpuMetricsPost() { |
Jian Li | c5cb4a1 | 2016-02-03 23:24:42 -0800 | [diff] [blame] | 86 | mockControlPlaneMonitorService.updateMetric(anyObject(), anyInt(), |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 87 | (Optional<DeviceId>) anyObject()); |
| 88 | expectLastCall().times(5); |
| 89 | replay(mockControlPlaneMonitorService); |
Jian Li | 54df73e | 2016-02-01 17:09:03 -0800 | [diff] [blame] | 90 | basePostTest("cpu-metrics-post.json", PREFIX + "/cpu_metrics"); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Jian Li | 1fdd224 | 2016-02-05 10:01:19 -0800 | [diff] [blame] | 93 | /** |
| 94 | * Tests memory metrics POST through REST API. |
| 95 | */ |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 96 | @Test |
| 97 | public void testMemoryMetricsPost() { |
Jian Li | c5cb4a1 | 2016-02-03 23:24:42 -0800 | [diff] [blame] | 98 | mockControlPlaneMonitorService.updateMetric(anyObject(), anyInt(), |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 99 | (Optional<DeviceId>) anyObject()); |
| 100 | expectLastCall().times(4); |
| 101 | replay(mockControlPlaneMonitorService); |
Jian Li | 54df73e | 2016-02-01 17:09:03 -0800 | [diff] [blame] | 102 | basePostTest("memory-metrics-post.json", PREFIX + "/memory_metrics"); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Jian Li | 1fdd224 | 2016-02-05 10:01:19 -0800 | [diff] [blame] | 105 | /** |
| 106 | * Tests disk metrics POST through REST API. |
| 107 | */ |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 108 | @Test |
Jian Li | 1fdd224 | 2016-02-05 10:01:19 -0800 | [diff] [blame] | 109 | public void testDiskMetrics() { |
Jian Li | c5cb4a1 | 2016-02-03 23:24:42 -0800 | [diff] [blame] | 110 | mockControlPlaneMonitorService.updateMetric(anyObject(), anyInt(), anyString()); |
Jian Li | ba6b117 | 2016-02-01 22:40:42 -0800 | [diff] [blame] | 111 | expectLastCall().times(4); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 112 | replay(mockControlPlaneMonitorService); |
Jian Li | 54df73e | 2016-02-01 17:09:03 -0800 | [diff] [blame] | 113 | basePostTest("disk-metrics-post.json", PREFIX + "/disk_metrics"); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Jian Li | 1fdd224 | 2016-02-05 10:01:19 -0800 | [diff] [blame] | 116 | /** |
| 117 | * Tests network metrics POST through REST API. |
| 118 | */ |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 119 | @Test |
Jian Li | 1fdd224 | 2016-02-05 10:01:19 -0800 | [diff] [blame] | 120 | public void testNetworkMetrics() { |
Jian Li | c5cb4a1 | 2016-02-03 23:24:42 -0800 | [diff] [blame] | 121 | mockControlPlaneMonitorService.updateMetric(anyObject(), anyInt(), anyString()); |
Jian Li | ba6b117 | 2016-02-01 22:40:42 -0800 | [diff] [blame] | 122 | expectLastCall().times(8); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 123 | replay(mockControlPlaneMonitorService); |
Jian Li | 54df73e | 2016-02-01 17:09:03 -0800 | [diff] [blame] | 124 | basePostTest("network-metrics-post.json", PREFIX + "/network_metrics"); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | @Test |
Jian Li | 1a42469 | 2016-02-03 16:21:18 -0800 | [diff] [blame^] | 128 | public void testSystemInfoPost() { |
| 129 | basePostTest("system-info-post.json", PREFIX + "/system_info"); |
| 130 | |
| 131 | SystemInfo si = SystemInfoFactory.getInstance().getSystemInfo(); |
| 132 | assertThat(si.cpuSpeed(), is(2048)); |
| 133 | assertThat(si.coreCount(), is(6)); |
| 134 | assertThat(si.cpuCount(), is(2)); |
| 135 | assertThat(si.totalMemory(), is(4096)); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Jian Li | ba6b117 | 2016-02-01 22:40:42 -0800 | [diff] [blame] | 138 | private ClientResponse baseTest(String jsonFile, String path) { |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 139 | final WebResource rs = resource(); |
| 140 | InputStream jsonStream = ControlMetricsCollectorResourceTest.class |
| 141 | .getResourceAsStream(jsonFile); |
| 142 | |
| 143 | assertThat(jsonStream, notNullValue()); |
| 144 | |
Jian Li | ba6b117 | 2016-02-01 22:40:42 -0800 | [diff] [blame] | 145 | return rs.path(path) |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 146 | .type(MediaType.APPLICATION_JSON_TYPE) |
| 147 | .post(ClientResponse.class, jsonStream); |
Jian Li | ba6b117 | 2016-02-01 22:40:42 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | private void basePostTest(String jsonFile, String path) { |
| 151 | ClientResponse response = baseTest(jsonFile, path); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 152 | assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK)); |
| 153 | } |
| 154 | |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 155 | /** |
| 156 | * Assigns an available port for the test. |
| 157 | * |
| 158 | * @param defaultPort If a port cannot be determined, this one is used. |
| 159 | * @return free port |
| 160 | */ |
| 161 | @Override |
| 162 | public int getPort(int defaultPort) { |
| 163 | try { |
| 164 | ServerSocket socket = new ServerSocket(0); |
| 165 | socket.setReuseAddress(true); |
| 166 | int port = socket.getLocalPort(); |
| 167 | socket.close(); |
| 168 | return port; |
| 169 | } catch (IOException ioe) { |
| 170 | return defaultPort; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public AppDescriptor configure() { |
| 176 | return new WebAppDescriptor.Builder("org.onosproject.cpman.rest").build(); |
| 177 | } |
| 178 | } |