Rename YangIRSchemaSource
[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.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertNotNull;
12 import static org.junit.jupiter.api.Assertions.assertThrows;
13
14 import com.google.common.collect.SetMultimap;
15 import com.google.common.util.concurrent.Futures;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import java.io.IOException;
18 import java.util.Arrays;
19 import java.util.concurrent.ExecutionException;
20 import java.util.stream.Collectors;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.opendaylight.yangtools.yang.common.QNameModule;
23 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
24 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactoryConfiguration;
25 import org.opendaylight.yangtools.yang.model.spi.source.URLYangTextSource;
26 import org.opendaylight.yangtools.yang.model.spi.source.YangIRSource;
27 import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
28 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToIRTransformer;
29
30 public abstract class AbstractSchemaRepositoryTest {
31     static @NonNull EffectiveModelContext assertModelContext(
32             final SetMultimap<QNameModule, QNameModule> modulesWithSupportedDeviations, final String... resources) {
33         final var future = createModelContext(modulesWithSupportedDeviations, resources);
34         try {
35             return Futures.getDone(future);
36         } catch (ExecutionException e) {
37             throw new AssertionError("Failed to create context", e);
38         }
39     }
40
41     static @NonNull ExecutionException assertExecutionException(
42             final SetMultimap<QNameModule, QNameModule> modulesWithSupportedDeviations, final String... resources) {
43         final var future = createModelContext(modulesWithSupportedDeviations, resources);
44         return assertThrows(ExecutionException.class, () -> Futures.getDone(future));
45     }
46
47     static ListenableFuture<EffectiveModelContext> createModelContext(
48             final SetMultimap<QNameModule, QNameModule> modulesWithSupportedDeviations, final String... resources) {
49         final var sharedSchemaRepository = new SharedSchemaRepository();
50         final var requiredSources = Arrays.stream(resources)
51             .map(resource -> {
52                 final var yangSource = assertYangTextResource(resource);
53                 yangSource.register(sharedSchemaRepository);
54                 yangSource.setResult();
55                 return yangSource.getId();
56             })
57             .collect(Collectors.toUnmodifiableList());
58
59         return sharedSchemaRepository
60             .createEffectiveModelContextFactory(SchemaContextFactoryConfiguration.builder()
61                 .setModulesDeviatedByModules(modulesWithSupportedDeviations)
62                 .build())
63             .createEffectiveModelContext(requiredSources);
64     }
65
66     static final SettableSchemaProvider<YangIRSource> assertYangTextResource(final String resourceName) {
67         final YangIRSource yangSource;
68         try {
69             yangSource = TextToIRTransformer.transformText(
70                 new URLYangTextSource(AbstractSchemaRepositoryTest.class.getResource(resourceName)));
71         } catch (YangSyntaxErrorException | IOException e) {
72             throw new AssertionError("Failed to parse " + resourceName, e);
73         }
74         return SettableSchemaProvider.createImmediate(yangSource, YangIRSource.class);
75     }
76
77     static final void assertSchemaContext(final EffectiveModelContext schemaContext, final int moduleSize) {
78         assertNotNull(schemaContext);
79         assertEquals(moduleSize, schemaContext.getModuleStatements().size());
80     }
81 }