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.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;
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;
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;
38 public SchemaSourceRegistry registry;
40 public Registration registration;
43 void inMemorySchemaSourceCacheTest1() {
44 try (var cache = GuavaSchemaSourceCache.createSoftCache(registry, REPRESENTATION)) {
50 void inMemorySchemaSourceCacheTest2() {
51 try (var cache = GuavaSchemaSourceCache.createSoftCache(registry, REPRESENTATION, LIFETIME, UNITS)) {
57 void inMemorySchemaSourceCacheOfferAndGetSourcestest() throws Exception {
58 doNothing().when(registration).close();
59 doReturn(registration).when(registry).registerSchemaSource(any(), any());
61 try (var cache = GuavaSchemaSourceCache.createSoftCache(registry, REPRESENTATION)) {
62 final var content = "content";
63 final var source = new TestingYangSource("test", "2012-12-12", content);
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());
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);
85 void inMemorySchemaSourceCache3test() throws InterruptedException, ExecutionException {
86 doNothing().when(registration).close();
87 doReturn(registration).when(registry).registerSchemaSource(any(), any());
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);
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);
102 assertEquals(checkedSource.get(), checkedSource2.get());
107 private static class TestingYangSource extends YangTextSource {
108 private final String content;
110 TestingYangSource(final String name, final String revision, final String content) {
111 super(new SourceIdentifier(name, revision));
112 this.content = content;
116 public StringReader openStream() {
117 return new StringReader(content);
121 public String symbolicName() {
126 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
127 return toStringHelper;