blob: 26d3712d995a29a3404a30e59cd30751b8a4ca6d [file] [log] [blame]
Jian Li5c411232015-12-16 15:29:16 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Jian Li5c411232015-12-16 15:29:16 -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
Jian Li5c411232015-12-16 15:29:16 -080018import com.fasterxml.jackson.databind.node.ObjectNode;
19import com.google.common.collect.ImmutableList;
20import org.junit.Before;
21import org.junit.Test;
22import org.onosproject.codec.JsonCodec;
23import org.onosproject.core.CoreService;
24import org.onosproject.net.NetTestTools;
25import org.onosproject.net.meter.Band;
26import org.onosproject.net.meter.DefaultBand;
27import org.onosproject.net.meter.DefaultMeter;
28import org.onosproject.net.meter.Meter;
29import org.onosproject.net.meter.MeterId;
30
Jian Li5c411232015-12-16 15:29:16 -080031import static org.easymock.EasyMock.createMock;
32import static org.easymock.EasyMock.expect;
33import static org.easymock.EasyMock.replay;
34import static org.hamcrest.MatcherAssert.assertThat;
Jian Li5c411232015-12-16 15:29:16 -080035import static org.hamcrest.Matchers.notNullValue;
36import static org.onosproject.codec.impl.MeterJsonMatcher.matchesMeter;
37import static org.onosproject.net.NetTestTools.APP_ID;
38
39/**
40 * Unit tests for Meter codec.
41 */
42public class MeterCodecTest {
43
44 MockCodecContext context;
45 JsonCodec<Meter> meterCodec;
46 final CoreService mockCoreService = createMock(CoreService.class);
47
48 /**
Jian Li64dd8892015-12-30 14:13:18 -080049 * Sets up for each test. Creates a context and fetches the meter
Jian Li5c411232015-12-16 15:29:16 -080050 * codec.
51 */
52 @Before
53 public void setUp() {
54 context = new MockCodecContext();
55 meterCodec = context.codec(Meter.class);
56 assertThat(meterCodec, notNullValue());
57
58 expect(mockCoreService.registerApplication(MeterCodec.REST_APP_ID))
59 .andReturn(APP_ID).anyTimes();
60 replay(mockCoreService);
61 context.registerService(CoreService.class, mockCoreService);
62 }
63
64 /**
65 * Tests encoding of a Meter object.
66 */
67 @Test
68 public void testMeterEncode() {
69 Band band1 = DefaultBand.builder()
70 .ofType(Band.Type.DROP)
71 .burstSize(10)
72 .withRate(10).build();
73 Band band2 = DefaultBand.builder()
74 .ofType(Band.Type.REMARK)
75 .burstSize(10)
76 .withRate(10)
77 .dropPrecedence((short) 10).build();
78
79 Meter meter = DefaultMeter.builder()
80 .fromApp(APP_ID)
81 .withId(MeterId.meterId(1L))
82 .forDevice(NetTestTools.did("d1"))
83 .withBands(ImmutableList.of(band1, band2))
84 .withUnit(Meter.Unit.KB_PER_SEC).build();
85
86 ObjectNode meterJson = meterCodec.encode(meter, context);
87 assertThat(meterJson, matchesMeter(meter));
88 }
Jian Li5c411232015-12-16 15:29:16 -080089}