Promote SchemaSourceRepresentation
[yangtools.git] / yang / yang-repo-spi / src / test / java / org / opendaylight / yangtools / yang / model / repo / spi / SoftSchemaSourceCacheTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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.repo.spi;
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 import static org.mockito.ArgumentMatchers.any;
14 import static org.mockito.Mockito.doNothing;
15 import static org.mockito.Mockito.doReturn;
16
17 import com.google.common.base.MoreObjects.ToStringHelper;
18 import java.io.StringReader;
19 import java.util.concurrent.ExecutionException;
20 import org.junit.jupiter.api.Test;
21 import org.junit.jupiter.api.extension.ExtendWith;
22 import org.mockito.Mock;
23 import org.mockito.junit.jupiter.MockitoExtension;
24 import org.opendaylight.yangtools.concepts.Registration;
25 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
26 import org.opendaylight.yangtools.yang.model.api.source.YangSourceRepresentation;
27 import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
28
29 @ExtendWith(MockitoExtension.class)
30 class SoftSchemaSourceCacheTest {
31     private static final Class<YangSourceRepresentation> REPRESENTATION = YangSourceRepresentation.class;
32
33     @Mock
34     private SchemaSourceRegistry registry;
35     @Mock
36     private Registration registration;
37
38     @Test
39     void inMemorySchemaSourceCacheTest() {
40         try (var cache = new SoftSchemaSourceCache<>(registry, REPRESENTATION)) {
41             assertNotNull(cache);
42         }
43     }
44
45     @Test
46     void inMemorySchemaSourceCacheOfferAndGetSourcestest() throws Exception {
47         doNothing().when(registration).close();
48         doReturn(registration).when(registry).registerSchemaSource(any(), any());
49
50         try (var cache = new SoftSchemaSourceCache<>(registry, REPRESENTATION)) {
51             final var content = "content";
52             final var source = new TestingYangSource("test", "2012-12-12", content);
53             cache.offer(source);
54             final var sourceIdentifier = new SourceIdentifier("test", "2012-12-12");
55             final var checkedSource = cache .getSource(sourceIdentifier);
56             assertNotNull(checkedSource);
57             final var yangSchemaSourceRepresentation = checkedSource.get();
58             assertNotNull(yangSchemaSourceRepresentation);
59             assertEquals(sourceIdentifier, yangSchemaSourceRepresentation.sourceId());
60         }
61     }
62
63     @Test
64     void inMemorySchemaSourceCacheNullGetSourcestest() throws Exception {
65         try (var cache = new SoftSchemaSourceCache<>(registry, REPRESENTATION)) {
66             final var sourceIdentifier = new SourceIdentifier("test", "2012-12-12");
67             final var checkedSource = cache.getSource(sourceIdentifier);
68             assertNotNull(checkedSource);
69             assertThrows(ExecutionException.class, checkedSource::get);
70         }
71     }
72
73     @Test
74     void inMemorySchemaSourceCache3test() throws InterruptedException, ExecutionException {
75         doNothing().when(registration).close();
76         doReturn(registration).when(registry).registerSchemaSource(any(), any());
77
78         try (var cache1 = new SoftSchemaSourceCache<>(registry, REPRESENTATION)) {
79             try (var cache2 = new SoftSchemaSourceCache<>(registry, REPRESENTATION)) {
80                 final var content = "content";
81                 final var source = new TestingYangSource("test", "2012-12-12", content);
82                 cache1.offer(source);
83                 cache2.offer(source);
84
85                 final var sourceIdentifier = new SourceIdentifier("test", "2012-12-12");
86                 final var checkedSource = cache1.getSource(sourceIdentifier);
87                 final var checkedSource2 = cache2.getSource(sourceIdentifier);
88                 assertNotNull(checkedSource);
89                 assertNotNull(checkedSource2);
90
91                 assertEquals(checkedSource.get(), checkedSource2.get());
92             }
93         }
94     }
95
96     private static class TestingYangSource extends YangTextSource {
97         private final String content;
98
99         TestingYangSource(final String name, final String revision, final String content) {
100             super(new SourceIdentifier(name, revision));
101             this.content = content;
102         }
103
104         @Override
105         public StringReader openStream() {
106             return new StringReader(content);
107         }
108
109         @Override
110         public String symbolicName() {
111             return null;
112         }
113
114         @Override
115         protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
116             return toStringHelper;
117         }
118     }
119 }