9efa922e9ae5c153d4360e86cdebd64327e5d310
[mdsal.git] / dom / mdsal-dom-broker / src / test / java / org / opendaylight / mdsal / dom / broker / MountPointServiceTest.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 package org.opendaylight.mdsal.dom.broker;
9
10 import static org.junit.Assert.assertFalse;
11 import static org.junit.Assert.assertSame;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Mockito.doNothing;
14 import static org.mockito.Mockito.mock;
15
16 import java.util.Map;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
20 import org.opendaylight.mdsal.dom.api.DOMMountPointListener;
21 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
22 import org.opendaylight.mdsal.dom.api.DOMMountPointService.DOMMountPointBuilder;
23 import org.opendaylight.mdsal.dom.api.DOMService;
24 import org.opendaylight.mdsal.dom.broker.DOMMountPointServiceImpl.DOMMountPointBuilderImpl;
25 import org.opendaylight.yangtools.concepts.ObjectRegistration;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29
30 public class MountPointServiceTest {
31
32     private static final YangInstanceIdentifier PATH = YangInstanceIdentifier.of(QName.create("namespace", "12-12-2012",
33         "top"));
34
35     private DOMMountPointService mountService;
36
37     @Before
38     public void setup() {
39         mountService = new DOMMountPointServiceImpl();
40     }
41
42     @Test
43     public void createSimpleMountPoint() throws Exception {
44         final DOMMountPointListener listener = mock(DOMMountPointListener.class);
45         doNothing().when(listener).onMountPointCreated(PATH);
46         mountService.registerProvisionListener(listener);
47
48         assertFalse(mountService.getMountPoint(PATH).isPresent());
49
50         mountService.createMountPoint(PATH).register();
51
52         assertTrue(mountService.getMountPoint(PATH).isPresent());
53     }
54
55     @Test
56     public void unregisterTest() throws Exception {
57         final DOMMountPointListener listener = mock(DOMMountPointListener.class);
58         doNothing().when(listener).onMountPointCreated(PATH);
59         doNothing().when(listener).onMountPointRemoved(PATH);
60         final DOMMountPointServiceImpl mountService = new DOMMountPointServiceImpl();
61         mountService.registerProvisionListener(listener);
62         mountService.createMountPoint(PATH).register();
63
64         assertTrue(mountService.getMountPoint(PATH).isPresent());
65
66         mountService.unregisterMountPoint(PATH);
67
68         assertFalse(mountService.getMountPoint(PATH).isPresent());
69     }
70
71     @Test
72     public void mountRegistrationTest() throws Exception {
73         final DOMMountPointBuilder mountBuilder = mountService.createMountPoint(PATH);
74         final ObjectRegistration<DOMMountPoint> objectRegistration = mountBuilder.register();
75
76         assertTrue(mountService.getMountPoint(PATH).isPresent());
77         assertSame(objectRegistration.getInstance(), mountService.getMountPoint(PATH).get());
78
79         objectRegistration.close();
80
81         assertFalse(mountService.getMountPoint(PATH).isPresent());
82     }
83
84     @Test
85     public void mountBuilderTest() throws Exception {
86         final DOMMountPointBuilderImpl mountBuilder = (DOMMountPointBuilderImpl) mountService.createMountPoint(PATH);
87         mountBuilder.register();
88
89         final SchemaContext mockSchemaContext = mock(SchemaContext.class);
90         mountBuilder.addInitialSchemaContext(mockSchemaContext);
91
92         assertSame(mockSchemaContext, mountBuilder.getSchemaContext());
93
94         final Map<Class<? extends DOMService>, DOMService> services = mountBuilder.getServices();
95         assertTrue(services.isEmpty());
96         mountBuilder.addService(DOMService.class, mock(DOMService.class));
97         assertTrue(services.containsKey(DOMService.class));
98     }
99 }