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 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 18 | import org.glassfish.jersey.server.ResourceConfig; |
| 19 | import org.glassfish.jersey.test.JerseyTest; |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [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.rest.BaseResource; |
| 25 | import org.onosproject.cpman.ControlPlaneMonitorService; |
Jian Li | 1a42469 | 2016-02-03 16:21:18 -0800 | [diff] [blame] | 26 | import org.onosproject.cpman.SystemInfo; |
| 27 | import org.onosproject.cpman.impl.SystemInfoFactory; |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 28 | import org.onosproject.net.DeviceId; |
| 29 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 30 | import javax.ws.rs.client.Entity; |
| 31 | import javax.ws.rs.client.WebTarget; |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 32 | import javax.ws.rs.core.MediaType; |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 33 | import javax.ws.rs.core.Response; |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 34 | import java.io.InputStream; |
| 35 | import java.net.HttpURLConnection; |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 36 | import java.util.Optional; |
| 37 | |
Jian Li | c5cb4a1 | 2016-02-03 23:24:42 -0800 | [diff] [blame] | 38 | import static org.easymock.EasyMock.anyInt; |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 39 | import static org.easymock.EasyMock.anyObject; |
Jian Li | ba6b117 | 2016-02-01 22:40:42 -0800 | [diff] [blame] | 40 | import static org.easymock.EasyMock.anyString; |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 41 | import static org.easymock.EasyMock.createMock; |
| 42 | import static org.easymock.EasyMock.expectLastCall; |
| 43 | import static org.easymock.EasyMock.replay; |
| 44 | import static org.hamcrest.Matchers.is; |
| 45 | import static org.hamcrest.Matchers.notNullValue; |
| 46 | import static org.junit.Assert.assertThat; |
| 47 | |
| 48 | /** |
| 49 | * Unit test for ControlMetricsCollector. |
| 50 | */ |
| 51 | public class ControlMetricsCollectorResourceTest extends JerseyTest { |
| 52 | |
| 53 | final ControlPlaneMonitorService mockControlPlaneMonitorService = |
| 54 | createMock(ControlPlaneMonitorService.class); |
| 55 | |
Jian Li | 54df73e | 2016-02-01 17:09:03 -0800 | [diff] [blame] | 56 | private static final String PREFIX = "collector"; |
| 57 | |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 58 | /** |
Jian Li | 1fdd224 | 2016-02-05 10:01:19 -0800 | [diff] [blame] | 59 | * Constructs a control metrics collector resource test instance. |
| 60 | */ |
| 61 | public ControlMetricsCollectorResourceTest() { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 62 | super(ResourceConfig.forApplicationClass(CPManWebApplication.class)); |
Jian Li | 1fdd224 | 2016-02-05 10:01:19 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /** |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 66 | * Sets up the global values for all the tests. |
| 67 | */ |
| 68 | @Before |
| 69 | public void setUpTest() { |
| 70 | ServiceDirectory testDirectory = |
| 71 | new TestServiceDirectory() |
| 72 | .add(ControlPlaneMonitorService.class, mockControlPlaneMonitorService); |
| 73 | BaseResource.setServiceDirectory(testDirectory); |
| 74 | } |
| 75 | |
Jian Li | 1fdd224 | 2016-02-05 10:01:19 -0800 | [diff] [blame] | 76 | /** |
| 77 | * Tests CPU metrics POST through REST API. |
| 78 | */ |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 79 | @Test |
| 80 | public void testCpuMetricsPost() { |
Jian Li | c5cb4a1 | 2016-02-03 23:24:42 -0800 | [diff] [blame] | 81 | mockControlPlaneMonitorService.updateMetric(anyObject(), anyInt(), |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 82 | (Optional<DeviceId>) anyObject()); |
| 83 | expectLastCall().times(5); |
| 84 | replay(mockControlPlaneMonitorService); |
Jian Li | 54df73e | 2016-02-01 17:09:03 -0800 | [diff] [blame] | 85 | basePostTest("cpu-metrics-post.json", PREFIX + "/cpu_metrics"); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Jian Li | 1fdd224 | 2016-02-05 10:01:19 -0800 | [diff] [blame] | 88 | /** |
| 89 | * Tests memory metrics POST through REST API. |
| 90 | */ |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 91 | @Test |
| 92 | public void testMemoryMetricsPost() { |
Jian Li | c5cb4a1 | 2016-02-03 23:24:42 -0800 | [diff] [blame] | 93 | mockControlPlaneMonitorService.updateMetric(anyObject(), anyInt(), |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 94 | (Optional<DeviceId>) anyObject()); |
| 95 | expectLastCall().times(4); |
| 96 | replay(mockControlPlaneMonitorService); |
Jian Li | 54df73e | 2016-02-01 17:09:03 -0800 | [diff] [blame] | 97 | basePostTest("memory-metrics-post.json", PREFIX + "/memory_metrics"); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Jian Li | 1fdd224 | 2016-02-05 10:01:19 -0800 | [diff] [blame] | 100 | /** |
| 101 | * Tests disk metrics POST through REST API. |
| 102 | */ |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 103 | @Test |
Jian Li | 1fdd224 | 2016-02-05 10:01:19 -0800 | [diff] [blame] | 104 | public void testDiskMetrics() { |
Jian Li | c5cb4a1 | 2016-02-03 23:24:42 -0800 | [diff] [blame] | 105 | mockControlPlaneMonitorService.updateMetric(anyObject(), anyInt(), anyString()); |
Jian Li | ba6b117 | 2016-02-01 22:40:42 -0800 | [diff] [blame] | 106 | expectLastCall().times(4); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 107 | replay(mockControlPlaneMonitorService); |
Jian Li | 54df73e | 2016-02-01 17:09:03 -0800 | [diff] [blame] | 108 | basePostTest("disk-metrics-post.json", PREFIX + "/disk_metrics"); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Jian Li | 1fdd224 | 2016-02-05 10:01:19 -0800 | [diff] [blame] | 111 | /** |
| 112 | * Tests network metrics POST through REST API. |
| 113 | */ |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 114 | @Test |
Jian Li | 1fdd224 | 2016-02-05 10:01:19 -0800 | [diff] [blame] | 115 | public void testNetworkMetrics() { |
Jian Li | c5cb4a1 | 2016-02-03 23:24:42 -0800 | [diff] [blame] | 116 | mockControlPlaneMonitorService.updateMetric(anyObject(), anyInt(), anyString()); |
Jian Li | ba6b117 | 2016-02-01 22:40:42 -0800 | [diff] [blame] | 117 | expectLastCall().times(8); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 118 | replay(mockControlPlaneMonitorService); |
Jian Li | 54df73e | 2016-02-01 17:09:03 -0800 | [diff] [blame] | 119 | basePostTest("network-metrics-post.json", PREFIX + "/network_metrics"); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | @Test |
Jian Li | 1a42469 | 2016-02-03 16:21:18 -0800 | [diff] [blame] | 123 | public void testSystemInfoPost() { |
| 124 | basePostTest("system-info-post.json", PREFIX + "/system_info"); |
| 125 | |
| 126 | SystemInfo si = SystemInfoFactory.getInstance().getSystemInfo(); |
| 127 | assertThat(si.cpuSpeed(), is(2048)); |
| 128 | assertThat(si.coreCount(), is(6)); |
| 129 | assertThat(si.cpuCount(), is(2)); |
| 130 | assertThat(si.totalMemory(), is(4096)); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 133 | private Response baseTest(String jsonFile, String path) { |
| 134 | final WebTarget wt = target(); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 135 | InputStream jsonStream = ControlMetricsCollectorResourceTest.class |
| 136 | .getResourceAsStream(jsonFile); |
| 137 | |
| 138 | assertThat(jsonStream, notNullValue()); |
| 139 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 140 | return wt.path(path) |
| 141 | .request(MediaType.APPLICATION_JSON_TYPE) |
| 142 | .post(Entity.json(jsonStream)); |
Jian Li | ba6b117 | 2016-02-01 22:40:42 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | private void basePostTest(String jsonFile, String path) { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 146 | Response response = baseTest(jsonFile, path); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 147 | assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK)); |
| 148 | } |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 149 | } |