Jian Li | 5c41123 | 2015-12-16 15:29:16 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Jian Li | 5c41123 | 2015-12-16 15:29:16 -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 | |
Jian Li | 5c41123 | 2015-12-16 15:29:16 -0800 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 19 | import com.google.common.collect.ImmutableList; |
| 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.NetTestTools; |
| 25 | import org.onosproject.net.meter.Band; |
| 26 | import org.onosproject.net.meter.DefaultBand; |
| 27 | import org.onosproject.net.meter.DefaultMeter; |
| 28 | import org.onosproject.net.meter.Meter; |
| 29 | import org.onosproject.net.meter.MeterId; |
| 30 | |
Jian Li | 5c41123 | 2015-12-16 15:29:16 -0800 | [diff] [blame] | 31 | import static org.easymock.EasyMock.createMock; |
| 32 | import static org.easymock.EasyMock.expect; |
| 33 | import static org.easymock.EasyMock.replay; |
| 34 | import static org.hamcrest.MatcherAssert.assertThat; |
Jian Li | 5c41123 | 2015-12-16 15:29:16 -0800 | [diff] [blame] | 35 | import static org.hamcrest.Matchers.notNullValue; |
| 36 | import static org.onosproject.codec.impl.MeterJsonMatcher.matchesMeter; |
| 37 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 38 | |
| 39 | /** |
| 40 | * Unit tests for Meter codec. |
| 41 | */ |
| 42 | public class MeterCodecTest { |
| 43 | |
| 44 | MockCodecContext context; |
| 45 | JsonCodec<Meter> meterCodec; |
| 46 | final CoreService mockCoreService = createMock(CoreService.class); |
| 47 | |
| 48 | /** |
Jian Li | 64dd889 | 2015-12-30 14:13:18 -0800 | [diff] [blame] | 49 | * Sets up for each test. Creates a context and fetches the meter |
Jian Li | 5c41123 | 2015-12-16 15:29:16 -0800 | [diff] [blame] | 50 | * 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 Li | 5c41123 | 2015-12-16 15:29:16 -0800 | [diff] [blame] | 89 | } |