Bug 3695 - UT GBP - 6 - 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.mockito.Matchers.any;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.verify;
14 import static org.mockito.Mockito.when;
15
16 import org.junit.Assert;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
20 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
23 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointKey;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContext;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28
29 import com.google.common.base.Optional;
30 import com.google.common.util.concurrent.CheckedFuture;
31
32 public class EndpointHelperTest {
33
34     EndpointKey epKey;
35     Endpoint endpoint;
36     ReadOnlyTransaction readTransaction;
37     ReadWriteTransaction writeTransaction;
38     Optional<Endpoint> readOptional;
39     CheckedFuture<Void, TransactionCommitFailedException> submitFuture;
40
41     @SuppressWarnings("unchecked")
42     @Before
43     public void initialise() throws Exception {
44         epKey = mock(EndpointKey.class);
45         endpoint = mock(Endpoint.class);
46         readTransaction = mock(ReadOnlyTransaction.class);
47         writeTransaction = mock(ReadWriteTransaction.class);
48
49         CheckedFuture<Optional<Endpoint>, ReadFailedException> readFuture = mock(CheckedFuture.class);
50         when(readTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(
51                 readFuture);
52         readOptional = mock(Optional.class);
53         when(readFuture.checkedGet()).thenReturn(readOptional);
54
55         OfOverlayContext ofc = mock(OfOverlayContext.class);
56         when(endpoint.getAugmentation(OfOverlayContext.class)).thenReturn(ofc);
57
58         submitFuture = mock(CheckedFuture.class);
59         when(writeTransaction.submit()).thenReturn(submitFuture);
60     }
61
62     @Test
63     public void lookupEndpointTest() {
64         when(readOptional.isPresent()).thenReturn(true);
65         when(readOptional.get()).thenReturn(endpoint);
66         Endpoint result = EndpointHelper.lookupEndpoint(epKey, readTransaction);
67         Assert.assertEquals(result, endpoint);
68     }
69
70     @Test
71     public void lookupEndpointTestNull() {
72         when(readOptional.isPresent()).thenReturn(false);
73         Endpoint result = EndpointHelper.lookupEndpoint(epKey, readTransaction);
74         Assert.assertNull(result);
75     }
76
77     @Test
78     public void updateEndpointWithLocationTest() throws Exception {
79         String nodeIdString = "nodeIdString";
80         String nodeConnectorIdString = "nodeConnectorIdString";
81         EndpointHelper.updateEndpointWithLocation(endpoint, nodeIdString, nodeConnectorIdString, writeTransaction);
82         verify(submitFuture).checkedGet();
83     }
84
85     @Test
86     public void updateEndpointRemoveLocationTest() throws Exception {
87         EndpointHelper.updateEndpointRemoveLocation(endpoint, writeTransaction);
88         verify(submitFuture).checkedGet();
89     }
90
91 }