blob: 88f0a1cbf798f645a4e96027bc341bf7e4661002 [file] [log] [blame]
Sho SHIMIZUe571d442016-02-11 19:10:52 -08001/*
2 * Copyright 2016 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 */
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080016package org.onosproject.net.resource;
Sho SHIMIZUe571d442016-02-11 19:10:52 -080017
18import com.google.common.testing.EqualsTester;
19import org.junit.Test;
20import org.onlab.packet.VlanId;
21import org.onlab.util.Bandwidth;
22import org.onosproject.net.DeviceId;
23import org.onosproject.net.PortNumber;
24
25import java.util.Optional;
26
27import static org.hamcrest.Matchers.is;
28import static org.junit.Assert.*;
29
30/**
31 * Unit test for ContinuousResource.
32 */
33public 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 SHIMIZUe5930cc2016-02-12 16:52:29 -080055
Sho SHIMIZUe571d442016-02-11 19:10:52 -080056 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 SHIMIZUe5930cc2016-02-12 16:52:29 -080065
Sho SHIMIZUe571d442016-02-11 19:10:52 -080066 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 SHIMIZUe5930cc2016-02-12 16:52:29 -080073 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 SHIMIZUe571d442016-02-11 19:10:52 -080082 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 SHIMIZUe5930cc2016-02-12 16:52:29 -080088
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 SHIMIZUe571d442016-02-11 19:10:52 -0800115}