Remove RevisionSourceIdentifier
[yangtools.git] / parser / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / repo / AbstractSchemaRepositoryTest.java
1 /*
2  * Copyright (c) 2022 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.yangtools.yang.parser.repo;
9
10 import static org.junit.Assert.assertThrows;
11
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;
28
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);
33         try {
34             return Futures.getDone(future);
35         } catch (ExecutionException e) {
36             throw new AssertionFailedError("Failed to create context", e);
37         }
38     }
39
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));
44     }
45
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)
50             .map(resource -> {
51                 final var yangSource = assertYangTextResource(resource);
52                 yangSource.register(sharedSchemaRepository);
53                 yangSource.setResult();
54                 return yangSource.getId();
55             })
56             .collect(Collectors.toUnmodifiableList());
57
58         return sharedSchemaRepository
59             .createEffectiveModelContextFactory(SchemaContextFactoryConfiguration.builder()
60                 .setModulesDeviatedByModules(modulesWithSupportedDeviations)
61                 .build())
62             .createEffectiveModelContext(requiredSources);
63     }
64
65     private static SettableSchemaProvider<IRSchemaSource> assertYangTextResource(final String resourceName) {
66         final IRSchemaSource yangSource;
67         try {
68             yangSource = TextToIRTransformer.transformText(YangTextSchemaSource.forResource(resourceName));
69         } catch (YangSyntaxErrorException | IOException e) {
70             throw new AssertionError("Failed to parse " + resourceName, e);
71         }
72         return SettableSchemaProvider.createImmediate(yangSource, IRSchemaSource.class);
73     }
74 }