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.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;
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;
41 public SchemaSourceRegistry registry;
43 public SchemaSourceRegistration<?> registration;
47 doNothing().when(registration).close();
48 doReturn(registration).when(registry).registerSchemaSource(any(SchemaSourceProvider.class),
49 any(PotentialSchemaSource.class));
53 public void inMemorySchemaSourceCacheTest1() {
54 try (var cache = GuavaSchemaSourceCache.createSoftCache(registry, REPRESENTATION)) {
60 public void inMemorySchemaSourceCacheTest2() {
61 try (var cache = GuavaSchemaSourceCache.createSoftCache(registry, REPRESENTATION, LIFETIME, UNITS)) {
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);
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());
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());
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);
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);
106 assertEquals(checkedSource.get(), checkedSource2.get());
111 private static class TestingYangSource extends YangTextSchemaSource {
112 private final String content;
114 TestingYangSource(final String name, final String revision, final String content) {
115 super(RevisionSourceIdentifier.create(name, Revision.ofNullable(revision)));
116 this.content = content;
120 public InputStream openStream() {
121 return new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
125 public Optional<String> getSymbolicName() {
126 return Optional.empty();
130 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
131 return toStringHelper;