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;
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;
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;
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() {
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(
.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)
}
@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)
}
@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)
}
@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)
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;
}