From 2e984294ac28c6f2ede290c38164c5d536ccaf4a Mon Sep 17 00:00:00 2001
From: ChrisC <cc697w@intl.att.com>
Date: Tue, 31 Jan 2017 13:57:24 +0100
Subject: Initial OpenECOMP MSO OpenStack SDK lib commit

Change-Id: Ieaacb2b2c0dcc469669880e73f0cda9fa59a6c5a
Signed-off-by: ChrisC <cc697w@intl.att.com>
---
 .../openstack/quantum/model/NetworkTest.java       | 119 ++++++++++++++++
 .../woorea/openstack/quantum/model/PortTest.java   | 155 +++++++++++++++++++++
 .../woorea/openstack/quantum/model/SubnetTest.java | 137 ++++++++++++++++++
 3 files changed, 411 insertions(+)
 create mode 100644 quantum-model/src/test/java/com/woorea/openstack/quantum/model/NetworkTest.java
 create mode 100644 quantum-model/src/test/java/com/woorea/openstack/quantum/model/PortTest.java
 create mode 100644 quantum-model/src/test/java/com/woorea/openstack/quantum/model/SubnetTest.java

(limited to 'quantum-model/src/test')

diff --git a/quantum-model/src/test/java/com/woorea/openstack/quantum/model/NetworkTest.java b/quantum-model/src/test/java/com/woorea/openstack/quantum/model/NetworkTest.java
new file mode 100644
index 0000000..b6255b9
--- /dev/null
+++ b/quantum-model/src/test/java/com/woorea/openstack/quantum/model/NetworkTest.java
@@ -0,0 +1,119 @@
+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.Arrays;
+
+import org.codehaus.jackson.map.ObjectMapper;
+import org.junit.Before;
+import org.junit.Test;
+
+public class NetworkTest {
+
+    private static final String ID = "testId";
+
+    private static final boolean ADMIN_STATE_UP = true;
+
+    private static final String NAME = "name";
+
+    private static final String TENANT_ID = "tenantId";
+
+    private static final String SHARED = "shared";
+
+    private static final String STATUS = "status";
+
+    private static final String SUBNET = "subnet";
+
+    private static final String PROVIDER_NETWORK_TYPE = "vlan";
+
+    private static final String PROVIDER_PHYSICAL_NETWORK = "physicalNetwork";
+
+    private static final int PROVIDER_SEGMENTATION_ID = 100;
+
+    private static final String ROUTER_EXTERNAL = "routerExternal";
+
+    /**
+     * JSON with read only attributes.
+     */
+    private static final String NETWORK_JSON = "{"
+            + "  \"network\" : {"
+            + "    \"id\" : \"" + ID + "\","
+            + "    \"status\" : \"" + STATUS + "\","
+            + "    \"subnets\" : [ \"" + SUBNET + "\" ]"
+            + "  }"
+            + "}";
+
+    private ObjectMapper objectMapper;
+
+    private String serializedNetwork;
+
+    @Before
+    public void setUp() throws Exception {
+        objectMapper = PortTest.initializeObjectMapper();
+    }
+
+    @Test
+    public void testSerialization() throws Exception {
+        Network network = new Network();
+        network.setId(ID);
+        network.setProviderNetworkType(PROVIDER_NETWORK_TYPE);
+        network.setProviderPhysicalNetwork(PROVIDER_PHYSICAL_NETWORK);
+        network.setProviderSegmentationId(PROVIDER_SEGMENTATION_ID);
+        network.setAdminStateUp(ADMIN_STATE_UP);
+        network.setSubnets(Arrays.asList(SUBNET));
+        network.setRouterExternal(ROUTER_EXTERNAL);
+        network.setName(NAME);
+        network.setShared(SHARED);
+        network.setTenantId(TENANT_ID);
+
+        serializedNetwork = objectMapper.writeValueAsString(network);
+        assertThat(serializedNetwork, not(containsString(ID)));
+        assertThat(serializedNetwork, not(containsString(STATUS)));
+        assertThat(serializedNetwork, not(containsString(SUBNET)));
+        assertThat(serializedNetwork, containsString("\"admin_state_up\" : " + ADMIN_STATE_UP));
+        assertThat(serializedNetwork, containsString(NAME));
+        assertThat(serializedNetwork, containsString(SHARED));
+        assertThat(serializedNetwork, containsString(TENANT_ID));
+        assertThat(serializedNetwork, containsString(ROUTER_EXTERNAL));
+        assertThat(serializedNetwork, containsString(PROVIDER_NETWORK_TYPE));
+        assertThat(serializedNetwork, containsString(PROVIDER_PHYSICAL_NETWORK));
+        assertThat(serializedNetwork, containsString(Integer.toString(PROVIDER_SEGMENTATION_ID)));
+    }
+
+    @Test
+    public void testSerializationEmpty() throws Exception {
+        Network network = new Network();
+        serializedNetwork = objectMapper.writeValueAsString(network);
+
+        assertThat(serializedNetwork, containsString("\"network\" : { }"));
+    }
+
+    @Test
+    public void testDeserializationReadOnlyFields() throws Exception {
+        Network network = objectMapper.readValue(NETWORK_JSON, Network.class);
+
+        assertThat(network.getId(), is(equalTo(ID)));
+        assertThat(network.getSubnets(), hasItem(equalTo(SUBNET)));
+        assertThat(network.getStatus(), is(equalTo(STATUS)));
+    }
+
+    @Test
+    public void testDeserialization() throws Exception {
+        testSerialization();
+        Network network = objectMapper.readValue(serializedNetwork, Network.class);
+
+        assertThat(network.getName(), is(equalTo(NAME)));
+        assertThat(network.isAdminStateUp(), is(equalTo(ADMIN_STATE_UP)));
+        assertThat(network.getShared(), is(equalTo(SHARED)));
+        assertThat(network.getTenantId(), is(equalTo(TENANT_ID)));
+        assertThat(network.getRouterExternal(), is(equalTo(ROUTER_EXTERNAL)));
+        assertThat(network.getProviderNetworkType(), is(equalTo(PROVIDER_NETWORK_TYPE)));
+        assertThat(network.getProviderPhysicalNetwork(), is(equalTo(PROVIDER_PHYSICAL_NETWORK)));
+        assertThat(network.getProviderSegmentationId(), is(equalTo(PROVIDER_SEGMENTATION_ID)));
+    }
+}
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
new file mode 100644
index 0000000..bce5044
--- /dev/null
+++ b/quantum-model/src/test/java/com/woorea/openstack/quantum/model/PortTest.java
@@ -0,0 +1,155 @@
+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 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.Test;
+
+import com.woorea.openstack.quantum.model.Port.Ip;
+
+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;
+    }
+
+    @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\" : { }"));
+    }
+
+    @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());
+            }
+        }));
+    }
+}
diff --git a/quantum-model/src/test/java/com/woorea/openstack/quantum/model/SubnetTest.java b/quantum-model/src/test/java/com/woorea/openstack/quantum/model/SubnetTest.java
new file mode 100644
index 0000000..9136a38
--- /dev/null
+++ b/quantum-model/src/test/java/com/woorea/openstack/quantum/model/SubnetTest.java
@@ -0,0 +1,137 @@
+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.Arrays;
+
+import org.codehaus.jackson.map.ObjectMapper;
+import org.hamcrest.CustomMatcher;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.woorea.openstack.quantum.model.Subnet.IpVersion;
+
+public class SubnetTest {
+
+    private static final String POOL_END = "poolEnd";
+
+    private static final String POOL_START = "poolStart";
+
+    private static final String TENANT_ID = "tenantId";
+
+    private static final String NETWORK_ID = "networkId";
+
+    private static final String NAME = "name";
+
+    private static final String HOST_ROUTE = "hostRoute";
+
+    private static final String GATEWAY = "gw";
+
+    private static final boolean ENABLE_DHCP = true;
+
+    private static final String ID = "testId";
+
+    private static final String CIDR = "10.0.0.0/8";
+
+    private static final String DNS_SERVER = "dnsServer";
+
+    private static final IpVersion IP_VERSION = IpVersion.IPV4;
+
+    /**
+     * JSON with read only attributes.
+     */
+    private static final String SUBNET_JSON = "{"
+            + "  \"subnet\" : {"
+            + "    \"id\" : \"" + ID + "\""
+            + "  }"
+            + "}";
+
+    private ObjectMapper objectMapper;
+
+    private String serializedSubnet;
+
+    @Before
+    public void setUp() throws Exception {
+        objectMapper = PortTest.initializeObjectMapper();
+    }
+
+    @Test
+    public void testSerialization() throws Exception {
+        Subnet subnet = new Subnet();
+        subnet.setId(ID);
+        subnet.setCidr(CIDR);
+        subnet.setDnsNames(Arrays.asList(DNS_SERVER));
+        subnet.setEnableDHCP(ENABLE_DHCP);
+        subnet.setIpversion(IP_VERSION);
+        subnet.setGw(GATEWAY);
+        subnet.setHostRoutes(Arrays.asList(HOST_ROUTE));
+        subnet.setName(NAME);
+        subnet.setNetworkId(NETWORK_ID);
+        subnet.setTenantId(TENANT_ID);
+
+        Pool pool = new Pool();
+        pool.setStart(POOL_START);
+        pool.setEnd(POOL_END);
+        subnet.setList(Arrays.asList(pool));
+
+        serializedSubnet = objectMapper.writeValueAsString(subnet);
+        assertThat(serializedSubnet, not(containsString(ID)));
+        assertThat(serializedSubnet, containsString(CIDR));
+        assertThat(serializedSubnet, containsString(DNS_SERVER));
+        assertThat(serializedSubnet, containsString("\"enable_dhcp\" : " + ENABLE_DHCP));
+        assertThat(serializedSubnet, containsString(Integer.toString(IP_VERSION.code())));
+        assertThat(serializedSubnet, containsString(GATEWAY));
+        assertThat(serializedSubnet, containsString(HOST_ROUTE));
+        assertThat(serializedSubnet, containsString(NAME));
+        assertThat(serializedSubnet, containsString(NETWORK_ID));
+        assertThat(serializedSubnet, containsString(TENANT_ID));
+        assertThat(serializedSubnet, containsString(POOL_START));
+        assertThat(serializedSubnet, containsString(POOL_END));
+    }
+
+    @Test
+    public void testSerializationEmpty() throws Exception {
+        Subnet subnet = new Subnet();
+        serializedSubnet = objectMapper.writeValueAsString(subnet);
+
+        assertThat(serializedSubnet, containsString("\"subnet\" : { }"));
+    }
+
+    @Test
+    public void testDeserializationReadOnlyFields() throws Exception {
+        Subnet subnet = objectMapper.readValue(SUBNET_JSON, Subnet.class);
+
+        assertThat(subnet.getId(), is(equalTo(ID)));
+    }
+
+    @Test
+    public void testDeserializationAfterSerialization() throws Exception {
+        testSerialization();
+        Subnet subnet = objectMapper.readValue(serializedSubnet, Subnet.class);
+
+        assertThat(subnet.getCidr(), is(equalTo(CIDR)));
+        assertThat(subnet.getDnsNames(), hasItem(equalTo(DNS_SERVER)));
+        assertThat(subnet.isEnableDHCP(), is(equalTo(ENABLE_DHCP)));
+        assertThat(subnet.getIpversion(), is(equalTo(IP_VERSION)));
+        assertThat(subnet.getGw(), is(equalTo(GATEWAY)));
+        assertThat(subnet.getHostRoutes(), hasItem(equalTo(HOST_ROUTE)));
+        assertThat(subnet.getName(), is(equalTo(NAME)));
+        assertThat(subnet.getNetworkId(), is(equalTo(NETWORK_ID)));
+        assertThat(subnet.getTenantId(), is(equalTo(TENANT_ID)));
+        assertThat(subnet.getList(), hasItem(new CustomMatcher<Pool>(
+                "a Pool object with start " + POOL_START + " and end " + POOL_END) {
+
+            @Override
+            public boolean matches(Object pool) {
+                return pool instanceof Pool
+                        && POOL_START.equals(((Pool) pool).getStart())
+                        && POOL_END.equals(((Pool) pool).getEnd());
+            }
+        }));
+    }
+}
-- 
cgit 1.2.3-korg