Ray Milkey

Unit tests for Instruction and Ethernet codecs

Added tests for codecs for Ethernet and Instruction classes
Also fixed some bugs uncovered by the tests

Change-Id: I29f82d169e81b3fca417f88fab992148bf36dc71
...@@ -94,6 +94,8 @@ public class InstructionsTest { ...@@ -94,6 +94,8 @@ public class InstructionsTest {
94 assertThatClassIsImmutable(L2ModificationInstruction.ModVlanIdInstruction.class); 94 assertThatClassIsImmutable(L2ModificationInstruction.ModVlanIdInstruction.class);
95 assertThatClassIsImmutable(L2ModificationInstruction.ModVlanPcpInstruction.class); 95 assertThatClassIsImmutable(L2ModificationInstruction.ModVlanPcpInstruction.class);
96 assertThatClassIsImmutable(L3ModificationInstruction.ModIPInstruction.class); 96 assertThatClassIsImmutable(L3ModificationInstruction.ModIPInstruction.class);
97 + assertThatClassIsImmutable(L2ModificationInstruction.ModMplsLabelInstruction.class);
98 + assertThatClassIsImmutable(L2ModificationInstruction.PushHeaderInstructions.class);
97 } 99 }
98 100
99 // DropInstruction 101 // DropInstruction
......
...@@ -36,13 +36,23 @@ public class EthernetCodec extends JsonCodec<Ethernet> { ...@@ -36,13 +36,23 @@ public class EthernetCodec extends JsonCodec<Ethernet> {
36 public ObjectNode encode(Ethernet ethernet, CodecContext context) { 36 public ObjectNode encode(Ethernet ethernet, CodecContext context) {
37 checkNotNull(ethernet, "Ethernet cannot be null"); 37 checkNotNull(ethernet, "Ethernet cannot be null");
38 38
39 - return context.mapper().createObjectNode() 39 + final ObjectNode result = context.mapper().createObjectNode()
40 - .put("destinationMacAddress", ethernet.getDestinationMAC().toString())
41 - .put("sourceMacAddress", ethernet.getSourceMAC().toString())
42 - .put("priorityCode", ethernet.getPriorityCode())
43 .put("vlanId", ethernet.getVlanID()) 40 .put("vlanId", ethernet.getVlanID())
44 .put("etherType", ethernet.getEtherType()) 41 .put("etherType", ethernet.getEtherType())
42 + .put("priorityCode", ethernet.getPriorityCode())
45 .put("pad", ethernet.isPad()); 43 .put("pad", ethernet.isPad());
44 +
45 + if (ethernet.getDestinationMAC() != null) {
46 + result.put("destMac",
47 + ethernet.getDestinationMAC().toString());
48 + }
49 +
50 + if (ethernet.getSourceMAC() != null) {
51 + result.put("srcMac",
52 + ethernet.getSourceMAC().toString());
53 + }
54 +
55 + return result;
46 } 56 }
47 57
48 } 58 }
......
...@@ -147,7 +147,7 @@ public class InstructionCodec extends JsonCodec<Instruction> { ...@@ -147,7 +147,7 @@ public class InstructionCodec extends JsonCodec<Instruction> {
147 case OUTPUT: 147 case OUTPUT:
148 final Instructions.OutputInstruction outputInstruction = 148 final Instructions.OutputInstruction outputInstruction =
149 (Instructions.OutputInstruction) instruction; 149 (Instructions.OutputInstruction) instruction;
150 - result.put("portNumber", outputInstruction.port().toLong()); 150 + result.put("port", outputInstruction.port().toLong());
151 break; 151 break;
152 152
153 case DROP: 153 case DROP:
......
1 +/*
2 + * Copyright 2015 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.codec.impl;
17 +
18 +import org.junit.Test;
19 +import org.onlab.packet.Ethernet;
20 +import org.onosproject.codec.CodecContext;
21 +import org.onosproject.codec.JsonCodec;
22 +
23 +import com.fasterxml.jackson.databind.node.ObjectNode;
24 +
25 +import static org.hamcrest.MatcherAssert.assertThat;
26 +import static org.hamcrest.Matchers.notNullValue;
27 +import static org.onosproject.codec.impl.EthernetJsonMatcher.matchesEthernet;
28 +
29 +/**
30 + * Unit test for Ethernet class codec.
31 + */
32 +public class EthernetCodecTest {
33 +
34 + /**
35 + * Unit test for the ethernet object codec.
36 + */
37 + @Test
38 + public void ethernetCodecTest() {
39 + final CodecContext context = new MockCodecContext();
40 + final JsonCodec<Ethernet> ethernetCodec = context.codec(Ethernet.class);
41 + assertThat(ethernetCodec, notNullValue());
42 +
43 + final Ethernet eth1 = new Ethernet();
44 + eth1.setSourceMACAddress("11:22:33:44:55:01");
45 + eth1.setDestinationMACAddress("11:22:33:44:55:02");
46 + eth1.setPad(true);
47 + eth1.setEtherType(Ethernet.TYPE_ARP);
48 + eth1.setPriorityCode((byte) 7);
49 + eth1.setVlanID((short) 33);
50 +
51 + final ObjectNode eth1Json = ethernetCodec.encode(eth1, context);
52 + assertThat(eth1Json, notNullValue());
53 + assertThat(eth1Json, matchesEthernet(eth1));
54 + }
55 +}
1 +/*
2 + * Copyright 2015 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.codec.impl;
17 +
18 +import org.hamcrest.Description;
19 +import org.hamcrest.TypeSafeMatcher;
20 +import org.onlab.packet.Ethernet;
21 +
22 +import com.fasterxml.jackson.databind.JsonNode;
23 +
24 +/**
25 + * Hamcrest matcher for ethernet objects.
26 + */
27 +public class EthernetJsonMatcher extends TypeSafeMatcher<JsonNode> {
28 +
29 + private final Ethernet ethernet;
30 + private String reason = "";
31 +
32 + public EthernetJsonMatcher(Ethernet ethernetValue) {
33 + ethernet = ethernetValue;
34 + }
35 +
36 + @Override
37 + public boolean matchesSafely(JsonNode jsonEthernet) {
38 +
39 + // check source MAC
40 + final JsonNode jsonSourceMacNode = jsonEthernet.get("srcMac");
41 + if (ethernet.getSourceMAC() != null) {
42 + final String jsonSourceMac = jsonSourceMacNode.textValue();
43 + final String sourceMac = ethernet.getSourceMAC().toString();
44 + if (!jsonSourceMac.equals(sourceMac)) {
45 + reason = "source MAC " + ethernet.getSourceMAC().toString();
46 + return false;
47 + }
48 + } else {
49 + // source MAC not specified, JSON representation must be empty
50 + if (jsonSourceMacNode != null) {
51 + reason = "source mac should be null ";
52 + return false;
53 + }
54 + }
55 +
56 + // check destination MAC
57 + final JsonNode jsonDestinationMacNode = jsonEthernet.get("destMac");
58 + if (ethernet.getDestinationMAC() != null) {
59 + final String jsonDestinationMac = jsonDestinationMacNode.textValue();
60 + final String destinationMac = ethernet.getDestinationMAC().toString();
61 + if (!jsonDestinationMac.equals(destinationMac)) {
62 + reason = "destination MAC " + ethernet.getDestinationMAC().toString();
63 + return false;
64 + }
65 + } else {
66 + // destination MAC not specified, JSON representation must be empty
67 + if (jsonDestinationMacNode != null) {
68 + reason = "destination mac should be null ";
69 + return false;
70 + }
71 + }
72 +
73 + // check priority code
74 + final short jsonPriorityCode = jsonEthernet.get("priorityCode").shortValue();
75 + final short priorityCode = ethernet.getPriorityCode();
76 + if (jsonPriorityCode != priorityCode) {
77 + reason = "priority code " + Short.toString(ethernet.getPriorityCode());
78 + return false;
79 + }
80 +
81 + // check vlanId
82 + final short jsonVlanId = jsonEthernet.get("vlanId").shortValue();
83 + final short vlanId = ethernet.getVlanID();
84 + if (jsonVlanId != vlanId) {
85 + reason = "vlan id " + Short.toString(ethernet.getVlanID());
86 + return false;
87 + }
88 +
89 + // check etherType
90 + final short jsonEtherType = jsonEthernet.get("etherType").shortValue();
91 + final short etherType = ethernet.getEtherType();
92 + if (jsonEtherType != etherType) {
93 + reason = "etherType " + Short.toString(ethernet.getEtherType());
94 + return false;
95 + }
96 +
97 + // check pad
98 + final boolean jsonPad = jsonEthernet.get("pad").asBoolean();
99 + final boolean pad = ethernet.isPad();
100 + if (jsonPad != pad) {
101 + reason = "pad " + Boolean.toString(ethernet.isPad());
102 + return false;
103 + }
104 +
105 + return true;
106 + }
107 +
108 + @Override
109 + public void describeTo(Description description) {
110 + description.appendText(reason);
111 + }
112 +
113 + /**
114 + * Factory to allocate a ethernet matcher.
115 + *
116 + * @param ethernet ethernet object we are looking for
117 + * @return matcher
118 + */
119 + public static EthernetJsonMatcher matchesEthernet(Ethernet ethernet) {
120 + return new EthernetJsonMatcher(ethernet);
121 + }
122 +}
1 +/*
2 + * Copyright 2015 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.codec.impl;
17 +
18 +import org.junit.Before;
19 +import org.junit.Test;
20 +import org.onlab.packet.IpAddress;
21 +import org.onlab.packet.IpPrefix;
22 +import org.onlab.packet.MacAddress;
23 +import org.onlab.packet.VlanId;
24 +import org.onosproject.codec.CodecContext;
25 +import org.onosproject.codec.JsonCodec;
26 +import org.onosproject.net.PortNumber;
27 +import org.onosproject.net.flow.instructions.Instruction;
28 +import org.onosproject.net.flow.instructions.Instructions;
29 +import org.onosproject.net.flow.instructions.L0ModificationInstruction;
30 +import org.onosproject.net.flow.instructions.L2ModificationInstruction;
31 +import org.onosproject.net.flow.instructions.L3ModificationInstruction;
32 +
33 +import com.fasterxml.jackson.databind.node.ObjectNode;
34 +
35 +import static org.hamcrest.MatcherAssert.assertThat;
36 +import static org.hamcrest.Matchers.notNullValue;
37 +import static org.onosproject.codec.impl.InstructionJsonMatcher.matchesInstruction;
38 +
39 +/**
40 + * Unit tests for Instruction codec.
41 + */
42 +public class InstructionCodecTest {
43 + CodecContext context;
44 + JsonCodec<Instruction> instructionCodec;
45 +
46 + /**
47 + * Sets up for each tests. Creates a context and fetches the instruction
48 + * codec.
49 + */
50 + @Before
51 + public void setUp() {
52 + context = new MockCodecContext();
53 + instructionCodec = context.codec(Instruction.class);
54 + assertThat(instructionCodec, notNullValue());
55 + }
56 +
57 + /**
58 + * Tests the encoding of push header instructions.
59 + */
60 + @Test
61 + public void pushHeaderInstructionsTest() {
62 + final L2ModificationInstruction.PushHeaderInstructions instruction =
63 + (L2ModificationInstruction.PushHeaderInstructions) Instructions.pushMpls();
64 + final ObjectNode instructionJson = instructionCodec.encode(instruction, context);
65 +
66 + assertThat(instructionJson, matchesInstruction(instruction));
67 + }
68 +
69 + /**
70 + * Tests the encoding of drop instructions.
71 + */
72 + @Test
73 + public void dropInstructionTest() {
74 + final Instructions.DropInstruction instruction =
75 + new Instructions.DropInstruction();
76 + final ObjectNode instructionJson =
77 + instructionCodec.encode(instruction, context);
78 + assertThat(instructionJson, matchesInstruction(instruction));
79 + }
80 +
81 + /**
82 + * Tests the encoding of output instructions.
83 + */
84 + @Test
85 + public void outputInstructionTest() {
86 + final Instructions.OutputInstruction instruction =
87 + Instructions.createOutput(PortNumber.portNumber(22));
88 + final ObjectNode instructionJson =
89 + instructionCodec.encode(instruction, context);
90 + assertThat(instructionJson, matchesInstruction(instruction));
91 + }
92 +
93 + /**
94 + * Tests the encoding of mod lambda instructions.
95 + */
96 + @Test
97 + public void modLambdaInstructionTest() {
98 + final L0ModificationInstruction.ModLambdaInstruction instruction =
99 + (L0ModificationInstruction.ModLambdaInstruction)
100 + Instructions.modL0Lambda((short) 55);
101 + final ObjectNode instructionJson =
102 + instructionCodec.encode(instruction, context);
103 + assertThat(instructionJson, matchesInstruction(instruction));
104 + }
105 +
106 + /**
107 + * Tests the encoding of mod ether instructions.
108 + */
109 + @Test
110 + public void modEtherInstructionTest() {
111 + final L2ModificationInstruction.ModEtherInstruction instruction =
112 + (L2ModificationInstruction.ModEtherInstruction)
113 + Instructions.modL2Src(MacAddress.valueOf("11:22:33:44:55:66"));
114 + final ObjectNode instructionJson =
115 + instructionCodec.encode(instruction, context);
116 + assertThat(instructionJson, matchesInstruction(instruction));
117 + }
118 +
119 + /**
120 + * Tests the encoding of mod vlan id instructions.
121 + */
122 + @Test
123 + public void modVlanIdInstructionTest() {
124 + final L2ModificationInstruction.ModVlanIdInstruction instruction =
125 + (L2ModificationInstruction.ModVlanIdInstruction)
126 + Instructions.modVlanId(VlanId.vlanId((short) 12));
127 +
128 + final ObjectNode instructionJson =
129 + instructionCodec.encode(instruction, context);
130 + assertThat(instructionJson, matchesInstruction(instruction));
131 + }
132 +
133 + /**
134 + * Tests the encoding of mod vlan pcp instructions.
135 + */
136 + @Test
137 + public void modVlanPcpInstructionTest() {
138 + final L2ModificationInstruction.ModVlanPcpInstruction instruction =
139 + (L2ModificationInstruction.ModVlanPcpInstruction)
140 + Instructions.modVlanPcp((byte) 9);
141 + final ObjectNode instructionJson =
142 + instructionCodec.encode(instruction, context);
143 + assertThat(instructionJson, matchesInstruction(instruction));
144 + }
145 +
146 + /**
147 + * Tests the encoding of mod ip instructions.
148 + */
149 + @Test
150 + public void modIPInstructionTest() {
151 + final IpAddress ip = IpPrefix.valueOf("1.2.3.4/24").address();
152 + final L3ModificationInstruction.ModIPInstruction instruction =
153 + (L3ModificationInstruction.ModIPInstruction)
154 + Instructions.modL3Src(ip);
155 + final ObjectNode instructionJson =
156 + instructionCodec.encode(instruction, context);
157 + assertThat(instructionJson, matchesInstruction(instruction));
158 + }
159 +
160 + /**
161 + * Tests the encoding of mod MPLS label instructions.
162 + */
163 + @Test
164 + public void modMplsLabelInstructionTest() {
165 + final L2ModificationInstruction.ModMplsLabelInstruction instruction =
166 + (L2ModificationInstruction.ModMplsLabelInstruction)
167 + Instructions.modMplsLabel(99);
168 + final ObjectNode instructionJson =
169 + instructionCodec.encode(instruction, context);
170 + assertThat(instructionJson, matchesInstruction(instruction));
171 + }
172 +
173 +}
1 +/*
2 + * Copyright 2015 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.codec.impl;
17 +
18 +import org.onosproject.codec.CodecContext;
19 +import org.onosproject.codec.JsonCodec;
20 +
21 +import com.fasterxml.jackson.databind.ObjectMapper;
22 +
23 +/**
24 + * Mock codec context for use in codec unit tests.
25 + */
26 +public final class MockCodecContext implements CodecContext {
27 +
28 + private ObjectMapper mapper = new ObjectMapper();
29 + private CodecManager manager = new CodecManager();
30 +
31 + public MockCodecContext() {
32 + manager.activate();
33 + }
34 +
35 + @Override
36 + public ObjectMapper mapper() {
37 + return mapper;
38 + }
39 +
40 + @Override
41 + @SuppressWarnings("unchecked")
42 + public <T> JsonCodec<T> codec(Class<T> entityClass) {
43 + return manager.getCodec(entityClass);
44 + }
45 +
46 + @Override
47 + public <T> T get(Class<T> serviceClass) {
48 + return null;
49 + }
50 +
51 +}