604143db50285af2dcf861c3bc730a173aa8a4d8
[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.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 @RunWith(MockitoJUnitRunner.StrictStubs.class)
33 public class SoftSchemaSourceCacheTest {
34     public static final Class<YangSchemaSourceRepresentation> REPRESENTATION = YangSchemaSourceRepresentation.class;
35     public static final long LIFETIME = 1000L;
36     public static final TimeUnit UNITS = TimeUnit.MILLISECONDS;
37
38     @Mock
39     public SchemaSourceRegistry registry;
40     @Mock
41     public SchemaSourceRegistration<?> registration;
42
43     @Before
44     public void setUp() {
45         doNothing().when(registration).close();
46         doReturn(registration).when(registry).registerSchemaSource(any(SchemaSourceProvider.class),
47             any(PotentialSchemaSource.class));
48     }
49
50     @Test
51     public void inMemorySchemaSourceCacheTest() {
52         try (var cache = new SoftSchemaSourceCache<>(registry, REPRESENTATION)) {
53             assertNotNull(cache);
54         }
55     }
56
57     @Test
58     public void inMemorySchemaSourceCacheOfferAndGetSourcestest() throws Exception {
59         try (var cache = new SoftSchemaSourceCache<>(registry, REPRESENTATION)) {
60             final String content = "content";
61             final YangTextSchemaSource source = new TestingYangSource("test", "2012-12-12", content);
62             cache.offer(source);
63             final var sourceIdentifier = new SourceIdentifier("test", "2012-12-12");
64             final var checkedSource = cache .getSource(sourceIdentifier);
65             assertNotNull(checkedSource);
66             final var yangSchemaSourceRepresentation = checkedSource.get();
67             assertNotNull(yangSchemaSourceRepresentation);
68             assertEquals(sourceIdentifier, yangSchemaSourceRepresentation.getIdentifier());
69         }
70     }
71
72     @Test
73     public void inMemorySchemaSourceCacheNullGetSourcestest() throws Exception {
74         try (var cache = new SoftSchemaSourceCache<>(registry, REPRESENTATION)) {
75             final var sourceIdentifier = new SourceIdentifier("test", "2012-12-12");
76             final var checkedSource = cache.getSource(sourceIdentifier);
77             assertNotNull(checkedSource);
78             assertThrows(ExecutionException.class, () -> checkedSource.get());
79         }
80     }
81
82     @Test
83     public void inMemorySchemaSourceCache3test() throws InterruptedException, ExecutionException {
84         try (var cache1 = new SoftSchemaSourceCache<>(registry, REPRESENTATION)) {
85             try (var cache2 = new SoftSchemaSourceCache<>(registry, REPRESENTATION)) {
86                 final String content = "content";
87                 final YangTextSchemaSource source = new TestingYangSource("test", "2012-12-12", content);
88                 cache1.offer(source);
89                 cache2.offer(source);
90
91                 final var sourceIdentifier = new SourceIdentifier("test", "2012-12-12");
92                 final var checkedSource = cache1.getSource(sourceIdentifier);
93                 final var checkedSource2 = cache2.getSource(sourceIdentifier);
94                 assertNotNull(checkedSource);
95                 assertNotNull(checkedSource2);
96
97                 assertEquals(checkedSource.get(), checkedSource2.get());
98             }
99         }
100     }
101
102     private static class TestingYangSource extends YangTextSchemaSource {
103         private final String content;
104
105         TestingYangSource(final String name, final String revision, final String content) {
106             super(new SourceIdentifier(name, revision));
107             this.content = content;
108         }
109
110         @Override
111         public Reader openStream() {
112             return new StringReader(content);
113         }
114
115         @Override
116         public Optional<String> getSymbolicName() {
117             return Optional.empty();
118         }
119
120         @Override
121         protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
122             return toStringHelper;
123         }
124     }
125 }