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