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;
32 @RunWith(MockitoJUnitRunner.StrictStubs.class)
33 public class SoftSchemaSourceCacheTest {
34 public static final Class<YangSchemaSourceRepresentation> REPRESENTATION = YangSchemaSourceRepresentation.class;
35 public static final long LIFETIME = 1000L;
36 public static final TimeUnit UNITS = TimeUnit.MILLISECONDS;
39 public SchemaSourceRegistry registry;
41 public SchemaSourceRegistration<?> registration;
45 doNothing().when(registration).close();
46 doReturn(registration).when(registry).registerSchemaSource(any(SchemaSourceProvider.class),
47 any(PotentialSchemaSource.class));
51 public void inMemorySchemaSourceCacheTest() {
52 try (var cache = new SoftSchemaSourceCache<>(registry, REPRESENTATION)) {
58 public void inMemorySchemaSourceCacheOfferAndGetSourcestest() throws Exception {
59 try (var cache = new SoftSchemaSourceCache<>(registry, REPRESENTATION)) {
60 final String content = "content";
61 final YangTextSchemaSource source = new TestingYangSource("test", "2012-12-12", content);
63 final var sourceIdentifier = new SourceIdentifier("test", "2012-12-12");
64 final var checkedSource = cache .getSource(sourceIdentifier);
65 assertNotNull(checkedSource);
66 final var yangSchemaSourceRepresentation = checkedSource.get();
67 assertNotNull(yangSchemaSourceRepresentation);
68 assertEquals(sourceIdentifier, yangSchemaSourceRepresentation.getIdentifier());
73 public void inMemorySchemaSourceCacheNullGetSourcestest() throws Exception {
74 try (var cache = new SoftSchemaSourceCache<>(registry, REPRESENTATION)) {
75 final var sourceIdentifier = new SourceIdentifier("test", "2012-12-12");
76 final var checkedSource = cache.getSource(sourceIdentifier);
77 assertNotNull(checkedSource);
78 assertThrows(ExecutionException.class, () -> checkedSource.get());
83 public void inMemorySchemaSourceCache3test() throws InterruptedException, ExecutionException {
84 try (var cache1 = new SoftSchemaSourceCache<>(registry, REPRESENTATION)) {
85 try (var cache2 = new SoftSchemaSourceCache<>(registry, REPRESENTATION)) {
86 final String content = "content";
87 final YangTextSchemaSource source = new TestingYangSource("test", "2012-12-12", content);
91 final var sourceIdentifier = new SourceIdentifier("test", "2012-12-12");
92 final var checkedSource = cache1.getSource(sourceIdentifier);
93 final var checkedSource2 = cache2.getSource(sourceIdentifier);
94 assertNotNull(checkedSource);
95 assertNotNull(checkedSource2);
97 assertEquals(checkedSource.get(), checkedSource2.get());
102 private static class TestingYangSource extends YangTextSchemaSource {
103 private final String content;
105 TestingYangSource(final String name, final String revision, final String content) {
106 super(new SourceIdentifier(name, revision));
107 this.content = content;
111 public Reader openStream() {
112 return new StringReader(content);
116 public Optional<String> getSymbolicName() {
117 return Optional.empty();
121 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
122 return toStringHelper;