2 * Copyright (c) 2022 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.repo;
10 import static org.junit.Assert.assertThrows;
12 import com.google.common.collect.SetMultimap;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import java.io.IOException;
16 import java.util.Arrays;
17 import java.util.concurrent.ExecutionException;
18 import java.util.stream.Collectors;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.opendaylight.yangtools.yang.common.QNameModule;
21 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
22 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactoryConfiguration;
23 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
24 import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
25 import org.opendaylight.yangtools.yang.parser.rfc7950.ir.IRSchemaSource;
26 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToIRTransformer;
27 import org.opentest4j.AssertionFailedError;
29 public abstract class AbstractSchemaRepositoryTest {
30 static @NonNull EffectiveModelContext assertModelContext(
31 final SetMultimap<QNameModule, QNameModule> modulesWithSupportedDeviations, final String... resources) {
32 final var future = createModelContext(modulesWithSupportedDeviations, resources);
34 return Futures.getDone(future);
35 } catch (ExecutionException e) {
36 throw new AssertionFailedError("Failed to create context", e);
40 static @NonNull ExecutionException assertExecutionException(
41 final SetMultimap<QNameModule, QNameModule> modulesWithSupportedDeviations, final String... resources) {
42 final var future = createModelContext(modulesWithSupportedDeviations, resources);
43 return assertThrows(ExecutionException.class, () -> Futures.getDone(future));
46 static ListenableFuture<EffectiveModelContext> createModelContext(
47 final SetMultimap<QNameModule, QNameModule> modulesWithSupportedDeviations, final String... resources) {
48 final var sharedSchemaRepository = new SharedSchemaRepository();
49 final var requiredSources = Arrays.stream(resources)
51 final var yangSource = assertYangTextResource(resource);
52 yangSource.register(sharedSchemaRepository);
53 yangSource.setResult();
54 return yangSource.getId();
56 .collect(Collectors.toUnmodifiableList());
58 return sharedSchemaRepository
59 .createEffectiveModelContextFactory(SchemaContextFactoryConfiguration.builder()
60 .setModulesDeviatedByModules(modulesWithSupportedDeviations)
62 .createEffectiveModelContext(requiredSources);
65 private static SettableSchemaProvider<IRSchemaSource> assertYangTextResource(final String resourceName) {
66 final IRSchemaSource yangSource;
68 yangSource = TextToIRTransformer.transformText(YangTextSchemaSource.forResource(resourceName));
69 } catch (YangSyntaxErrorException | IOException e) {
70 throw new AssertionError("Failed to parse " + resourceName, e);
72 return SettableSchemaProvider.createImmediate(yangSource, IRSchemaSource.class);