67ca8cd5a5d0dd6ffb995f12348a183e8e5dd20d
[netconf.git] / apps / yanglib-mdsal-writer / src / test / java / org / opendaylight / netconf / yanglib / writer / YangLibraryWriterSingletonIntegrationTest.java
1 /*
2  * Copyright (c) 2024 PANTHEON.tech, s.r.o. 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.netconf.yanglib.writer;
9
10 import static org.awaitility.Awaitility.await;
11
12 import java.time.Duration;
13 import org.junit.Test;
14 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractConcurrentDataBrokerTest;
15 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
16 import org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService;
17 import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipService;
18 import org.opendaylight.mdsal.eos.dom.simple.SimpleDOMEntityOwnershipService;
19 import org.opendaylight.mdsal.singleton.impl.EOSClusterSingletonServiceProvider;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.YangLibrary;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22
23 public class YangLibraryWriterSingletonIntegrationTest extends AbstractConcurrentDataBrokerTest {
24     private final DOMEntityOwnershipService eos = new SimpleDOMEntityOwnershipService();
25
26     @Test
27     public void testIntegrationNoLegacy() throws Exception {
28         testIntegration(false);
29     }
30
31     @Test
32     public void testIntegrationLegacy() throws Exception {
33         testIntegration(true);
34     }
35
36     private void testIntegration(final boolean writeLegacy) throws Exception {
37         try (var cssProvider = new EOSClusterSingletonServiceProvider(eos)) {
38             try (var singleton = new YangLibraryWriterSingleton(cssProvider, new FixedDOMSchemaService(modelContext()),
39                     getDataBroker(), writeLegacy)) {
40                 await().atMost(Duration.ofSeconds(5)).until(this::yangLibraryExists);
41             }
42             await().atMost(Duration.ofSeconds(5)).until(() -> !yangLibraryExists());
43         }
44     }
45
46     private boolean yangLibraryExists() throws Exception {
47         try (var tx = getDataBroker().newReadOnlyTransaction()) {
48             return tx.exists(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(YangLibrary.class)).get();
49         }
50     }
51 }