blob: 344a425892f3a3b8845149e3b892afcbd673029f [file] [log] [blame]
Brian Stankeaa6211a2016-02-04 18:22:54 -05001/*
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
Claudine Chiu31ad5272016-02-17 20:56:24 +000017package org.onosproject.net.key;
Brian Stankeaa6211a2016-02-04 18:22:54 -050018
19import org.junit.Test;
20
21import static org.junit.Assert.assertEquals;
22import static org.junit.Assert.assertNotNull;
23
24/**
25 * Test class for DeviceKey.
26 */
27public class DeviceKeyTest {
28
29 final String deviceKeyIdValue = "DeviceKeyId1";
30 final String deviceKeyLabel = "DeviceKeyLabel";
31 final String deviceKeySnmpName = "DeviceKeySnmpName";
Brian Stanke9bf8d7c2016-02-11 10:17:23 -050032 final String deviceKeyUsername = "DeviceKeyUsername";
33 final String deviceKeyPassword = "DeviceKeyPassword";
Brian Stankeaa6211a2016-02-04 18:22:54 -050034
35 /**
Brian Stanke9bf8d7c2016-02-11 10:17:23 -050036 * Checks the construction of a community name device key with a null
Brian Stankeaa6211a2016-02-04 18:22:54 -050037 * value passed into it. This will throw a NullPointerException.
38 */
39 @Test(expected = NullPointerException.class)
40 public void testCreateDeviceKeyUsingCommunityNameWithNull() {
41 DeviceKey deviceKey = DeviceKey.createDeviceKeyUsingCommunityName(null, null, null);
42 }
43
44 /**
Brian Stanke9bf8d7c2016-02-11 10:17:23 -050045 * Checks the construction of a community name device key name with non-null
Brian Stankeaa6211a2016-02-04 18:22:54 -050046 * values passed into it.
47 */
48 @Test
49 public void testCreateDeviceKeyUsingCommunityName() {
50 DeviceKeyId deviceKeyId = DeviceKeyId.deviceKeyId(deviceKeyIdValue);
51
52 DeviceKey deviceKey = DeviceKey.createDeviceKeyUsingCommunityName(deviceKeyId,
53 deviceKeyLabel, deviceKeySnmpName);
54 assertNotNull("The deviceKey should not be null.", deviceKey);
55 assertEquals("The deviceKeyId should match as expected", deviceKeyId, deviceKey.deviceKeyId());
56 assertEquals("The label should match as expected", deviceKeyLabel, deviceKey.label());
57 assertEquals("The type should match as expected", DeviceKey.Type.COMMUNITY_NAME, deviceKey.type());
58
59 CommunityName communityName = deviceKey.asCommunityName();
60 assertNotNull("The communityName should not be null.", communityName);
61 assertEquals("The name should match as expected", deviceKeySnmpName, communityName.name());
62 }
Brian Stanke9bf8d7c2016-02-11 10:17:23 -050063
64 /**
65 * Checks the invalid conversion a device key of type COMMUNITY_NAME to
66 * a username / password object.
67 */
68 @Test(expected = IllegalStateException.class)
69 public void testInvalidConversionToAsUsernamePassword() {
70 DeviceKeyId deviceKeyId = DeviceKeyId.deviceKeyId(deviceKeyIdValue);
71
72 DeviceKey deviceKey = DeviceKey.createDeviceKeyUsingCommunityName(deviceKeyId,
73 deviceKeyLabel, deviceKeySnmpName);
74 // Attempting to convert this device key to a username / password object
75 // should throw and IllegalStateException.
76 UsernamePassword usernamePassword = deviceKey.asUsernamePassword();
77 }
78
79 /**
80 * Checks the construction of a username / password device key with a null
81 * value passed into it. This will throw a NullPointerException.
82 */
83 @Test(expected = NullPointerException.class)
84 public void testCreateDeviceKeyUsingUsernamePasswordWithNull() {
85 DeviceKey deviceKey = DeviceKey.createDeviceKeyUsingUsernamePassword(null, null, null, null);
86 }
87
88 /**
89 * Checks the construction of a username and password device key name with non-null
90 * values passed into it.
91 */
92 @Test
93 public void testCreateDeviceKeyUsingUsernamePassword() {
94 DeviceKeyId deviceKeyId = DeviceKeyId.deviceKeyId(deviceKeyIdValue);
95
96 DeviceKey deviceKey = DeviceKey.createDeviceKeyUsingUsernamePassword(deviceKeyId, deviceKeyLabel,
97 deviceKeyUsername, deviceKeyPassword);
98 assertNotNull("The deviceKey should not be null.", deviceKey);
99 assertEquals("The deviceKeyId should match as expected", deviceKeyId, deviceKey.deviceKeyId());
100 assertEquals("The label should match as expected", deviceKeyLabel, deviceKey.label());
101 assertEquals("The type should match as expected", DeviceKey.Type.USERNAME_PASSWORD, deviceKey.type());
102
103 UsernamePassword usernamePassword = deviceKey.asUsernamePassword();
104 assertNotNull("The usernamePassword should not be null.", usernamePassword);
105 assertEquals("The username should match as expected", deviceKeyUsername, usernamePassword.username());
106 assertEquals("The password should match as expected", deviceKeyPassword, usernamePassword.password());
107 }
108
109 /**
110 * Checks the invalid conversion a device key of type USERNAME_PASSWORD to
111 * a community name object.
112 */
113 @Test(expected = IllegalStateException.class)
114 public void testInvalidConversionToAsCommunityName() {
115 DeviceKeyId deviceKeyId = DeviceKeyId.deviceKeyId(deviceKeyIdValue);
116
117 DeviceKey deviceKey = DeviceKey.createDeviceKeyUsingUsernamePassword(deviceKeyId, deviceKeyLabel,
118 deviceKeyUsername, deviceKeyPassword);
119 // Attempting to convert this device key to a community name should throw and IllegalStateException.
120 CommunityName communityName = deviceKey.asCommunityName();
121 }
Brian Stankeaa6211a2016-02-04 18:22:54 -0500122}