608fd72f4af0463a7b63e2f3cb7651821466a0e9
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / test / java / org / opendaylight / controller / sal / dom / broker / BackwardsCompatibleMountPointManagerTest.java
1 /*
2  * Copyright (c) 2014 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.controller.sal.dom.broker;
10
11 import static junit.framework.Assert.fail;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Mockito.doAnswer;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.times;
17 import static org.mockito.Mockito.verify;
18 import com.google.common.base.Optional;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.mockito.Mock;
22 import org.mockito.MockitoAnnotations;
23 import org.mockito.invocation.InvocationOnMock;
24 import org.mockito.stubbing.Answer;
25 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
26 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
27 import org.opendaylight.controller.md.sal.dom.api.DOMService;
28 import org.opendaylight.controller.md.sal.dom.broker.impl.mount.DOMMountPointServiceImpl;
29 import org.opendaylight.controller.sal.core.api.mount.MountProvisionInstance;
30 import org.opendaylight.controller.sal.core.api.mount.MountProvisionListener;
31 import org.opendaylight.yangtools.concepts.ObjectRegistration;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
34 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 public class BackwardsCompatibleMountPointManagerTest {
40     private static final Logger log = LoggerFactory.getLogger(BackwardsCompatibleMountPointManagerTest.class);
41
42     @Mock
43     private DOMMountPointServiceImpl domMountPointService;
44     @Mock
45     private DOMMountPointService.DOMMountPointBuilder mountBuilder;
46
47     private BackwardsCompatibleMountPointManager compatibleMountPointManager;
48     static final QName qName = QName.create("namespace", "12-12-1212", "mount");
49     static final YangInstanceIdentifier id = YangInstanceIdentifier.of(qName);
50
51     @Before
52     public void setUp() throws Exception {
53         MockitoAnnotations.initMocks(this);
54         stubMountService();
55         compatibleMountPointManager = new BackwardsCompatibleMountPointManager(domMountPointService);
56     }
57
58     @Test
59     public void testCreateMountpointAlreadyCreated() throws Exception {
60         compatibleMountPointManager.createMountPoint(id);
61         verify(domMountPointService).createMountPoint(id);
62         verify(mountBuilder, times(3)).addService(any(Class.class), any(DOMService.class));
63         verify(mountBuilder).addInitialSchemaContext(any(SchemaContext.class));
64
65         try {
66             compatibleMountPointManager.createMountPoint(id);
67         } catch (final IllegalStateException e) {
68             log.debug("", e);
69             return;
70         }
71         fail("Should fail to create duplicate mount");
72     }
73
74     @Test
75     public void testCreateMountpointGetOrCreate() throws Exception {
76         compatibleMountPointManager = new BackwardsCompatibleMountPointManager(new DOMMountPointServiceImpl());
77
78         final MountProvisionListener listener = new MountProvisionListener() {
79             public int createdMounts = 0;
80
81             @Override
82             public void onMountPointCreated(final YangInstanceIdentifier path) {
83                 if(createdMounts++ > 1 ) {
84                     fail("Only one mount point should have been created");
85                 }
86             }
87
88             @Override
89             public void onMountPointRemoved(final YangInstanceIdentifier path) {}
90         };
91
92         compatibleMountPointManager.registerProvisionListener(listener);
93
94         final MountProvisionInstance m1 = compatibleMountPointManager.createOrGetMountPoint(id);
95         m1.setSchemaContext(mockSchemaContext());
96         compatibleMountPointManager.createOrGetMountPoint(id);
97         compatibleMountPointManager.createOrGetMountPoint(id);
98     }
99
100     private void stubMountService() {
101         doReturn(mockMountPointBuilder()).when(domMountPointService).createMountPoint(any(YangInstanceIdentifier.class));
102         doReturn(Optional.of(mockMountPoint())).when(domMountPointService).getMountPoint(any(YangInstanceIdentifier.class));
103     }
104
105     private DOMMountPoint mockMountPoint() {
106         final DOMMountPoint mock = mock(DOMMountPoint.class);
107         doAnswer(new Answer<Object>() {
108             @Override
109             public Object answer(final InvocationOnMock invocation) throws Throwable {
110                 return Optional.of(mock(((Class<?>) invocation.getArguments()[0])));
111             }
112         }).when(mock).getService(any(Class.class));
113         doReturn(mockSchemaContext()).when(mock).getSchemaContext();
114         return mock;
115     }
116
117     static SchemaContext mockSchemaContext() {
118         final SchemaContext mock = mock(SchemaContext.class);
119         doReturn(qName).when(mock).getQName();
120         doReturn("schema").when(mock).toString();
121         doReturn(mock(DataSchemaNode.class)).when(mock).getDataChildByName(any(QName.class));
122         return mock;
123     }
124
125     private DOMMountPointService.DOMMountPointBuilder mockMountPointBuilder() {
126         doReturn(mountBuilder).when(mountBuilder).addService(any(Class.class), any(DOMService.class));
127         doReturn(mockObjectRegistration()).when(mountBuilder).register();
128         doReturn(mountBuilder).when(mountBuilder).addInitialSchemaContext(any(SchemaContext.class));
129         return mountBuilder;
130     }
131
132     private ObjectRegistration<?> mockObjectRegistration() {
133         return mock(ObjectRegistration.class);
134     }
135
136 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.