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; |
| 40 | import static org.easymock.EasyMock.createMock; |
| 41 | import static org.easymock.EasyMock.expectLastCall; |
| 42 | import static org.easymock.EasyMock.replay; |
| 43 | import static org.hamcrest.Matchers.is; |
| 44 | import static org.hamcrest.Matchers.notNullValue; |
| 45 | import static org.junit.Assert.assertThat; |
| 46 | |
| 47 | /** |
| 48 | * Unit test for ControlMetricsCollector. |
| 49 | */ |
| 50 | public class ControlMetricsCollectorResourceTest extends JerseyTest { |
| 51 | |
| 52 | final ControlPlaneMonitorService mockControlPlaneMonitorService = |
| 53 | createMock(ControlPlaneMonitorService.class); |
| 54 | |
| 55 | /** |
| 56 | * Sets up the global values for all the tests. |
| 57 | */ |
| 58 | @Before |
| 59 | public void setUpTest() { |
| 60 | ServiceDirectory testDirectory = |
| 61 | new TestServiceDirectory() |
| 62 | .add(ControlPlaneMonitorService.class, mockControlPlaneMonitorService); |
| 63 | BaseResource.setServiceDirectory(testDirectory); |
| 64 | } |
| 65 | |
| 66 | @Test |
| 67 | public void testCpuMetricsPost() { |
| 68 | mockControlPlaneMonitorService.updateMetric(anyObject(), anyObject(), |
| 69 | (Optional<DeviceId>) anyObject()); |
| 70 | expectLastCall().times(5); |
| 71 | replay(mockControlPlaneMonitorService); |
| 72 | basePostTest("cpu-metrics-post.json", "cpman/cpumetrics"); |
| 73 | } |
| 74 | |
| 75 | @Test |
| 76 | public void testMemoryMetricsPost() { |
| 77 | mockControlPlaneMonitorService.updateMetric(anyObject(), anyObject(), |
| 78 | (Optional<DeviceId>) anyObject()); |
| 79 | expectLastCall().times(4); |
| 80 | replay(mockControlPlaneMonitorService); |
| 81 | basePostTest("memory-metrics-post.json", "cpman/memorymetrics"); |
| 82 | } |
| 83 | |
| 84 | @Test |
| 85 | public void testDiskMetricsPost() { |
| 86 | mockControlPlaneMonitorService.updateMetric(anyObject(), anyObject(), |
| 87 | (Optional<DeviceId>) anyObject()); |
| 88 | expectLastCall().times(2); |
| 89 | replay(mockControlPlaneMonitorService); |
| 90 | basePostTest("disk-metrics-post.json", "cpman/diskmetrics"); |
| 91 | } |
| 92 | |
| 93 | @Test |
| 94 | public void testNetworkMetricsPost() { |
| 95 | mockControlPlaneMonitorService.updateMetric(anyObject(), anyObject(), |
| 96 | (Optional<DeviceId>) anyObject()); |
| 97 | expectLastCall().times(4); |
| 98 | replay(mockControlPlaneMonitorService); |
| 99 | basePostTest("network-metrics-post.json", "cpman/networkmetrics"); |
| 100 | } |
| 101 | |
| 102 | @Test |
| 103 | public void testSystemSpecsPost() { |
| 104 | basePostTest("system-spec-post.json", "cpman/systemspecs"); |
| 105 | } |
| 106 | |
| 107 | private void basePostTest(String jsonFile, String path) { |
| 108 | final WebResource rs = resource(); |
| 109 | InputStream jsonStream = ControlMetricsCollectorResourceTest.class |
| 110 | .getResourceAsStream(jsonFile); |
| 111 | |
| 112 | assertThat(jsonStream, notNullValue()); |
| 113 | |
| 114 | ClientResponse response = rs.path(path) |
| 115 | .type(MediaType.APPLICATION_JSON_TYPE) |
| 116 | .post(ClientResponse.class, jsonStream); |
| 117 | assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK)); |
| 118 | } |
| 119 | |
| 120 | public ControlMetricsCollectorResourceTest() { |
| 121 | super(new WebAppDescriptor.Builder("javax.ws.rs.Application", |
| 122 | CPManWebApplication.class.getCanonicalName()) |
| 123 | .servletClass(ServletContainer.class).build()); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Assigns an available port for the test. |
| 128 | * |
| 129 | * @param defaultPort If a port cannot be determined, this one is used. |
| 130 | * @return free port |
| 131 | */ |
| 132 | @Override |
| 133 | public int getPort(int defaultPort) { |
| 134 | try { |
| 135 | ServerSocket socket = new ServerSocket(0); |
| 136 | socket.setReuseAddress(true); |
| 137 | int port = socket.getLocalPort(); |
| 138 | socket.close(); |
| 139 | return port; |
| 140 | } catch (IOException ioe) { |
| 141 | return defaultPort; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public AppDescriptor configure() { |
| 147 | return new WebAppDescriptor.Builder("org.onosproject.cpman.rest").build(); |
| 148 | } |
| 149 | } |