diff options
Diffstat (limited to 'quantum-model/src/test/java/com/woorea/openstack/quantum/model/PortTest.java')
-rw-r--r-- | quantum-model/src/test/java/com/woorea/openstack/quantum/model/PortTest.java | 236 |
1 files changed, 98 insertions, 138 deletions
diff --git a/quantum-model/src/test/java/com/woorea/openstack/quantum/model/PortTest.java b/quantum-model/src/test/java/com/woorea/openstack/quantum/model/PortTest.java index 7baa8d9..756753d 100644 --- a/quantum-model/src/test/java/com/woorea/openstack/quantum/model/PortTest.java +++ b/quantum-model/src/test/java/com/woorea/openstack/quantum/model/PortTest.java @@ -2,8 +2,8 @@ * ============LICENSE_START======================================================= * ONAP - SO * ================================================================================ - * Copyright (C) 2018 Huawei Intellectual Property. All rights reserved. - * ================================================================================ + * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -20,156 +20,116 @@ package com.woorea.openstack.quantum.model; -import static org.hamcrest.CoreMatchers.containsString; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.hasItem; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.not; -import static org.junit.Assert.assertThat; - -import java.util.ArrayList; -import java.util.Arrays; +import com.woorea.openstack.quantum.model.Port; +import com.woorea.openstack.quantum.model.Port.Binding; +import com.woorea.openstack.quantum.model.Port.Ip; import java.util.List; - import org.codehaus.jackson.map.DeserializationConfig; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.SerializationConfig; import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion; -import org.hamcrest.CustomMatcher; -import org.junit.Before; +import org.junit.Assert; import org.junit.Test; - -import com.woorea.openstack.quantum.model.Port.Ip; +import org.skyscreamer.jsonassert.JSONAssert; +import org.skyscreamer.jsonassert.JSONCompareMode; public class PortTest { - private static final String TENANT_ID = "tenantId"; - - private static final String STATUS = "status"; - - private static final String NETWORK_ID = "networkId"; - - private static final String NAME = "name"; - - private static final String MAC_ADDRESS = "macAddress"; - - private static final String DEVICE_OWNER = "deviceOwner"; - - private static final String DEVICE_ID = "deviceId"; - - private static final String IP_SUBNET_ID = "ipSubnetId"; - - private static final String IP_ADDRESS = "10.0.0.1"; - - private static final boolean ADMIN_STATE_UP = true; - - private static final String SEC_GROUP = "secGroup"; - - private static final String ID = "testId"; - - /** - * JSON with read only attributes. - */ - private static final String PORT_JSON = "{" - + " \"port\" : {" - + " \"id\" : \"" + ID + "\"," - + " \"status\" : \"" + STATUS + "\"" - + " }" - + "}"; - - private ObjectMapper objectMapper; - - private String serializedPort; - - @Before - public void setUp() throws Exception { - objectMapper = initializeObjectMapper(); - } - - public static ObjectMapper initializeObjectMapper() { - ObjectMapper objectMapper = new ObjectMapper(); - objectMapper.setSerializationInclusion(Inclusion.NON_NULL) - .enable(SerializationConfig.Feature.INDENT_OUTPUT) - .enable(SerializationConfig.Feature.WRAP_ROOT_VALUE) - .enable(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE) - .enable(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY); - return objectMapper; - } + private static final String EOL = System.lineSeparator(); + + private static final String JSON_FULL = "{" + EOL + + " \"port\" : {" + EOL + + " \"name\" : \"name\"," + EOL + + " \"binding:host_id\" : \"hostid\"," + EOL + + " \"binding:vif_type\" : \"viftype\"," + EOL + + " \"binding:vnic_type\" : \"vnictype\"," + EOL + + " \"binding:vif_details\" : {" + EOL + + " \"vifdetails-k1\" : \"vifdetails-v1\"," + EOL + + " \"vifdetails-k2\" : \"vifdetails-v2\"" + EOL + + " }," + EOL + + " \"binding:profile\" : {" + EOL + + " \"profile-k1\" : \"profile-v1\"," + EOL + + " \"profile-k2\" : \"profile-v2\"" + EOL + + " }," + EOL + + " \"admin_state_up\" : false," + EOL + + " \"device_id\" : \"deviceid\"," + EOL + + " \"device_owner\" : \"deviceowner\"," + EOL + + " \"fixed_ips\" : [ {" + EOL + + " \"ip_address\" : \"address\"," + EOL + + " \"subnet_id\" : \"subnetid\"" + EOL + + " }, {" + EOL + + " \"ip_address\" : \"address\"," + EOL + + " \"subnet_id\" : \"subnetid\"" + EOL + + " } ]," + EOL + + " \"mac_address\" : \"macaddress\"," + EOL + + " \"network_id\" : \"networkid\"," + EOL + + " \"tenant_id\" : \"tenantid\"," + EOL + + " \"security_groups\" : [ \"securitygroups-v1\", \"securitygroups-v2\" ]" + EOL + + " }" + EOL + + "}"; + + private ObjectMapper objectMapper = new ObjectMapper() + .setSerializationInclusion(Inclusion.NON_NULL) + .enable(SerializationConfig.Feature.INDENT_OUTPUT) + .enable(SerializationConfig.Feature.WRAP_ROOT_VALUE) + .enable(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE) + .enable(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY); @Test public void testSerialization() throws Exception { - Port port = new Port(); - port.setId(ID); - port.setAdminStateUp(ADMIN_STATE_UP); - port.setSecurityGroups(Arrays.asList(SEC_GROUP)); - port.setDeviceId(DEVICE_ID); - port.setDeviceOwner(DEVICE_OWNER); - port.setMacAddress(MAC_ADDRESS); - port.setName(NAME); - port.setNetworkId(NETWORK_ID); - port.setStatus(STATUS); - port.setTenantId(TENANT_ID); - - List<Ip> ips = new ArrayList<Port.Ip>(); - Ip ip = new Ip(); - ip.setAddress(IP_ADDRESS); - ip.setSubnetId(IP_SUBNET_ID); - ips.add(ip); - port.setList(ips); - - serializedPort = objectMapper.writeValueAsString(port); - assertThat(serializedPort, not(containsString(ID))); - assertThat(serializedPort, not(containsString(STATUS))); - assertThat(serializedPort, containsString("\"admin_state_up\" : " + ADMIN_STATE_UP)); - assertThat(serializedPort, containsString(SEC_GROUP)); - assertThat(serializedPort, containsString(IP_ADDRESS)); - assertThat(serializedPort, containsString(DEVICE_ID)); - assertThat(serializedPort, containsString(DEVICE_OWNER)); - assertThat(serializedPort, containsString(MAC_ADDRESS)); - assertThat(serializedPort, containsString(NAME)); - assertThat(serializedPort, containsString(NETWORK_ID)); - assertThat(serializedPort, containsString(TENANT_ID)); - } - - @Test - public void testSerializationEmpty() throws Exception { - Port port = new Port(); - serializedPort = objectMapper.writeValueAsString(port); - - assertThat(serializedPort, containsString("\"port\" : { }")); + System.out.println("CLASS: " + Port.class.getName()); + System.out.println("TEST JSON: " + JSON_FULL); + Port port = objectMapper.readValue(JSON_FULL, Port.class); + String json = objectMapper.writeValueAsString(port); + System.out.println("RE-SERIALIZED OBJECT: " + json); + JSONAssert.assertEquals(JSON_FULL, json, JSONCompareMode.LENIENT); } @Test - public void testDeserializationReadOnlyFields() throws Exception { - Port port = objectMapper.readValue(PORT_JSON, Port.class); - - assertThat(port.getId(), is(equalTo(ID))); - assertThat(port.getStatus(), is(equalTo(STATUS))); - } - - @Test - public void testDeserializationAfterSerialization() throws Exception { - testSerialization(); - Port port = objectMapper.readValue(serializedPort, Port.class); - - assertThat(port.getAdminStateUp(), is(equalTo(ADMIN_STATE_UP))); - assertThat(port.getDeviceId(), is(equalTo(DEVICE_ID))); - assertThat(port.getDeviceOwner(), is(equalTo(DEVICE_OWNER))); - assertThat(port.getMacAddress(), is(equalTo(MAC_ADDRESS))); - assertThat(port.getName(), is(equalTo(NAME))); - assertThat(port.getNetworkId(), is(equalTo(NETWORK_ID))); - assertThat(port.getTenantId(), is(equalTo(TENANT_ID))); - - assertThat(port.getSecurityGroups(), hasItem(equalTo(SEC_GROUP))); - assertThat(port.getList(), hasItem(new CustomMatcher<Ip>( - "an Ip object with address " + IP_ADDRESS + " and subnet id " + IP_SUBNET_ID) { - - @Override - public boolean matches(Object ip) { - return ip instanceof Ip - && IP_ADDRESS.equals(((Ip) ip).getAddress()) - && IP_SUBNET_ID.equals(((Ip) ip).getSubnetId()); - } - })); + public void testMethods() throws Exception { + Port port = objectMapper.readValue(JSON_FULL, Port.class); + port.toString(); + + String deviceOwner = port.getDeviceOwner(); + Assert.assertNotNull(deviceOwner); + port.setDeviceOwner(deviceOwner); + + Boolean adminStateUp = port.getAdminStateUp(); + Assert.assertNotNull(adminStateUp); + port.setAdminStateUp(adminStateUp); + + String name = port.getName(); + Assert.assertNotNull(name); + port.setName(name); + + String tenantId = port.getTenantId(); + Assert.assertNotNull(tenantId); + port.setTenantId(tenantId); + + Binding binding = port.getBinding(); + Assert.assertNotNull(binding); + port.setBinding(binding); + + String macAddress = port.getMacAddress(); + Assert.assertNotNull(macAddress); + port.setMacAddress(macAddress); + + String networkId = port.getNetworkId(); + Assert.assertNotNull(networkId); + port.setNetworkId(networkId); + + List<Ip> list = port.getList(); + Assert.assertNotNull(list); + Assert.assertEquals(2, list.size()); + port.setList(list); + + List<String> securityGroups = port.getSecurityGroups(); + Assert.assertNotNull(securityGroups); + Assert.assertEquals(2, securityGroups.size()); + port.setSecurityGroups(securityGroups); + + String deviceId = port.getDeviceId(); + Assert.assertNotNull(deviceId); + port.setDeviceId(deviceId); } } |