Sho SHIMIZU | e571d44 | 2016-02-11 19:10:52 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Sho SHIMIZU | e571d44 | 2016-02-11 19:10: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 | */ |
Sho SHIMIZU | e18cb12 | 2016-02-22 21:04:56 -0800 | [diff] [blame] | 16 | package org.onosproject.net.resource; |
Sho SHIMIZU | e571d44 | 2016-02-11 19:10:52 -0800 | [diff] [blame] | 17 | |
| 18 | import com.google.common.testing.EqualsTester; |
| 19 | import org.junit.Test; |
| 20 | import org.onlab.packet.VlanId; |
| 21 | import org.onlab.util.Bandwidth; |
| 22 | import org.onosproject.net.DeviceId; |
| 23 | import org.onosproject.net.PortNumber; |
| 24 | |
| 25 | import java.util.Optional; |
| 26 | |
| 27 | import static org.hamcrest.Matchers.is; |
| 28 | import static org.junit.Assert.*; |
| 29 | |
| 30 | /** |
| 31 | * Unit test for ContinuousResource. |
| 32 | */ |
| 33 | public class ContinuousResourceTest { |
| 34 | |
| 35 | private static final DeviceId D1 = DeviceId.deviceId("of:001"); |
| 36 | private static final PortNumber P1 = PortNumber.portNumber(1); |
| 37 | private static final Bandwidth BW1 = Bandwidth.gbps(2); |
| 38 | |
| 39 | @Test |
| 40 | public void testEquals() { |
| 41 | ContinuousResource resource1 = Resources.continuous(D1, P1, Bandwidth.class) |
| 42 | .resource(BW1.bps()); |
| 43 | ContinuousResource sameAsResource1 = Resources.continuous(D1, P1, Bandwidth.class) |
| 44 | .resource(BW1.bps()); |
| 45 | |
| 46 | new EqualsTester() |
| 47 | .addEqualityGroup(resource1, sameAsResource1) |
| 48 | .testEquals(); |
| 49 | } |
| 50 | |
| 51 | @Test |
| 52 | public void testTypeOf() { |
| 53 | ContinuousResource continuous = Resources.continuous(D1, P1, Bandwidth.class) |
| 54 | .resource(BW1.bps()); |
Sho SHIMIZU | e5930cc | 2016-02-12 16:52:29 -0800 | [diff] [blame] | 55 | |
Sho SHIMIZU | e571d44 | 2016-02-11 19:10:52 -0800 | [diff] [blame] | 56 | assertThat(continuous.isTypeOf(DeviceId.class), is(false)); |
| 57 | assertThat(continuous.isTypeOf(PortNumber.class), is(false)); |
| 58 | assertThat(continuous.isTypeOf(Bandwidth.class), is(true)); |
| 59 | } |
| 60 | |
| 61 | @Test |
| 62 | public void testSubTypeOf() { |
| 63 | ContinuousResource continuous = Resources.continuous(D1, P1, Bandwidth.class) |
| 64 | .resource(BW1.bps()); |
Sho SHIMIZU | e5930cc | 2016-02-12 16:52:29 -0800 | [diff] [blame] | 65 | |
Sho SHIMIZU | e571d44 | 2016-02-11 19:10:52 -0800 | [diff] [blame] | 66 | assertThat(continuous.isSubTypeOf(DeviceId.class), is(true)); |
| 67 | assertThat(continuous.isSubTypeOf(PortNumber.class), is(true)); |
| 68 | assertThat(continuous.isSubTypeOf(Bandwidth.class), is(true)); |
| 69 | assertThat(continuous.isSubTypeOf(VlanId.class), is(false)); |
| 70 | } |
| 71 | |
| 72 | @Test |
Sho SHIMIZU | e5930cc | 2016-02-12 16:52:29 -0800 | [diff] [blame] | 73 | public void testSubTypeOfObject() { |
| 74 | ContinuousResource continuous = Resources.continuous(D1, P1, Bandwidth.class) |
| 75 | .resource(BW1.bps()); |
| 76 | |
| 77 | assertThat(continuous.isSubTypeOf(Object.class), is(true)); |
| 78 | } |
| 79 | |
| 80 | @Test |
| 81 | public void testValueAsPrimitiveDouble() { |
Sho SHIMIZU | e571d44 | 2016-02-11 19:10:52 -0800 | [diff] [blame] | 82 | ContinuousResource resource = Resources.continuous(D1, P1, Bandwidth.class) |
| 83 | .resource(BW1.bps()); |
| 84 | |
| 85 | Optional<Double> volume = resource.valueAs(double.class); |
| 86 | assertThat(volume.get(), is(BW1.bps())); |
| 87 | } |
Sho SHIMIZU | e5930cc | 2016-02-12 16:52:29 -0800 | [diff] [blame] | 88 | |
| 89 | @Test |
| 90 | public void testValueAsDouble() { |
| 91 | ContinuousResource resource = Resources.continuous(D1, P1, Bandwidth.class) |
| 92 | .resource(BW1.bps()); |
| 93 | |
| 94 | Optional<Double> value = resource.valueAs(Double.class); |
| 95 | assertThat(value.get(), is(BW1.bps())); |
| 96 | } |
| 97 | |
| 98 | @Test |
| 99 | public void testValueAsObject() { |
| 100 | ContinuousResource resource = Resources.continuous(D1, P1, Bandwidth.class) |
| 101 | .resource(BW1.bps()); |
| 102 | |
| 103 | Optional<Double> value = resource.valueAs(Double.class); |
| 104 | assertThat(value.get(), is(BW1.bps())); |
| 105 | } |
| 106 | |
| 107 | @Test |
| 108 | public void testValueAsIncompatibleType() { |
| 109 | ContinuousResource resource = Resources.continuous(D1, P1, Bandwidth.class) |
| 110 | .resource(BW1.bps()); |
| 111 | |
| 112 | Optional<VlanId> value = resource.valueAs(VlanId.class); |
| 113 | assertThat(value, is(Optional.empty())); |
| 114 | } |
Sho SHIMIZU | e571d44 | 2016-02-11 19:10:52 -0800 | [diff] [blame] | 115 | } |