blob: bd93618d9f0e6e45c151dfd238899277c289028e [file] [log] [blame]
Jian Li64dd8892015-12-30 14:13:18 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Jian Li64dd8892015-12-30 14:13:18 -08003 *
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 */
16package org.onosproject.codec.impl;
17
18import com.codahale.metrics.Counter;
19import com.codahale.metrics.Meter;
20import com.codahale.metrics.Metric;
21import com.codahale.metrics.Timer;
22import com.fasterxml.jackson.databind.node.ObjectNode;
23import org.junit.Before;
24import org.junit.Test;
25import org.onosproject.codec.JsonCodec;
26import org.onosproject.core.CoreService;
27
28import java.util.concurrent.TimeUnit;
29
30import static org.easymock.EasyMock.createMock;
31import static org.easymock.EasyMock.replay;
32import static org.hamcrest.MatcherAssert.assertThat;
33import static org.hamcrest.Matchers.notNullValue;
34import static org.onosproject.codec.impl.MetricJsonMatcher.matchesMetric;
35
36/**
37 * Unit tests for Metric codec.
38 */
39public 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}