Tests for neutron-ovsdb
[groupbasedpolicy.git] / neutron-ovsdb / src / test / java / org / opendaylight / groupbasedpolicy / neutron / ovsdb / util / EndpointHelperTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others. 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.groupbasedpolicy.neutron.ovsdb.util;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNull;
13 import static org.mockito.Matchers.any;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.verify;
16 import static org.mockito.Mockito.when;
17
18 import com.google.common.base.Optional;
19 import com.google.common.util.concurrent.CheckedFuture;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
23 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
26 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2BridgeDomainId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContext;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35
36 public class EndpointHelperTest {
37
38     private EndpointKey epKey;
39     private Endpoint endpoint;
40     private ReadOnlyTransaction readTransaction;
41     private ReadWriteTransaction writeTransaction;
42     private Optional<Endpoint> readOptional;
43     private CheckedFuture<Void, TransactionCommitFailedException> submitFuture;
44
45     @SuppressWarnings("unchecked")
46     @Before
47     public void init() throws Exception {
48         epKey = mock(EndpointKey.class);
49         OfOverlayContext ofc = mock(OfOverlayContext.class);
50         endpoint = new EndpointBuilder().setL2Context(new L2BridgeDomainId("foo"))
51             .setMacAddress(new MacAddress("01:23:45:67:89:AB"))
52             .setTenant(new TenantId("fooTenant"))
53             .addAugmentation(OfOverlayContext.class, ofc)
54             .build();
55         readTransaction = mock(ReadOnlyTransaction.class);
56         writeTransaction = mock(ReadWriteTransaction.class);
57
58         CheckedFuture<Optional<Endpoint>, ReadFailedException> readFuture = mock(CheckedFuture.class);
59         when(readTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class)))
60             .thenReturn(readFuture);
61         readOptional = mock(Optional.class);
62         when(readFuture.checkedGet()).thenReturn(readOptional);
63
64         submitFuture = mock(CheckedFuture.class);
65         when(writeTransaction.submit()).thenReturn(submitFuture);
66     }
67
68     @Test
69     public void testLookupEndpoint() {
70         when(readOptional.isPresent()).thenReturn(true);
71         when(readOptional.get()).thenReturn(endpoint);
72         Endpoint result = EndpointHelper.lookupEndpoint(epKey, readTransaction);
73         assertEquals(result, endpoint);
74     }
75
76     @Test
77     public void testLookupEndpoint_NotPresent() {
78         when(readOptional.isPresent()).thenReturn(false);
79         Endpoint result = EndpointHelper.lookupEndpoint(epKey, readTransaction);
80         assertNull(result);
81     }
82
83     @Test
84     public void testUpdateEndpointWithLocation() throws Exception {
85         String nodeIdString = "nodeIdString";
86         String nodeConnectorIdString = "nodeConnectorIdString";
87         EndpointHelper.updateEndpointWithLocation(endpoint, nodeIdString, nodeConnectorIdString, writeTransaction);
88         verify(submitFuture).checkedGet();
89     }
90
91     @Test
92     public void testUpdateEndpointRemoveLocation() throws Exception {
93         EndpointHelper.updateEndpointRemoveLocation(endpoint, writeTransaction);
94         verify(submitFuture).checkedGet();
95     }
96
97 }