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