blob: 2383116c922d0c19187a1bf75cf3f1b6354b9f7a [file] [log] [blame]
Jian Li44155b02017-02-15 17:03:38 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jian Li44155b02017-02-15 17:03:38 +09003 *
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 */
16package org.onosproject.mapping.actions;
17
18import org.junit.Test;
19
20import static org.hamcrest.MatcherAssert.assertThat;
21import static org.hamcrest.Matchers.equalTo;
22import static org.hamcrest.Matchers.instanceOf;
23import static org.hamcrest.Matchers.is;
24import static org.hamcrest.Matchers.notNullValue;
25import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
26import static org.onlab.junit.UtilityClassChecker.assertThatClassIsUtility;
27
28/**
29 * Unit tests for various mapping action implementation classes.
30 */
31public class MappingActionsTest {
32
33 /**
34 * Checks that a MappingAction object has the proper type, and then converts
35 * it to the proper type.
36 *
37 * @param action MappingAction object to convert
38 * @param type Enumerated type value for the MappingAction class
39 * @param clazz Desired MappingAction class
40 * @param <T> The type the caller wants returned
41 * @return converted object
42 */
43 @SuppressWarnings("unchecked")
44 private <T> T checkAndConvert(MappingAction action, MappingAction.Type type, Class clazz) {
45 assertThat(action, is(notNullValue()));
46 assertThat(action.type(), is(equalTo(type)));
47 assertThat(action, instanceOf(clazz));
48 return (T) action;
49 }
50
51 /**
52 * Checks that the MappingActions class is a valid utility class.
53 */
54 @Test
55 public void testMappingActionsUtility() {
56 assertThatClassIsUtility(MappingActions.class);
57 }
58
59 /**
60 * Checks that the mapping action implementations are immutable.
61 */
62 @Test
63 public void testMappingActionsImmutability() {
64 assertThatClassIsImmutable(NoMappingAction.class);
65 assertThatClassIsImmutable(ForwardMappingAction.class);
66 assertThatClassIsImmutable(NativeForwardMappingAction.class);
67 assertThatClassIsImmutable(DropMappingAction.class);
68 }
69
70 /**
71 * Tests the noAction method.
72 */
73 @Test
74 public void testNoActionMethod() {
75 MappingAction mappingAction = MappingActions.noAction();
76 checkAndConvert(mappingAction,
77 MappingAction.Type.NO_ACTION,
78 NoMappingAction.class);
79 }
80
81 /**
82 * Tests the forward method.
83 */
84 @Test
85 public void testForwardMethod() {
86 MappingAction mappingAction = MappingActions.forward();
87 checkAndConvert(mappingAction,
88 MappingAction.Type.FORWARD,
89 ForwardMappingAction.class);
90 }
91
92 /**
93 * Tests the native forward method.
94 */
95 @Test
96 public void testNativeForwardMethod() {
97 MappingAction mappingAction = MappingActions.nativeForward();
98 checkAndConvert(mappingAction,
99 MappingAction.Type.NATIVE_FORWARD,
100 NativeForwardMappingAction.class);
101 }
102
103 /**
104 * Tests the drop method.
105 */
106 @Test
107 public void testDropMethod() {
108 MappingAction mappingAction = MappingActions.drop();
109 checkAndConvert(mappingAction,
110 MappingAction.Type.DROP,
111 DropMappingAction.class);
112 }
113}