blob: 4e9769bfcc8c884d84fc866d875183e52d2c683c [file] [log] [blame]
Jian Li95678962016-01-26 17:17:52 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Jian Li95678962016-01-26 17:17:52 -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.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.junit.Before;
21import org.junit.Test;
22import org.onosproject.codec.JsonCodec;
23import org.onosproject.core.CoreService;
24import org.onosproject.net.meter.Band;
25import org.onosproject.net.meter.MeterRequest;
26
27import java.io.IOException;
28import java.io.InputStream;
29
30import static org.easymock.EasyMock.createMock;
31import static org.easymock.EasyMock.expect;
32import static org.easymock.EasyMock.replay;
33import static org.hamcrest.MatcherAssert.assertThat;
34import static org.hamcrest.Matchers.is;
35import static org.hamcrest.Matchers.notNullValue;
36import static org.onosproject.net.NetTestTools.APP_ID;
37
38/**
39 * Unit tests for MeterRequest codec.
40 */
41public class MeterRequestCodecTest {
42 MockCodecContext context;
43 JsonCodec<MeterRequest> meterRequestCodec;
44 final CoreService mockCoreService = createMock(CoreService.class);
45
46 /**
47 * Sets up for each test. Creates a context and fetches the meterRequest
48 * codec.
49 */
50 @Before
51 public void setUp() {
52 context = new MockCodecContext();
53 meterRequestCodec = context.codec(MeterRequest.class);
54 assertThat(meterRequestCodec, notNullValue());
55
56 expect(mockCoreService.registerApplication(MeterCodec.REST_APP_ID))
57 .andReturn(APP_ID).anyTimes();
58 replay(mockCoreService);
59 context.registerService(CoreService.class, mockCoreService);
60 }
61
62 /**
63 * Test decoding of a MeterRequest object.
64 */
65 @Test
66 public void testMeterRequestDecode() throws IOException {
67 MeterRequest meterRequest = getMeterRequest("simple-meter-request.json");
68 checkCommonData(meterRequest);
69
70 assertThat(meterRequest.bands().size(), is(1));
71 Band band = meterRequest.bands().iterator().next();
72 assertThat(band.type().toString(), is("REMARK"));
73 assertThat(band.rate(), is(10L));
74 assertThat(band.dropPrecedence(), is((short) 20));
75 assertThat(band.burst(), is(30L));
76 }
77
78 /**
79 * Checks that the data shared by all the resource is correct for a given meterRequest.
80 *
81 * @param meterRequest meterRequest to check
82 */
83 private void checkCommonData(MeterRequest meterRequest) {
84 assertThat(meterRequest.deviceId().toString(), is("of:0000000000000001"));
85 assertThat(meterRequest.appId(), is(APP_ID));
86 assertThat(meterRequest.unit().toString(), is("KB_PER_SEC"));
87 }
88
89 /**
90 * Reads in a meter from the given resource and decodes it.
91 *
92 * @param resourceName resource to use to read the JSON for the rule
93 * @return decoded meterRequest
94 * @throws IOException if processing the resource fails
95 */
96 private MeterRequest getMeterRequest(String resourceName) throws IOException {
97 InputStream jsonStream = MeterRequestCodecTest.class.getResourceAsStream(resourceName);
98 JsonNode json = context.mapper().readTree(jsonStream);
99 assertThat(json, notNullValue());
100 MeterRequest meterRequest = meterRequestCodec.decode((ObjectNode) json, context);
101 assertThat(meterRequest, notNullValue());
102 return meterRequest;
103 }
104}