Harden yang-model-util test suite
[yangtools.git] / yang / yang-model-util / src / test / java / org / opendaylight / yangtools / yang / model / util / SimpleSchemaContextTest.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, 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.model.util;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.mockito.Mockito.doReturn;
12 import static org.mockito.Mockito.mock;
13
14 import com.google.common.collect.ImmutableList;
15 import com.google.common.collect.ImmutableSet;
16 import java.net.URI;
17 import java.util.List;
18 import java.util.Optional;
19 import java.util.Set;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.yang.common.QNameModule;
22 import org.opendaylight.yangtools.yang.common.Revision;
23 import org.opendaylight.yangtools.yang.model.api.Module;
24
25 public class SimpleSchemaContextTest {
26     @Test
27     public void testGetModulesOrdering() {
28         final Module foo0 = mockModule("foo", Optional.empty());
29         final Module foo1 = mockModule("foo", Revision.ofNullable("2018-01-01"));
30         final Module foo2 = mockModule("foo", Revision.ofNullable("2018-01-16"));
31
32         final List<Module> expected = ImmutableList.of(foo2, foo1, foo0);
33         assertGetModules(expected, foo0, foo1, foo2);
34         assertGetModules(expected, foo0, foo2, foo1);
35         assertGetModules(expected, foo1, foo0, foo2);
36         assertGetModules(expected, foo1, foo2, foo0);
37         assertGetModules(expected, foo2, foo0, foo1);
38         assertGetModules(expected, foo2, foo1, foo0);
39
40         assertFindModules(expected, "foo", foo0, foo1, foo2);
41         assertFindModules(expected, "foo", foo0, foo2, foo1);
42         assertFindModules(expected, "foo", foo1, foo0, foo2);
43         assertFindModules(expected, "foo", foo1, foo2, foo0);
44         assertFindModules(expected, "foo", foo2, foo0, foo1);
45         assertFindModules(expected, "foo", foo2, foo1, foo0);
46
47         final URI uri = URI.create("foo");
48         assertFindModules(expected, uri, foo0, foo1, foo2);
49         assertFindModules(expected, uri, foo0, foo2, foo1);
50         assertFindModules(expected, uri, foo1, foo0, foo2);
51         assertFindModules(expected, uri, foo1, foo2, foo0);
52         assertFindModules(expected, uri, foo2, foo0, foo1);
53         assertFindModules(expected, uri, foo2, foo1, foo0);
54     }
55
56     private static void assertGetModules(final List<Module> expected, final Module... modules) {
57         final Set<Module> actual = SimpleSchemaContext.forModules(ImmutableSet.copyOf(modules)).getModules();
58         assertArrayEquals(expected.toArray(), actual.toArray());
59     }
60
61     private static void assertFindModules(final List<Module> expected, final String name, final Module... modules) {
62         final Set<Module> actual = SimpleSchemaContext.forModules(ImmutableSet.copyOf(modules)).findModules(name);
63         assertArrayEquals(expected.toArray(), actual.toArray());
64     }
65
66     private static void assertFindModules(final List<Module> expected, final URI uri, final Module... modules) {
67         final Set<Module> actual = SimpleSchemaContext.forModules(ImmutableSet.copyOf(modules)).findModules(uri);
68         assertArrayEquals(expected.toArray(), actual.toArray());
69     }
70
71     private static Module mockModule(final String name, final Optional<Revision> revision) {
72         final QNameModule mod = QNameModule.create(URI.create(name), revision);
73         final Module ret = mock(Module.class);
74         doReturn(name).when(ret).getName();
75         doReturn(mod.getNamespace()).when(ret).getNamespace();
76         doReturn(mod.getRevision()).when(ret).getRevision();
77         doReturn(mod).when(ret).getQNameModule();
78         doReturn(mod.toString()).when(ret).toString();
79         doReturn(ImmutableSet.of()).when(ret).getImports();
80         return ret;
81     }
82 }