Brian Stanke
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
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.incubator.net.key;
import com.google.common.annotations.Beta;
import org.onosproject.event.AbstractEvent;
/**
* Describes device key events.
*/
@Beta
public class DeviceKeyEvent extends AbstractEvent<DeviceKeyEvent.Type, DeviceKey> {
/**
* Type of device key events.
*/
public enum Type {
/**
* Signals that a new device key has been added.
*/
DEVICE_KEY_ADDED,
/**
* Signals that a device key has been updated or changed state.
*/
DEVICE_KEY_UPDATED,
/**
* Signals that a device key has been removed.
*/
DEVICE_KEY_REMOVED
}
/**
* Creates an event of a given type, and for the specified device key.
*
* @param type device key event type
* @param deviceKey event device key subject
*/
public DeviceKeyEvent(Type type, DeviceKey deviceKey) {
super(type, deviceKey);
}
/**
* Creates an event of a given type, for the specified device key, and
* the current time.
*
* @param type device key event type
* @param deviceKey event device key subject
* @param time occurrence time
*/
public DeviceKeyEvent(Type type, DeviceKey deviceKey, long time) {
super(type, deviceKey, time);
}
}
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.incubator.net.key;
import com.google.common.annotations.Beta;
import org.onosproject.event.EventListener;
/**
* Listener for device key related events.
*/
@Beta
public interface DeviceKeyListener extends EventListener<DeviceKeyEvent> {
}
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.incubator.net.key;
import com.google.common.annotations.Beta;
import org.onosproject.store.Store;
/**
* Manages inventory of device keys; not intended for direct use.
*/
@Beta
public interface DeviceKeyStore extends Store<DeviceKeyEvent, DeviceKeyStoreDelegate> {
/**
* Creates or updates a device key.
*
* @param deviceKey device key
* @return device key event
*/
DeviceKeyEvent createOrUpdateDeviceKey(DeviceKey deviceKey);
/**
* Deletes a device key by a specific device key identifier.
*
* @param deviceKeyId device key unique identifier
* @return device key event
*/
DeviceKeyEvent deleteDeviceKey(DeviceKeyId deviceKeyId);
}
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.incubator.net.key;
import com.google.common.annotations.Beta;
import org.onosproject.store.StoreDelegate;
/**
* Device key store delegate abstraction.
*/
@Beta
public interface DeviceKeyStoreDelegate extends StoreDelegate<DeviceKeyEvent> {
}
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.incubator.net.key;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
/**
* Test class for CommunityName.
*/
public class CommunityNameTest {
final String cName = "CommunityName";
/**
* Checks that the CommunityName class is immutable.
*/
@Test
public void testImmutability() {
assertThatClassIsImmutable(CommunityName.class);
}
/**
* Checks the construction of a community name object with a null
* value passed into it.
*/
@Test
public void testCommunityNameNull() {
CommunityName communityName = CommunityName.communityName(null);
assertNotNull("The CommunityName should not be null.", communityName);
assertNull("The name should be null.", communityName.name());
}
/**
* Checks the construction of a community name object with a non-null
* value passed into it.
*/
@Test
public void testCommunityName() {
CommunityName communityName = CommunityName.communityName(cName);
assertNotNull("The CommunityName should not be null.", communityName);
assertEquals("The name should match the expected value.", cName, communityName.name());
}
}
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.incubator.net.key;
import com.google.common.testing.EqualsTester;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
/**
* Test class for DeviceDeyId.
*/
public class DeviceKeyIdTest {
final String deviceKeyIdValue1 = "DeviceKeyId1";
final String deviceKeyIdValue2 = "DeviceKeyId2";
/**
* Checks that the DeviceKeyId class is immutable.
*/
@Test
public void testImmutability() {
assertThatClassIsImmutable(DeviceKeyId.class);
}
/**
* Checks the construction of a DeviceKeyId object throws an
* IllegalArgumentException when the input identifier is null.
*/
@Test(expected = NullPointerException.class)
public void testConstructionUsingNullId() {
DeviceKeyId.deviceKeyId(null);
}
/**
* Checks the construction of a DeviceKeyId object.
*/
@Test
public void testConstruction() {
DeviceKeyId deviceKeyId = DeviceKeyId.deviceKeyId(deviceKeyIdValue1);
assertNotNull("The deviceKeyId should not be null.", deviceKeyId);
assertEquals("The id should match the expected value.",
deviceKeyIdValue1, deviceKeyId.id());
}
/**
* Tests the equality of device key identifiers.
*/
@Test
public void testEquality() {
DeviceKeyId deviceKeyId1 = DeviceKeyId.deviceKeyId(deviceKeyIdValue1);
DeviceKeyId deviceKeyId2 = DeviceKeyId.deviceKeyId(deviceKeyIdValue1);
DeviceKeyId deviceKeyId3 = DeviceKeyId.deviceKeyId(deviceKeyIdValue2);
new EqualsTester().addEqualityGroup(deviceKeyId1, deviceKeyId2)
.addEqualityGroup(deviceKeyId3)
.testEquals();
}
}
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.incubator.net.key;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
/**
* Test class for DeviceKey.
*/
public class DeviceKeyTest {
final String deviceKeyIdValue = "DeviceKeyId1";
final String deviceKeyLabel = "DeviceKeyLabel";
final String deviceKeySnmpName = "DeviceKeySnmpName";
/**
* Checks the construction of a device key name with a null
* value passed into it. This will throw a NullPointerException.
*/
@Test(expected = NullPointerException.class)
public void testCreateDeviceKeyUsingCommunityNameWithNull() {
DeviceKey deviceKey = DeviceKey.createDeviceKeyUsingCommunityName(null, null, null);
}
/**
* Checks the construction of a device key name with non-null
* values passed into it.
*/
@Test
public void testCreateDeviceKeyUsingCommunityName() {
DeviceKeyId deviceKeyId = DeviceKeyId.deviceKeyId(deviceKeyIdValue);
DeviceKey deviceKey = DeviceKey.createDeviceKeyUsingCommunityName(deviceKeyId,
deviceKeyLabel, deviceKeySnmpName);
assertNotNull("The deviceKey should not be null.", deviceKey);
assertEquals("The deviceKeyId should match as expected", deviceKeyId, deviceKey.deviceKeyId());
assertEquals("The label should match as expected", deviceKeyLabel, deviceKey.label());
assertEquals("The type should match as expected", DeviceKey.Type.COMMUNITY_NAME, deviceKey.type());
CommunityName communityName = deviceKey.asCommunityName();
assertNotNull("The communityName should not be null.", communityName);
assertEquals("The name should match as expected", deviceKeySnmpName, communityName.name());
}
}