Add unit tests for ControlMessageEvent and DefaultControlMessage
Change-Id: I3909b528befa479be96256d999b953fb923a9fda
Showing
3 changed files
with
136 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 | +package org.onosproject.cpman.message; | ||
| 17 | + | ||
| 18 | +import org.junit.Test; | ||
| 19 | +import org.onosproject.cpman.ControlMessage; | ||
| 20 | +import org.onosproject.cpman.DefaultControlMessage; | ||
| 21 | +import org.onosproject.event.AbstractEventTest; | ||
| 22 | + | ||
| 23 | +import java.util.ArrayList; | ||
| 24 | +import java.util.Collection; | ||
| 25 | + | ||
| 26 | +import static org.onosproject.cpman.ControlMessage.Type.*; | ||
| 27 | + | ||
| 28 | +/** | ||
| 29 | + * Tests of the control message event. | ||
| 30 | + */ | ||
| 31 | +public class ControlMessageEventTest extends AbstractEventTest { | ||
| 32 | + | ||
| 33 | + private ControlMessage createControlMessage(ControlMessage.Type type) { | ||
| 34 | + return new DefaultControlMessage(type, 0L, 0L, 0L, 0L); | ||
| 35 | + } | ||
| 36 | + | ||
| 37 | + private Collection<ControlMessage> createControlMessages() { | ||
| 38 | + Collection<ControlMessage> controlMessages = new ArrayList<>(); | ||
| 39 | + controlMessages.add(createControlMessage(INBOUND_PACKET)); | ||
| 40 | + controlMessages.add(createControlMessage(OUTBOUND_PACKET)); | ||
| 41 | + return controlMessages; | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + @Override | ||
| 45 | + @Test | ||
| 46 | + public void withoutTime() { | ||
| 47 | + Collection<ControlMessage> cms = createControlMessages(); | ||
| 48 | + long before = System.currentTimeMillis(); | ||
| 49 | + ControlMessageEvent event = | ||
| 50 | + new ControlMessageEvent(ControlMessageEvent.Type.STATS_UPDATE, cms); | ||
| 51 | + long after = System.currentTimeMillis(); | ||
| 52 | + validateEvent(event, ControlMessageEvent.Type.STATS_UPDATE, cms, before, after); | ||
| 53 | + } | ||
| 54 | +} | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
apps/cpman/api/src/test/java/org/onosproject/cpman/message/ControlMessageServiceAdaptor.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 | +package org.onosproject.cpman.message; | ||
| 17 | + | ||
| 18 | +/** | ||
| 19 | + * Test adapter for control message service. | ||
| 20 | + */ | ||
| 21 | +public class ControlMessageServiceAdaptor implements ControlMessageService { | ||
| 22 | + @Override | ||
| 23 | + public void addListener(ControlMessageListener listener) { | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + @Override | ||
| 27 | + public void removeListener(ControlMessageListener listener) { | ||
| 28 | + } | ||
| 29 | +} |
apps/cpman/api/src/test/java/org/onosproject/cpman/message/DefaultControlMessageTest.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 | +package org.onosproject.cpman.message; | ||
| 17 | + | ||
| 18 | +import org.junit.Test; | ||
| 19 | +import org.onosproject.cpman.DefaultControlMessage; | ||
| 20 | + | ||
| 21 | +import static org.hamcrest.Matchers.is; | ||
| 22 | +import static org.junit.Assert.assertThat; | ||
| 23 | +import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutableBaseClass; | ||
| 24 | +import static org.onosproject.cpman.ControlMessage.Type.INBOUND_PACKET; | ||
| 25 | + | ||
| 26 | +/** | ||
| 27 | + * Unit tests for the default control message class. | ||
| 28 | + */ | ||
| 29 | +public class DefaultControlMessageTest { | ||
| 30 | + | ||
| 31 | + /** | ||
| 32 | + * Checks that the DefaultControlMessage class is immutable but can be | ||
| 33 | + * inherited from. | ||
| 34 | + */ | ||
| 35 | + @Test | ||
| 36 | + public void testImmutability() { | ||
| 37 | + assertThatClassIsImmutableBaseClass(DefaultControlMessage.class); | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + /** | ||
| 41 | + * Tests creation of a DefaultControlMessage using a regular constructor. | ||
| 42 | + */ | ||
| 43 | + @Test | ||
| 44 | + public void testBasic() { | ||
| 45 | + final DefaultControlMessage cm = | ||
| 46 | + new DefaultControlMessage(INBOUND_PACKET, 0L, 1L, 2L, 3L); | ||
| 47 | + assertThat(cm.type(), is(INBOUND_PACKET)); | ||
| 48 | + assertThat(cm.load(), is(0L)); | ||
| 49 | + assertThat(cm.rate(), is(1L)); | ||
| 50 | + assertThat(cm.count(), is(2L)); | ||
| 51 | + assertThat(cm.timeStamp(), is(3L)); | ||
| 52 | + } | ||
| 53 | +} |
-
Please register or login to post a comment