blob: 476785dd810512e762bcdce217ee0c3a0c0c0324 [file] [log] [blame]
Jian Li5c411232015-12-16 15:29:16 -08001/*
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 */
16package org.onosproject.codec.impl;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import com.google.common.collect.ImmutableList;
21import org.junit.Before;
22import org.junit.Test;
23import org.onosproject.codec.JsonCodec;
24import org.onosproject.core.CoreService;
25import org.onosproject.net.NetTestTools;
26import org.onosproject.net.meter.Band;
27import org.onosproject.net.meter.DefaultBand;
28import org.onosproject.net.meter.DefaultMeter;
29import org.onosproject.net.meter.Meter;
30import org.onosproject.net.meter.MeterId;
31
32import java.io.IOException;
33import java.io.InputStream;
34
35import static org.easymock.EasyMock.createMock;
36import static org.easymock.EasyMock.expect;
37import static org.easymock.EasyMock.replay;
38import static org.hamcrest.MatcherAssert.assertThat;
39import static org.hamcrest.Matchers.is;
40import static org.hamcrest.Matchers.notNullValue;
41import static org.onosproject.codec.impl.MeterJsonMatcher.matchesMeter;
42import static org.onosproject.net.NetTestTools.APP_ID;
43
44/**
45 * Unit tests for Meter codec.
46 */
47public class MeterCodecTest {
48
49 MockCodecContext context;
50 JsonCodec<Meter> meterCodec;
51 final CoreService mockCoreService = createMock(CoreService.class);
52
53 /**
Jian Li64dd8892015-12-30 14:13:18 -080054 * Sets up for each test. Creates a context and fetches the meter
Jian Li5c411232015-12-16 15:29:16 -080055 * codec.
56 */
57 @Before
58 public void setUp() {
59 context = new MockCodecContext();
60 meterCodec = context.codec(Meter.class);
61 assertThat(meterCodec, notNullValue());
62
63 expect(mockCoreService.registerApplication(MeterCodec.REST_APP_ID))
64 .andReturn(APP_ID).anyTimes();
65 replay(mockCoreService);
66 context.registerService(CoreService.class, mockCoreService);
67 }
68
69 /**
70 * Tests encoding of a Meter object.
71 */
72 @Test
73 public void testMeterEncode() {
74 Band band1 = DefaultBand.builder()
75 .ofType(Band.Type.DROP)
76 .burstSize(10)
77 .withRate(10).build();
78 Band band2 = DefaultBand.builder()
79 .ofType(Band.Type.REMARK)
80 .burstSize(10)
81 .withRate(10)
82 .dropPrecedence((short) 10).build();
83
84 Meter meter = DefaultMeter.builder()
85 .fromApp(APP_ID)
86 .withId(MeterId.meterId(1L))
87 .forDevice(NetTestTools.did("d1"))
88 .withBands(ImmutableList.of(band1, band2))
89 .withUnit(Meter.Unit.KB_PER_SEC).build();
90
91 ObjectNode meterJson = meterCodec.encode(meter, context);
92 assertThat(meterJson, matchesMeter(meter));
93 }
94
95 /**
96 * Test decoding of a Meter object.
97 */
98 @Test
99 public void testMeterDecode() throws IOException {
100 Meter meter = getMeter("simple-meter.json");
101 checkCommonData(meter);
102
103 assertThat(meter.bands().size(), is(1));
104 Band band = meter.bands().iterator().next();
105 assertThat(band.type().toString(), is("REMARK"));
106 assertThat(band.rate(), is(10L));
107 assertThat(band.dropPrecedence(), is((short) 20));
108 assertThat(band.burst(), is(30L));
109 }
110
111 /**
112 * Checks that the data shared by all the resource is correct for a given meter.
113 *
114 * @param meter meter to check
115 */
116 private void checkCommonData(Meter meter) {
117 assertThat(meter.id().id(), is(1L));
118 assertThat(meter.deviceId().toString(), is("of:0000000000000001"));
119 assertThat(meter.appId(), is(APP_ID));
120 assertThat(meter.unit().toString(), is("KB_PER_SEC"));
121 }
122
123 /**
124 * Reads in a meter from the given resource and decodes it.
125 *
126 * @param resourceName resource to use to read the JSON for the rule
127 * @return decoded meter
128 * @throws IOException if processing the resource fails
129 */
130 private Meter getMeter(String resourceName) throws IOException {
131 InputStream jsonStream = MeterCodecTest.class.getResourceAsStream(resourceName);
132 JsonNode json = context.mapper().readTree(jsonStream);
133 assertThat(json, notNullValue());
134 Meter meter = meterCodec.decode((ObjectNode) json, context);
135 assertThat(meter, notNullValue());
136 return meter;
137 }
138}