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