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.YangIRSchemaSource;
24 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
25 import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
26 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToIRTransformer;
28 public abstract class AbstractSchemaRepositoryTest {
29 static @NonNull EffectiveModelContext assertModelContext(
30 final SetMultimap<QNameModule, QNameModule> modulesWithSupportedDeviations, final String... resources) {
31 final var future = createModelContext(modulesWithSupportedDeviations, resources);
33 return Futures.getDone(future);
34 } catch (ExecutionException e) {
35 throw new AssertionError("Failed to create context", e);
39 static @NonNull ExecutionException assertExecutionException(
40 final SetMultimap<QNameModule, QNameModule> modulesWithSupportedDeviations, final String... resources) {
41 final var future = createModelContext(modulesWithSupportedDeviations, resources);
42 return assertThrows(ExecutionException.class, () -> Futures.getDone(future));
45 static ListenableFuture<EffectiveModelContext> createModelContext(
46 final SetMultimap<QNameModule, QNameModule> modulesWithSupportedDeviations, final String... resources) {
47 final var sharedSchemaRepository = new SharedSchemaRepository();
48 final var requiredSources = Arrays.stream(resources)
50 final var yangSource = assertYangTextResource(resource);
51 yangSource.register(sharedSchemaRepository);
52 yangSource.setResult();
53 return yangSource.getId();
55 .collect(Collectors.toUnmodifiableList());
57 return sharedSchemaRepository
58 .createEffectiveModelContextFactory(SchemaContextFactoryConfiguration.builder()
59 .setModulesDeviatedByModules(modulesWithSupportedDeviations)
61 .createEffectiveModelContext(requiredSources);
64 private static SettableSchemaProvider<YangIRSchemaSource> assertYangTextResource(final String resourceName) {
65 final YangIRSchemaSource yangSource;
67 yangSource = TextToIRTransformer.transformText(YangTextSchemaSource.forResource(resourceName));
68 } catch (YangSyntaxErrorException | IOException e) {
69 throw new AssertionError("Failed to parse " + resourceName, e);
71 return SettableSchemaProvider.createImmediate(yangSource, YangIRSchemaSource.class);