Promote SchemaSourceRepresentation
[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.jupiter.api.Assertions.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.ir.YangIRSchemaSource;
22 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
23 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactoryConfiguration;
24 import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
25 import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
26 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToIRTransformer;
27
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);
32         try {
33             return Futures.getDone(future);
34         } catch (ExecutionException e) {
35             throw new AssertionError("Failed to create context", e);
36         }
37     }
38
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));
43     }
44
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)
49             .map(resource -> {
50                 final var yangSource = assertYangTextResource(resource);
51                 yangSource.register(sharedSchemaRepository);
52                 yangSource.setResult();
53                 return yangSource.getId();
54             })
55             .collect(Collectors.toUnmodifiableList());
56
57         return sharedSchemaRepository
58             .createEffectiveModelContextFactory(SchemaContextFactoryConfiguration.builder()
59                 .setModulesDeviatedByModules(modulesWithSupportedDeviations)
60                 .build())
61             .createEffectiveModelContext(requiredSources);
62     }
63
64     private static SettableSchemaProvider<YangIRSchemaSource> assertYangTextResource(final String resourceName) {
65         final YangIRSchemaSource yangSource;
66         try {
67             yangSource = TextToIRTransformer.transformText(YangTextSource.forResource(resourceName));
68         } catch (YangSyntaxErrorException | IOException e) {
69             throw new AssertionError("Failed to parse " + resourceName, e);
70         }
71         return SettableSchemaProvider.createImmediate(yangSource, YangIRSchemaSource.class);
72     }
73 }