blob: 0246227a181d00b1ed69863d85a0725f630d686b [file] [log] [blame]
Frank Wange33e4ed2017-06-28 10:01:07 +08001/*
2 * Copyright 2017-present 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
17package org.onosproject.net.pi.runtime;
18
19import com.google.common.collect.Maps;
20import com.google.common.testing.EqualsTester;
21import org.apache.commons.collections.CollectionUtils;
22import org.junit.Test;
23import org.onlab.util.ImmutableByteSequence;
24
25import java.util.Map;
26
27import static org.hamcrest.MatcherAssert.assertThat;
28import static org.hamcrest.Matchers.is;
29import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
30import static org.onosproject.net.pi.runtime.PiConstantsTest.DROP;
31import static org.onosproject.net.pi.runtime.PiConstantsTest.DST_ADDR;
32import static org.onosproject.net.pi.runtime.PiConstantsTest.IPV4_HEADER_NAME;
33
34/**
35 * Unit tests for PiTableEntry class.
36 */
37public class PiTableEntryTest {
38 final PiTableEntry piTableEntry1 = PiTableEntry.builder()
39 .forTable(PiTableId.of("Table10"))
40 .withCookie(0xac)
41 .withPriority(10)
42 .withAction(PiAction.builder().withId(PiActionId.of(DROP)).build())
43 .withTimeout(100)
44 .build();
45 final PiTableEntry sameAsPiTableEntry1 = PiTableEntry.builder()
46 .forTable(PiTableId.of("Table10"))
47 .withCookie(0xac)
48 .withPriority(10)
49 .withAction(PiAction.builder().withId(PiActionId.of(DROP)).build())
50 .withTimeout(100)
51 .build();
52 final PiTableEntry piTableEntry2 = PiTableEntry.builder()
53 .forTable(PiTableId.of("Table20"))
54 .withCookie(0xac)
55 .withPriority(10)
56 .withAction(PiAction.builder().withId(PiActionId.of(DROP)).build())
57 .withTimeout(1000)
58 .build();
59
60 /**
61 * Checks that the PiTableEntry class is immutable.
62 */
63 @Test
64 public void testImmutability() {
65 assertThatClassIsImmutable(PiTableEntry.class);
66 }
67
68 /**
69 * Tests the equals, hashCode and toString methods using Guava EqualsTester.
70 */
71 @Test
72 public void testEquals() {
73 new EqualsTester()
74 .addEqualityGroup(piTableEntry1, sameAsPiTableEntry1)
75 .addEqualityGroup(piTableEntry2)
76 .testEquals();
77 }
78
79 /**
80 * Tests creation of a DefaultFlowRule using a FlowRule constructor.
81 */
82 @Test
83 public void testBuilder() {
84
85 PiTableId piTableId = PiTableId.of("table10");
86 long cookie = 0xfff0323;
87 int priority = 100;
88 double timeout = 1000;
89 PiHeaderFieldId piHeaderFieldId = PiHeaderFieldId.of(IPV4_HEADER_NAME, DST_ADDR);
90 PiFieldMatch piFieldMatch = new PiExactFieldMatch(piHeaderFieldId, ImmutableByteSequence.copyFrom(0x0a010101));
91 PiAction piAction = PiAction.builder().withId(PiActionId.of(DROP)).build();
92 final Map<PiHeaderFieldId, PiFieldMatch> fieldMatches = Maps.newHashMap();
93 fieldMatches.put(piHeaderFieldId, piFieldMatch);
94 final PiTableEntry piTableEntry = PiTableEntry.builder()
95 .forTable(piTableId)
96 .withFieldMatches(fieldMatches.values())
97 .withAction(piAction)
98 .withCookie(cookie)
99 .withPriority(priority)
100 .withTimeout(timeout)
101 .build();
102
103 assertThat(piTableEntry.table(), is(piTableId));
104 assertThat(piTableEntry.cookie(), is(cookie));
105 assertThat(piTableEntry.priority().get(), is(priority));
106 assertThat(piTableEntry.timeout().get(), is(timeout));
107 assertThat("Incorrect match param value",
108 CollectionUtils.isEqualCollection(piTableEntry.fieldMatches(), fieldMatches.values()));
109 assertThat(piTableEntry.action(), is(piAction));
110 }
111}