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