Jian Li | 7231515 | 2015-12-10 17:20:43 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2014-2015 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.rest; |
| 17 | |
| 18 | import com.codahale.metrics.Counter; |
| 19 | import com.codahale.metrics.Meter; |
| 20 | import com.codahale.metrics.Metric; |
| 21 | import com.eclipsesource.json.JsonArray; |
| 22 | import com.eclipsesource.json.JsonObject; |
| 23 | import com.google.common.collect.ImmutableMap; |
| 24 | import com.sun.jersey.api.client.WebResource; |
| 25 | import org.junit.After; |
| 26 | import org.junit.Before; |
| 27 | import org.junit.Test; |
| 28 | import org.onlab.metrics.MetricsService; |
| 29 | import org.onlab.osgi.ServiceDirectory; |
| 30 | import org.onlab.osgi.TestServiceDirectory; |
| 31 | import org.onlab.rest.BaseResource; |
| 32 | import org.onosproject.codec.CodecService; |
| 33 | import org.onosproject.codec.impl.CodecManager; |
| 34 | |
| 35 | import static org.easymock.EasyMock.createMock; |
| 36 | import static org.easymock.EasyMock.expect; |
| 37 | import static org.easymock.EasyMock.replay; |
| 38 | import static org.easymock.EasyMock.verify; |
| 39 | import static org.hamcrest.Matchers.containsString; |
| 40 | import static org.hamcrest.Matchers.is; |
| 41 | import static org.hamcrest.Matchers.notNullValue; |
| 42 | import static org.junit.Assert.assertThat; |
| 43 | |
| 44 | /** |
| 45 | * Unit tests for Metrics REST APIs. |
| 46 | */ |
| 47 | public class MetricsResourceTest extends ResourceTest { |
| 48 | MetricsService mockMetricsService; |
| 49 | |
| 50 | /** |
| 51 | * Initializes test mocks and environment. |
| 52 | */ |
| 53 | @Before |
| 54 | public void setUpTest() { |
| 55 | mockMetricsService = createMock(MetricsService.class); |
| 56 | |
| 57 | // Register the services needed for the test |
| 58 | final CodecManager codecService = new CodecManager(); |
| 59 | codecService.activate(); |
| 60 | ServiceDirectory testDirectory = |
| 61 | new TestServiceDirectory() |
| 62 | .add(MetricsService.class, mockMetricsService) |
| 63 | .add(CodecService.class, codecService); |
| 64 | BaseResource.setServiceDirectory(testDirectory); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Verifies mocks. |
| 69 | */ |
| 70 | @After |
| 71 | public void tearDownTest() { |
| 72 | verify(mockMetricsService); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Tests that a fetch of a non-existent object throws an exception. |
| 77 | */ |
| 78 | @Test |
| 79 | public void testBadGet() { |
| 80 | Counter onosCounter = new Counter(); |
| 81 | onosCounter.inc(); |
| 82 | |
| 83 | Meter onosMeter = new Meter(); |
| 84 | onosMeter.mark(); |
| 85 | |
| 86 | ImmutableMap<String, Metric> metrics = |
| 87 | new ImmutableMap.Builder<String, Metric>() |
| 88 | .put("onosCounter", onosCounter) |
| 89 | .put("onosMeter", onosMeter) |
| 90 | .build(); |
| 91 | |
| 92 | expect(mockMetricsService.getMetrics()) |
| 93 | .andReturn(metrics) |
| 94 | .anyTimes(); |
| 95 | |
| 96 | replay(mockMetricsService); |
| 97 | |
| 98 | WebResource rs = resource(); |
| 99 | String response = rs.path("metrics").get(String.class); |
| 100 | assertThat(response, containsString("{\"metrics\":[")); |
| 101 | |
| 102 | JsonObject result = JsonObject.readFrom(response); |
| 103 | assertThat(result, notNullValue()); |
| 104 | |
| 105 | JsonArray jsonMetrics = result.get("metrics").asArray(); |
| 106 | assertThat(jsonMetrics, notNullValue()); |
| 107 | assertThat(jsonMetrics.size(), is(2)); |
| 108 | } |
| 109 | } |