Jian Li | 64dd889 | 2015-12-30 14:13:18 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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.codec.impl; |
| 17 | |
| 18 | import com.codahale.metrics.Counter; |
| 19 | import com.codahale.metrics.Meter; |
| 20 | import com.codahale.metrics.Metric; |
| 21 | import com.codahale.metrics.Timer; |
| 22 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 23 | import org.junit.Before; |
| 24 | import org.junit.Test; |
| 25 | import org.onosproject.codec.JsonCodec; |
| 26 | import org.onosproject.core.CoreService; |
| 27 | |
| 28 | import java.util.concurrent.TimeUnit; |
| 29 | |
| 30 | import static org.easymock.EasyMock.createMock; |
| 31 | import static org.easymock.EasyMock.replay; |
| 32 | import static org.hamcrest.MatcherAssert.assertThat; |
| 33 | import static org.hamcrest.Matchers.notNullValue; |
| 34 | import static org.onosproject.codec.impl.MetricJsonMatcher.matchesMetric; |
| 35 | |
| 36 | /** |
| 37 | * Unit tests for Metric codec. |
| 38 | */ |
| 39 | public class MetricCodecTest { |
| 40 | |
| 41 | MockCodecContext context; |
| 42 | JsonCodec<Metric> metricCodec; |
| 43 | final CoreService mockCoreService = createMock(CoreService.class); |
| 44 | |
| 45 | /** |
| 46 | * Sets up for each test. Creates a context and fetches the metric codec. |
| 47 | */ |
| 48 | @Before |
| 49 | public void setUp() { |
| 50 | context = new MockCodecContext(); |
| 51 | metricCodec = context.codec(Metric.class); |
| 52 | assertThat(metricCodec, notNullValue()); |
| 53 | |
| 54 | replay(mockCoreService); |
| 55 | context.registerService(CoreService.class, mockCoreService); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Tests encoding of a Metric object. |
| 60 | */ |
| 61 | @Test |
| 62 | public void testMetricEncode() { |
| 63 | Counter counter = new Counter(); |
| 64 | Meter meter = new Meter(); |
| 65 | Timer timer = new Timer(); |
| 66 | |
| 67 | counter.inc(); |
| 68 | meter.mark(); |
| 69 | timer.update(1, TimeUnit.MILLISECONDS); |
| 70 | |
| 71 | ObjectNode counterJson = metricCodec.encode(counter, context); |
| 72 | assertThat(counterJson.get("counter"), matchesMetric(counter)); |
| 73 | |
| 74 | ObjectNode meterJson = metricCodec.encode(meter, context); |
| 75 | assertThat(meterJson.get("meter"), matchesMetric(meter)); |
| 76 | |
| 77 | ObjectNode timerJson = metricCodec.encode(timer, context); |
| 78 | assertThat(timerJson.get("timer"), matchesMetric(timer)); |
| 79 | } |
| 80 | } |