2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.model.repo.spi;
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;
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;
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;
40 public SchemaSourceRegistry registry;
42 public SchemaSourceRegistration<?> registration;
46 doNothing().when(registration).close();
47 doReturn(registration).when(registry).registerSchemaSource(any(SchemaSourceProvider.class),
48 any(PotentialSchemaSource.class));
52 public void inMemorySchemaSourceCacheTest1() {
53 try (var cache = GuavaSchemaSourceCache.createSoftCache(registry, REPRESENTATION)) {
59 public void inMemorySchemaSourceCacheTest2() {
60 try (var cache = GuavaSchemaSourceCache.createSoftCache(registry, REPRESENTATION, LIFETIME, UNITS)) {
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);
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());
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());
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);
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);
105 assertEquals(checkedSource.get(), checkedSource2.get());
110 private static class TestingYangSource extends YangTextSchemaSource {
111 private final String content;
113 TestingYangSource(final String name, final String revision, final String content) {
114 super(new SourceIdentifier(name, revision));
115 this.content = content;
119 public Reader openStream() {
120 return new StringReader(content);
124 public Optional<String> getSymbolicName() {
125 return Optional.empty();
129 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
130 return toStringHelper;