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; |
| 30 | import org.onosproject.net.DeviceId; |
| 31 | |
| 32 | import javax.ws.rs.core.MediaType; |
| 33 | import java.io.IOException; |
| 34 | import java.io.InputStream; |
| 35 | import java.net.HttpURLConnection; |
| 36 | import java.net.ServerSocket; |
| 37 | import java.util.Optional; |
| 38 | |
| 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 | /** |
| 59 | * Sets up the global values for all the tests. |
| 60 | */ |
| 61 | @Before |
| 62 | public void setUpTest() { |
| 63 | ServiceDirectory testDirectory = |
| 64 | new TestServiceDirectory() |
| 65 | .add(ControlPlaneMonitorService.class, mockControlPlaneMonitorService); |
| 66 | BaseResource.setServiceDirectory(testDirectory); |
| 67 | } |
| 68 | |
| 69 | @Test |
| 70 | public void testCpuMetricsPost() { |
| 71 | mockControlPlaneMonitorService.updateMetric(anyObject(), anyObject(), |
| 72 | (Optional<DeviceId>) anyObject()); |
| 73 | expectLastCall().times(5); |
| 74 | replay(mockControlPlaneMonitorService); |
Jian Li | 54df73e | 2016-02-01 17:09:03 -0800 | [diff] [blame] | 75 | basePostTest("cpu-metrics-post.json", PREFIX + "/cpu_metrics"); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | @Test |
| 79 | public void testMemoryMetricsPost() { |
| 80 | mockControlPlaneMonitorService.updateMetric(anyObject(), anyObject(), |
| 81 | (Optional<DeviceId>) anyObject()); |
| 82 | expectLastCall().times(4); |
| 83 | replay(mockControlPlaneMonitorService); |
Jian Li | 54df73e | 2016-02-01 17:09:03 -0800 | [diff] [blame] | 84 | basePostTest("memory-metrics-post.json", PREFIX + "/memory_metrics"); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Test |
Jian Li | ba6b117 | 2016-02-01 22:40:42 -0800 | [diff] [blame] | 88 | public void testDiskMetricsWithNullName() { |
| 89 | mockControlPlaneMonitorService.updateMetric(anyObject(), anyObject(), anyString()); |
| 90 | expectLastCall().times(4); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 91 | replay(mockControlPlaneMonitorService); |
Jian Li | 54df73e | 2016-02-01 17:09:03 -0800 | [diff] [blame] | 92 | basePostTest("disk-metrics-post.json", PREFIX + "/disk_metrics"); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | @Test |
Jian Li | ba6b117 | 2016-02-01 22:40:42 -0800 | [diff] [blame] | 96 | public void testNetworkMetricsWithNullName() { |
| 97 | mockControlPlaneMonitorService.updateMetric(anyObject(), anyObject(), anyString()); |
| 98 | expectLastCall().times(8); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 99 | replay(mockControlPlaneMonitorService); |
Jian Li | 54df73e | 2016-02-01 17:09:03 -0800 | [diff] [blame] | 100 | basePostTest("network-metrics-post.json", PREFIX + "/network_metrics"); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Test |
| 104 | public void testSystemSpecsPost() { |
Jian Li | 54df73e | 2016-02-01 17:09:03 -0800 | [diff] [blame] | 105 | basePostTest("system-spec-post.json", PREFIX + "/system_specs"); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 106 | } |
| 107 | |
Jian Li | ba6b117 | 2016-02-01 22:40:42 -0800 | [diff] [blame] | 108 | private ClientResponse baseTest(String jsonFile, String path) { |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 109 | final WebResource rs = resource(); |
| 110 | InputStream jsonStream = ControlMetricsCollectorResourceTest.class |
| 111 | .getResourceAsStream(jsonFile); |
| 112 | |
| 113 | assertThat(jsonStream, notNullValue()); |
| 114 | |
Jian Li | ba6b117 | 2016-02-01 22:40:42 -0800 | [diff] [blame] | 115 | return rs.path(path) |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 116 | .type(MediaType.APPLICATION_JSON_TYPE) |
| 117 | .post(ClientResponse.class, jsonStream); |
Jian Li | ba6b117 | 2016-02-01 22:40:42 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | private void basePostTest(String jsonFile, String path) { |
| 121 | ClientResponse response = baseTest(jsonFile, path); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 122 | assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK)); |
| 123 | } |
| 124 | |
| 125 | public ControlMetricsCollectorResourceTest() { |
| 126 | super(new WebAppDescriptor.Builder("javax.ws.rs.Application", |
| 127 | CPManWebApplication.class.getCanonicalName()) |
| 128 | .servletClass(ServletContainer.class).build()); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Assigns an available port for the test. |
| 133 | * |
| 134 | * @param defaultPort If a port cannot be determined, this one is used. |
| 135 | * @return free port |
| 136 | */ |
| 137 | @Override |
| 138 | public int getPort(int defaultPort) { |
| 139 | try { |
| 140 | ServerSocket socket = new ServerSocket(0); |
| 141 | socket.setReuseAddress(true); |
| 142 | int port = socket.getLocalPort(); |
| 143 | socket.close(); |
| 144 | return port; |
| 145 | } catch (IOException ioe) { |
| 146 | return defaultPort; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public AppDescriptor configure() { |
| 152 | return new WebAppDescriptor.Builder("org.onosproject.cpman.rest").build(); |
| 153 | } |
| 154 | } |