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;
35 @RunWith(MockitoJUnitRunner.StrictStubs.class)
36 public class GuavaSchemaSourceCacheTest {
37 public static final Class<YangSchemaSourceRepresentation> REPRESENTATION = YangSchemaSourceRepresentation.class;
38 public static final long LIFETIME = 1000L;
39 public static final TimeUnit UNITS = TimeUnit.MILLISECONDS;
42 public SchemaSourceRegistry registry;
44 public SchemaSourceRegistration<?> registration;
48 doNothing().when(registration).close();
49 doReturn(registration).when(registry).registerSchemaSource(any(SchemaSourceProvider.class),
50 any(PotentialSchemaSource.class));
54 public void inMemorySchemaSourceCacheTest1() {
55 try (var cache = GuavaSchemaSourceCache.createSoftCache(registry, REPRESENTATION)) {
61 public void inMemorySchemaSourceCacheTest2() {
62 try (var cache = GuavaSchemaSourceCache.createSoftCache(registry, REPRESENTATION, LIFETIME, UNITS)) {
68 public void inMemorySchemaSourceCacheOfferAndGetSourcestest() throws Exception {
69 try (var cache = GuavaSchemaSourceCache.createSoftCache(registry, REPRESENTATION)) {
70 final String content = "content";
71 final YangTextSchemaSource source = new TestingYangSource("test", "2012-12-12", content);
73 final var sourceIdentifier = RevisionSourceIdentifier.create("test", Revision.of("2012-12-12"));
74 final var checkedSource = cache .getSource(sourceIdentifier);
75 assertNotNull(checkedSource);
76 final var yangSchemaSourceRepresentation = checkedSource.get();
77 assertNotNull(yangSchemaSourceRepresentation);
78 assertEquals(sourceIdentifier, yangSchemaSourceRepresentation.getIdentifier());
83 public void inMemorySchemaSourceCacheNullGetSourcestest() throws Exception {
84 try (var cache = GuavaSchemaSourceCache.createSoftCache(registry, REPRESENTATION)) {
85 final var sourceIdentifier = RevisionSourceIdentifier.create("test", Revision.of("2012-12-12"));
86 final var checkedSource = cache.getSource(sourceIdentifier);
87 assertNotNull(checkedSource);
88 assertThrows(ExecutionException.class, () -> checkedSource.get());
93 public void inMemorySchemaSourceCache3test() throws InterruptedException, ExecutionException {
94 try (var cache1 = GuavaSchemaSourceCache.createSoftCache(registry, REPRESENTATION)) {
95 try (var cache2 = GuavaSchemaSourceCache.createSoftCache(registry, REPRESENTATION, LIFETIME, UNITS)) {
96 final String content = "content";
97 final YangTextSchemaSource source = new TestingYangSource("test", "2012-12-12", content);
101 final var sourceIdentifier = RevisionSourceIdentifier.create("test", Revision.of("2012-12-12"));
102 final var checkedSource = cache1.getSource(sourceIdentifier);
103 final var checkedSource2 = cache2.getSource(sourceIdentifier);
104 assertNotNull(checkedSource);
105 assertNotNull(checkedSource2);
107 assertEquals(checkedSource.get(), checkedSource2.get());
112 private static class TestingYangSource extends YangTextSchemaSource {
113 private final String content;
115 TestingYangSource(final String name, final String revision, final String content) {
116 super(RevisionSourceIdentifier.create(name, Revision.ofNullable(revision)));
117 this.content = content;
121 public InputStream openStream() {
122 return new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
126 public Optional<String> getSymbolicName() {
127 return Optional.empty();
131 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
132 return toStringHelper;