Committed by
Gerrit Code Review
ONOS-3655 - Adding Junit tests for DeviceKey subsystem, and adding DeviceKey
event, listener, store and delegate classes. DeviceKeyManager and store implementation will be added in the next submission. Change-Id: I60edb1753e9ee2985ccabc2b6ec9c223867590de
Showing
7 changed files
with
363 additions
and
0 deletions
1 | +/* | ||
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 | + */ | ||
16 | + | ||
17 | +package org.onosproject.incubator.net.key; | ||
18 | + | ||
19 | +import com.google.common.annotations.Beta; | ||
20 | +import org.onosproject.event.AbstractEvent; | ||
21 | + | ||
22 | +/** | ||
23 | + * Describes device key events. | ||
24 | + */ | ||
25 | +@Beta | ||
26 | +public class DeviceKeyEvent extends AbstractEvent<DeviceKeyEvent.Type, DeviceKey> { | ||
27 | + | ||
28 | + /** | ||
29 | + * Type of device key events. | ||
30 | + */ | ||
31 | + public enum Type { | ||
32 | + /** | ||
33 | + * Signals that a new device key has been added. | ||
34 | + */ | ||
35 | + DEVICE_KEY_ADDED, | ||
36 | + | ||
37 | + /** | ||
38 | + * Signals that a device key has been updated or changed state. | ||
39 | + */ | ||
40 | + DEVICE_KEY_UPDATED, | ||
41 | + | ||
42 | + /** | ||
43 | + * Signals that a device key has been removed. | ||
44 | + */ | ||
45 | + DEVICE_KEY_REMOVED | ||
46 | + } | ||
47 | + | ||
48 | + /** | ||
49 | + * Creates an event of a given type, and for the specified device key. | ||
50 | + * | ||
51 | + * @param type device key event type | ||
52 | + * @param deviceKey event device key subject | ||
53 | + */ | ||
54 | + public DeviceKeyEvent(Type type, DeviceKey deviceKey) { | ||
55 | + super(type, deviceKey); | ||
56 | + } | ||
57 | + | ||
58 | + /** | ||
59 | + * Creates an event of a given type, for the specified device key, and | ||
60 | + * the current time. | ||
61 | + * | ||
62 | + * @param type device key event type | ||
63 | + * @param deviceKey event device key subject | ||
64 | + * @param time occurrence time | ||
65 | + */ | ||
66 | + public DeviceKeyEvent(Type type, DeviceKey deviceKey, long time) { | ||
67 | + super(type, deviceKey, time); | ||
68 | + } | ||
69 | +} |
1 | +/* | ||
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 | + */ | ||
16 | + | ||
17 | +package org.onosproject.incubator.net.key; | ||
18 | + | ||
19 | +import com.google.common.annotations.Beta; | ||
20 | +import org.onosproject.event.EventListener; | ||
21 | + | ||
22 | +/** | ||
23 | + * Listener for device key related events. | ||
24 | + */ | ||
25 | +@Beta | ||
26 | +public interface DeviceKeyListener extends EventListener<DeviceKeyEvent> { | ||
27 | +} |
1 | +/* | ||
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 | + */ | ||
16 | + | ||
17 | +package org.onosproject.incubator.net.key; | ||
18 | + | ||
19 | +import com.google.common.annotations.Beta; | ||
20 | +import org.onosproject.store.Store; | ||
21 | + | ||
22 | +/** | ||
23 | + * Manages inventory of device keys; not intended for direct use. | ||
24 | + */ | ||
25 | +@Beta | ||
26 | +public interface DeviceKeyStore extends Store<DeviceKeyEvent, DeviceKeyStoreDelegate> { | ||
27 | + /** | ||
28 | + * Creates or updates a device key. | ||
29 | + * | ||
30 | + * @param deviceKey device key | ||
31 | + * @return device key event | ||
32 | + */ | ||
33 | + DeviceKeyEvent createOrUpdateDeviceKey(DeviceKey deviceKey); | ||
34 | + | ||
35 | + /** | ||
36 | + * Deletes a device key by a specific device key identifier. | ||
37 | + * | ||
38 | + * @param deviceKeyId device key unique identifier | ||
39 | + * @return device key event | ||
40 | + */ | ||
41 | + DeviceKeyEvent deleteDeviceKey(DeviceKeyId deviceKeyId); | ||
42 | +} |
incubator/api/src/main/java/org/onosproject/incubator/net/key/DeviceKeyStoreDelegate.java
0 → 100644
1 | +/* | ||
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 | + */ | ||
16 | + | ||
17 | +package org.onosproject.incubator.net.key; | ||
18 | + | ||
19 | +import com.google.common.annotations.Beta; | ||
20 | +import org.onosproject.store.StoreDelegate; | ||
21 | + | ||
22 | +/** | ||
23 | + * Device key store delegate abstraction. | ||
24 | + */ | ||
25 | +@Beta | ||
26 | +public interface DeviceKeyStoreDelegate extends StoreDelegate<DeviceKeyEvent> { | ||
27 | +} |
1 | +/* | ||
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 | + */ | ||
16 | + | ||
17 | +package org.onosproject.incubator.net.key; | ||
18 | + | ||
19 | +import org.junit.Test; | ||
20 | + | ||
21 | +import static org.junit.Assert.*; | ||
22 | +import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; | ||
23 | + | ||
24 | +/** | ||
25 | + * Test class for CommunityName. | ||
26 | + */ | ||
27 | +public class CommunityNameTest { | ||
28 | + | ||
29 | + final String cName = "CommunityName"; | ||
30 | + | ||
31 | + /** | ||
32 | + * Checks that the CommunityName class is immutable. | ||
33 | + */ | ||
34 | + @Test | ||
35 | + public void testImmutability() { | ||
36 | + assertThatClassIsImmutable(CommunityName.class); | ||
37 | + } | ||
38 | + | ||
39 | + /** | ||
40 | + * Checks the construction of a community name object with a null | ||
41 | + * value passed into it. | ||
42 | + */ | ||
43 | + @Test | ||
44 | + public void testCommunityNameNull() { | ||
45 | + CommunityName communityName = CommunityName.communityName(null); | ||
46 | + | ||
47 | + assertNotNull("The CommunityName should not be null.", communityName); | ||
48 | + assertNull("The name should be null.", communityName.name()); | ||
49 | + } | ||
50 | + | ||
51 | + /** | ||
52 | + * Checks the construction of a community name object with a non-null | ||
53 | + * value passed into it. | ||
54 | + */ | ||
55 | + @Test | ||
56 | + public void testCommunityName() { | ||
57 | + CommunityName communityName = CommunityName.communityName(cName); | ||
58 | + | ||
59 | + assertNotNull("The CommunityName should not be null.", communityName); | ||
60 | + assertEquals("The name should match the expected value.", cName, communityName.name()); | ||
61 | + } | ||
62 | +} |
1 | +/* | ||
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 | + */ | ||
16 | + | ||
17 | +package org.onosproject.incubator.net.key; | ||
18 | + | ||
19 | +import com.google.common.testing.EqualsTester; | ||
20 | +import org.junit.Test; | ||
21 | + | ||
22 | +import static org.junit.Assert.assertEquals; | ||
23 | +import static org.junit.Assert.assertNotNull; | ||
24 | +import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; | ||
25 | + | ||
26 | +/** | ||
27 | + * Test class for DeviceDeyId. | ||
28 | + */ | ||
29 | +public class DeviceKeyIdTest { | ||
30 | + | ||
31 | + final String deviceKeyIdValue1 = "DeviceKeyId1"; | ||
32 | + final String deviceKeyIdValue2 = "DeviceKeyId2"; | ||
33 | + | ||
34 | + /** | ||
35 | + * Checks that the DeviceKeyId class is immutable. | ||
36 | + */ | ||
37 | + @Test | ||
38 | + public void testImmutability() { | ||
39 | + assertThatClassIsImmutable(DeviceKeyId.class); | ||
40 | + } | ||
41 | + | ||
42 | + /** | ||
43 | + * Checks the construction of a DeviceKeyId object throws an | ||
44 | + * IllegalArgumentException when the input identifier is null. | ||
45 | + */ | ||
46 | + @Test(expected = NullPointerException.class) | ||
47 | + public void testConstructionUsingNullId() { | ||
48 | + DeviceKeyId.deviceKeyId(null); | ||
49 | + } | ||
50 | + | ||
51 | + /** | ||
52 | + * Checks the construction of a DeviceKeyId object. | ||
53 | + */ | ||
54 | + @Test | ||
55 | + public void testConstruction() { | ||
56 | + DeviceKeyId deviceKeyId = DeviceKeyId.deviceKeyId(deviceKeyIdValue1); | ||
57 | + assertNotNull("The deviceKeyId should not be null.", deviceKeyId); | ||
58 | + assertEquals("The id should match the expected value.", | ||
59 | + deviceKeyIdValue1, deviceKeyId.id()); | ||
60 | + } | ||
61 | + | ||
62 | + /** | ||
63 | + * Tests the equality of device key identifiers. | ||
64 | + */ | ||
65 | + @Test | ||
66 | + public void testEquality() { | ||
67 | + DeviceKeyId deviceKeyId1 = DeviceKeyId.deviceKeyId(deviceKeyIdValue1); | ||
68 | + DeviceKeyId deviceKeyId2 = DeviceKeyId.deviceKeyId(deviceKeyIdValue1); | ||
69 | + DeviceKeyId deviceKeyId3 = DeviceKeyId.deviceKeyId(deviceKeyIdValue2); | ||
70 | + | ||
71 | + new EqualsTester().addEqualityGroup(deviceKeyId1, deviceKeyId2) | ||
72 | + .addEqualityGroup(deviceKeyId3) | ||
73 | + .testEquals(); | ||
74 | + } | ||
75 | +} |
1 | +/* | ||
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 | + */ | ||
16 | + | ||
17 | +package org.onosproject.incubator.net.key; | ||
18 | + | ||
19 | +import org.junit.Test; | ||
20 | + | ||
21 | +import static org.junit.Assert.assertEquals; | ||
22 | +import static org.junit.Assert.assertNotNull; | ||
23 | + | ||
24 | +/** | ||
25 | + * Test class for DeviceKey. | ||
26 | + */ | ||
27 | +public class DeviceKeyTest { | ||
28 | + | ||
29 | + final String deviceKeyIdValue = "DeviceKeyId1"; | ||
30 | + final String deviceKeyLabel = "DeviceKeyLabel"; | ||
31 | + final String deviceKeySnmpName = "DeviceKeySnmpName"; | ||
32 | + | ||
33 | + /** | ||
34 | + * Checks the construction of a device key name with a null | ||
35 | + * value passed into it. This will throw a NullPointerException. | ||
36 | + */ | ||
37 | + @Test(expected = NullPointerException.class) | ||
38 | + public void testCreateDeviceKeyUsingCommunityNameWithNull() { | ||
39 | + DeviceKey deviceKey = DeviceKey.createDeviceKeyUsingCommunityName(null, null, null); | ||
40 | + } | ||
41 | + | ||
42 | + /** | ||
43 | + * Checks the construction of a device key name with non-null | ||
44 | + * values passed into it. | ||
45 | + */ | ||
46 | + @Test | ||
47 | + public void testCreateDeviceKeyUsingCommunityName() { | ||
48 | + DeviceKeyId deviceKeyId = DeviceKeyId.deviceKeyId(deviceKeyIdValue); | ||
49 | + | ||
50 | + DeviceKey deviceKey = DeviceKey.createDeviceKeyUsingCommunityName(deviceKeyId, | ||
51 | + deviceKeyLabel, deviceKeySnmpName); | ||
52 | + assertNotNull("The deviceKey should not be null.", deviceKey); | ||
53 | + assertEquals("The deviceKeyId should match as expected", deviceKeyId, deviceKey.deviceKeyId()); | ||
54 | + assertEquals("The label should match as expected", deviceKeyLabel, deviceKey.label()); | ||
55 | + assertEquals("The type should match as expected", DeviceKey.Type.COMMUNITY_NAME, deviceKey.type()); | ||
56 | + | ||
57 | + CommunityName communityName = deviceKey.asCommunityName(); | ||
58 | + assertNotNull("The communityName should not be null.", communityName); | ||
59 | + assertEquals("The name should match as expected", deviceKeySnmpName, communityName.name()); | ||
60 | + } | ||
61 | +} |
-
Please register or login to post a comment