Add support for keyed entries
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / test / java / org / opendaylight / mdsal / binding / dom / adapter / query / QueryBuilderTest.java
index 5fe94de761ad01edf060af55c1b96800504ae974..983929fcfb6f3fffb39eb6765413bfc68c4a879b 100644 (file)
@@ -13,6 +13,7 @@ import com.google.common.base.Stopwatch;
 import java.util.List;
 import org.eclipse.jdt.annotation.NonNull;
 import org.junit.AfterClass;
+import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
 import org.opendaylight.mdsal.binding.api.query.QueryExecutor;
@@ -20,7 +21,6 @@ import org.opendaylight.mdsal.binding.api.query.QueryExpression;
 import org.opendaylight.mdsal.binding.api.query.QueryFactory;
 import org.opendaylight.mdsal.binding.api.query.QueryResult;
 import org.opendaylight.mdsal.binding.api.query.QueryResult.Item;
-import org.opendaylight.mdsal.binding.api.query.QueryStructureException;
 import org.opendaylight.mdsal.binding.dom.codec.impl.BindingNormalizedNodeCodecRegistry;
 import org.opendaylight.mdsal.binding.generator.impl.GeneratedClassLoadingStrategy;
 import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext;
@@ -30,6 +30,7 @@ import org.opendaylight.yang.gen.v1.mdsal.query.norev.Foo;
 import org.opendaylight.yang.gen.v1.mdsal.query.norev.FooBuilder;
 import org.opendaylight.yang.gen.v1.mdsal.query.norev.first.grp.System;
 import org.opendaylight.yang.gen.v1.mdsal.query.norev.first.grp.SystemBuilder;
+import org.opendaylight.yang.gen.v1.mdsal.query.norev.first.grp.SystemKey;
 import org.opendaylight.yang.gen.v1.mdsal.query.norev.second.grp.Alarms;
 import org.opendaylight.yang.gen.v1.mdsal.query.norev.second.grp.AlarmsBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.Top;
@@ -46,9 +47,9 @@ import org.slf4j.LoggerFactory;
 public class QueryBuilderTest {
     private static final Logger LOG = LoggerFactory.getLogger(QueryBuilderTest.class);
     private static BindingNormalizedNodeCodecRegistry CODEC;
-    private static QueryExecutor EXECUTOR;
 
     private final QueryFactory factory = new DefaultQueryFactory(CODEC);
+    private QueryExecutor executor;
 
     @BeforeClass
     public static void beforeClass() {
@@ -58,7 +59,16 @@ public class QueryBuilderTest {
 
         CODEC = new BindingNormalizedNodeCodecRegistry(
             BindingRuntimeContext.create(GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy(), schemaContext));
-        EXECUTOR = SimpleQueryExecutor.builder(CODEC.getCodecContext())
+    }
+
+    @AfterClass
+    public static final void afterClass() {
+        CODEC = null;
+    }
+
+    @Before
+    public void before() {
+        executor = SimpleQueryExecutor.builder(CODEC.getCodecContext())
             .add(new FooBuilder()
                 .setSystem(List.of(
                     new SystemBuilder().setName("first").setAlarms(List.of(
@@ -91,14 +101,8 @@ public class QueryBuilderTest {
             .build();
     }
 
-    @AfterClass
-    public static void afterClass() {
-        CODEC = null;
-        EXECUTOR = null;
-    }
-
     @Test
-    public void bar() throws QueryStructureException {
+    public void bar() {
         final Stopwatch sw = Stopwatch.createStarted();
         final QueryExpression<TopLevelList> query = factory.querySubtree(InstanceIdentifier.create(Top.class))
                 .extractChild(TopLevelList.class)
@@ -113,7 +117,7 @@ public class QueryBuilderTest {
     }
 
     @Test
-    public void testFindCriticalAlarms() throws QueryStructureException {
+    public void testFindCriticalAlarms() {
         final Stopwatch sw = Stopwatch.createStarted();
         final QueryExpression<Alarms> query = factory.querySubtree(InstanceIdentifier.create(Foo.class))
             .extractChild(System.class)
@@ -128,7 +132,7 @@ public class QueryBuilderTest {
     }
 
     @Test
-    public void testFindNonCriticalAlarms() throws QueryStructureException {
+    public void testFindNonCriticalAlarms() {
         final Stopwatch sw = Stopwatch.createStarted();
         final QueryExpression<Alarms> query = factory.querySubtree(InstanceIdentifier.create(Foo.class))
             .extractChild(System.class)
@@ -143,7 +147,7 @@ public class QueryBuilderTest {
     }
 
     @Test
-    public void testFindZeroAlarms() throws QueryStructureException {
+    public void testFindZeroAlarms() {
         final Stopwatch sw = Stopwatch.createStarted();
         final QueryExpression<Alarms> query = factory.querySubtree(InstanceIdentifier.create(Foo.class))
             .extractChild(System.class)
@@ -157,9 +161,40 @@ public class QueryBuilderTest {
         assertEquals(2, items.size());
     }
 
-    private static <T extends @NonNull DataObject> QueryResult<T> execute(final QueryExpression<T> query) {
+    @Test
+    public void testFindSystemFirstAlarmOne() {
+        final Stopwatch sw = Stopwatch.createStarted();
+        final QueryExpression<Alarms> query = factory.querySubtree(InstanceIdentifier.create(Foo.class))
+            .extractChild(System.class, new SystemKey("first"))
+            .extractChild(Alarms.class)
+                .matching()
+                    .leaf(Alarms::getId).valueEquals(Uint64.ZERO)
+                .build();
+        LOG.info("Query built in {}", sw);
+
+        final List<? extends Item<@NonNull Alarms>> items = execute(query).getItems();
+        assertEquals(1, items.size());
+    }
+
+    @Test
+    public void testFindSystemFirstWithAlarmOne() {
+        final Stopwatch sw = Stopwatch.createStarted();
+        final QueryExpression<System> query = factory.querySubtree(InstanceIdentifier.create(Foo.class))
+            .extractChild(System.class, new SystemKey("first"))
+                .matching()
+                    .childObject(Alarms.class)
+                        .leaf(Alarms::getId).valueEquals(Uint64.ZERO)
+                .build();
+        LOG.info("Query built in {}", sw);
+
+        final List<? extends Item<@NonNull System>> items = execute(query).getItems();
+        assertEquals(1, items.size());
+    }
+
+
+    private <T extends @NonNull DataObject> QueryResult<T> execute(final QueryExpression<T> query) {
         final Stopwatch sw = Stopwatch.createStarted();
-        final QueryResult<T> result = EXECUTOR.executeQuery(query);
+        final QueryResult<T> result = executor.executeQuery(query);
         LOG.info("Query executed in {}", sw);
         return result;
     }