checkstyle: TypeName
[neutron.git] / neutron-spi / src / test / java / org / opendaylight / neutron / spi / NeutronPortSecurityJAXBTest.java
1 /*
2  * Copyright (c) 2016 Intel Corporation  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.neutron.spi;
10
11 import java.util.List;
12 import java.util.Map;
13 import javax.xml.bind.JAXBException;
14 import org.junit.Assert;
15 import org.junit.Test;
16
17 public class NeutronPortSecurityJAXBTest {
18
19     // JSON with PortSecurityEnabled set true
20     private static final String PORT_SECURITY_ENABLED_SOURCE_JSON = "{ " + "\"status\": \"ACTIVE\", "
21             + "\"name\": \"net1\", " + "\"admin_state_up\": true, "
22             + "\"tenant_id\": \"9bacb3c5d39d41a79512987f338cf177\", "
23             + "\"fixed_ips\": [ { \"ip_address\":\"192.168.111.3\" , "
24             + "\"subnet_id\": \"22b44fc2-4ffb-4de4-b0f9-69d58b37ae27\" } ],"
25             + "\"binding:vif_details\": {\"port_filter\": \"true\" , \"ovs_hybrid_plug\": \"false\" }, "
26             + "\"extra_dhcp_opts\": [\"\"], " + "\"security_groups\": [\"\"], " + "\"allowed_address_pairs\": [\"\"], "
27             + "\"device_id\": \"257614cc-e178-4c92-9c61-3b28d40eca44\", " + "\"device_owner\": \"\", "
28             + "\"binding:host_id\": \"\", " + "\"binding:vif_type\": \"unbound\", "
29             + "\"binding:vnic_type\": \"normal\", " + "\"mac_address\": \"fa:16:3e:c9:cb:f0\", "
30             + "\"network_id\": \"e9330b1f-a2ef-4160-a991-169e56ab17f5\", "
31             + "\"id\": \"4e8e5957-649f-477b-9e5b-f1f75b21c03c\", " + "\"port_security_enabled\": true }";
32
33     // JSON with PortSecurityEnabled set false
34     private static final String PORT_SECURITY_DISABLED_SOURCE_JSON = "{ " + "\"status\": \"ACTIVE\", "
35             + "\"name\": \"net1\", " + "\"admin_state_up\": true, "
36             + "\"tenant_id\": \"9bacb3c5d39d41a79512987f338cf177\", "
37             + "\"fixed_ips\": [ { \"ip_address\":\"192.168.111.3\" , "
38             + "\"subnet_id\": \"22b44fc2-4ffb-4de4-b0f9-69d58b37ae27\" } ],"
39             + "\"binding:vif_details\": {\"port_filter\": \"true\" , \"ovs_hybrid_plug\": \"false\" }, "
40             + "\"extra_dhcp_opts\": [\"\"], " + "\"security_groups\": [\"\"], " + "\"allowed_address_pairs\": [\"\"], "
41             + "\"device_id\": \"257614cc-e178-4c92-9c61-3b28d40eca44\", " + "\"device_owner\": \"\", "
42             + "\"binding:host_id\": \"\", " + "\"binding:vif_type\": \"unbound\", "
43             + "\"binding:vnic_type\": \"normal\", " + "\"mac_address\": \"fa:16:3e:c9:cb:f0\", "
44             + "\"network_id\": \"e9330b1f-a2ef-4160-a991-169e56ab17f5\", "
45             + "\"id\": \"4e8e5957-649f-477b-9e5b-f1f75b21c03c\", " + "\"port_security_enabled\": false }";
46
47     // JSON with PortSecurityEnabled not set for compatibility test
48     private static final String PORT_SECURITY_DEFAULT_SOURCE_JSON = "{ " + "\"status\": \"ACTIVE\", "
49             + "\"name\": \"net1\", " + "\"admin_state_up\": true, "
50             + "\"tenant_id\": \"9bacb3c5d39d41a79512987f338cf177\", "
51             + "\"fixed_ips\": [ { \"ip_address\":\"192.168.111.3\" , "
52             + "\"subnet_id\": \"22b44fc2-4ffb-4de4-b0f9-69d58b37ae27\" } ],"
53             + "\"binding:vif_details\": {\"port_filter\": \"true\" , \"ovs_hybrid_plug\": \"false\" }, "
54             + "\"extra_dhcp_opts\": [\"\"], " + "\"security_groups\": [\"\"], " + "\"allowed_address_pairs\": [\"\"], "
55             + "\"device_id\": \"257614cc-e178-4c92-9c61-3b28d40eca44\", " + "\"device_owner\": \"\", "
56             + "\"binding:host_id\": \"\", " + "\"binding:vif_type\": \"unbound\", "
57             + "\"binding:vnic_type\": \"normal\", " + "\"mac_address\": \"fa:16:3e:c9:cb:f0\", "
58             + "\"network_id\": \"e9330b1f-a2ef-4160-a991-169e56ab17f5\", "
59             + "\"id\": \"4e8e5957-649f-477b-9e5b-f1f75b21c03c\" }";
60
61     @Test
62     public void test_NeutronPortSecurityEnabled_JAXB() throws JAXBException {
63         test_PortSecurityEnabled_JAXB(PORT_SECURITY_ENABLED_SOURCE_JSON, true);
64     }
65
66     @Test
67     public void test_NeutronPortSecurityDisabled_JAXB() throws JAXBException {
68         test_PortSecurityEnabled_JAXB(PORT_SECURITY_DISABLED_SOURCE_JSON, false);
69     }
70
71     /*
72         By Default PortSecurity is enabled for backward compatibility.
73         This test checks the case when the parameter is not available.
74      */
75     @Test
76     public void test_NeutronPortSecurityDefault_JAXB() throws JAXBException {
77         test_PortSecurityEnabled_JAXB(PORT_SECURITY_DEFAULT_SOURCE_JSON, true);
78     }
79
80     private void test_PortSecurityEnabled_JAXB(String sourceJson, Boolean portSecurityEnabled) throws JAXBException {
81         NeutronPort testObject = new NeutronPort();
82
83         NeutronPort neutronObject = (NeutronPort) JaxbTestHelper.jaxbUnmarshall(testObject, sourceJson);
84         Assert.assertEquals("NeutronPort JAXB Test 1: Testing id failed", "4e8e5957-649f-477b-9e5b-f1f75b21c03c",
85                 neutronObject.getID());
86
87         Assert.assertEquals("NeutronPort JAXB Test 2: Testing tenant_id failed", "9bacb3c5d39d41a79512987f338cf177",
88                 neutronObject.getTenantID());
89
90         Assert.assertEquals("NeutronPort JAXB Test 3: Testing network_id failed",
91                 "e9330b1f-a2ef-4160-a991-169e56ab17f5", neutronObject.getNetworkUUID());
92
93         Assert.assertEquals("NeutronPort JAXB Test 4: Testing status failed", "ACTIVE", neutronObject.getStatus());
94
95         List<NeutronIPs> fixedIPs = neutronObject.getFixedIPs();
96         Assert.assertEquals(" NeutronPort JAXB Test 5.1: Testing fixed_ips list length failed", 1, fixedIPs.size());
97
98         Assert.assertEquals(" NeutronPort JAXB Test 5.2: Testing ip_address value failed", "192.168.111.3",
99                 fixedIPs.get(0).ipAddress);
100
101         Assert.assertEquals(" NeutronPort JAXB Test 5.3: Testing subnet_id value failed",
102                 "22b44fc2-4ffb-4de4-b0f9-69d58b37ae27", fixedIPs.get(0).subnetUUID);
103
104         List<NeutronPortExtraDHCPOption> extraDHCPOptions = neutronObject.getExtraDHCPOptions();
105
106         Assert.assertEquals("NeutronPort JAXB Test 6: Testing extra_dhcp_opts list length failed", 1,
107                 extraDHCPOptions.size());
108
109         List<NeutronSecurityGroup> securityGroups = neutronObject.getSecurityGroups();
110         Assert.assertEquals("NeutronPort JAXB Test 7: Testing security_groups list length failed", 1,
111                 securityGroups.size());
112
113         List<NeutronPortAllowedAddressPairs> allowedAddressPairs = neutronObject.getAllowedAddressPairs();
114         Assert.assertEquals("NeutronPort JAXB Test 8: Testing allowed_address_pairs list length failed", 1,
115                 allowedAddressPairs.size());
116
117         Map<String, String> vifDetails = neutronObject.getVIFDetails();
118
119         Assert.assertEquals("NeutronPort JAXB Test 9.1: Testing vif_details list length failed", 2,
120                 vifDetails.size());
121
122         Assert.assertEquals("NeutronPort JAXB Test 9.2: Testing port_filter value failed", "true",
123                 vifDetails.get("port_filter"));
124
125         Assert.assertEquals("NeutronPort JAXB Test 9.3: Testing ovs_hybrid_plug value failed", "false",
126                 vifDetails.get("ovs_hybrid_plug"));
127
128         Assert.assertEquals("NeutronPort JAXB Test 10: Testing name failed", "net1", neutronObject.getName());
129
130         Assert.assertEquals("NeutronPort JAXB Test 11: Testing admin_state_up failed", true,
131                 neutronObject.getAdminStateUp());
132
133         Assert.assertEquals("NeutronPort JAXB Test 12: Testing binding:vif_type failed", "unbound",
134                 neutronObject.getBindingvifType());
135
136         Assert.assertEquals("NeutronPort JAXB Test 13: Testing binding:vnic_type failed", "normal",
137                 neutronObject.getBindingvnicType());
138
139         Assert.assertEquals("NeutronPort JAXB Test 14: Testing mac_address failed", "fa:16:3e:c9:cb:f0",
140                 neutronObject.getMacAddress());
141
142         Assert.assertEquals("NeutronPort JAXB Test 15: Testing device_id failed",
143                 "257614cc-e178-4c92-9c61-3b28d40eca44", neutronObject.getDeviceID());
144
145         Assert.assertEquals("NeutronPort JAXB Test 16: Testing device_owner failed", "",
146                 neutronObject.getDeviceOwner());
147
148         Assert.assertEquals("NeutronPort JAXB Test 17: Testing binding:host_id failed", "",
149                 neutronObject.getBindinghostID());
150
151         Assert.assertEquals("NeutronPort JAXB Test 18: Testing port_security_enabled failed", portSecurityEnabled,
152                 neutronObject.getPortSecurityEnabled());
153     }
154 }