Jian Li | 9567896 | 2016-01-26 17:17:52 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Jian Li | 9567896 | 2016-01-26 17:17:52 -0800 | [diff] [blame] | 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.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| 22 | import org.onosproject.codec.JsonCodec; |
| 23 | import org.onosproject.core.CoreService; |
| 24 | import org.onosproject.net.meter.Band; |
| 25 | import org.onosproject.net.meter.MeterRequest; |
| 26 | |
| 27 | import java.io.IOException; |
| 28 | import java.io.InputStream; |
| 29 | |
| 30 | import static org.easymock.EasyMock.createMock; |
| 31 | import static org.easymock.EasyMock.expect; |
| 32 | import static org.easymock.EasyMock.replay; |
| 33 | import static org.hamcrest.MatcherAssert.assertThat; |
| 34 | import static org.hamcrest.Matchers.is; |
| 35 | import static org.hamcrest.Matchers.notNullValue; |
| 36 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 37 | |
| 38 | /** |
| 39 | * Unit tests for MeterRequest codec. |
| 40 | */ |
| 41 | public 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 | } |