Fix failure source not being reported
[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 com.google.common.collect.SetMultimap;
11 import com.google.common.util.concurrent.ListenableFuture;
12 import java.io.IOException;
13 import java.util.Arrays;
14 import java.util.stream.Collectors;
15 import org.opendaylight.yangtools.yang.common.QNameModule;
16 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
17 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactoryConfiguration;
18 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
19 import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
20 import org.opendaylight.yangtools.yang.parser.rfc7950.ir.IRSchemaSource;
21 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToIRTransformer;
22
23 public abstract class AbstractSchemaRepositoryTest {
24     static ListenableFuture<EffectiveModelContext> createSchemaContext(
25             final SetMultimap<QNameModule, QNameModule> modulesWithSupportedDeviations, final String... resources) {
26         final var sharedSchemaRepository = new SharedSchemaRepository();
27         final var requiredSources = Arrays.stream(resources)
28             .map(resource -> {
29                 final var yangSource = assertYangTextResource(resource);
30                 yangSource.register(sharedSchemaRepository);
31                 yangSource.setResult();
32                 return yangSource.getId();
33             })
34             .collect(Collectors.toUnmodifiableList());
35
36         return sharedSchemaRepository
37             .createEffectiveModelContextFactory(SchemaContextFactoryConfiguration.builder()
38                 .setModulesDeviatedByModules(modulesWithSupportedDeviations)
39                 .build())
40             .createEffectiveModelContext(requiredSources);
41     }
42
43     private static SettableSchemaProvider<IRSchemaSource> assertYangTextResource(final String resourceName) {
44         final IRSchemaSource yangSource;
45         try {
46             yangSource = TextToIRTransformer.transformText(YangTextSchemaSource.forResource(resourceName));
47         } catch (YangSyntaxErrorException | IOException e) {
48             throw new AssertionError("Failed to parse " + resourceName, e);
49         }
50         return SettableSchemaProvider.createImmediate(yangSource, IRSchemaSource.class);
51     }
52 }