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.model.repo.api.SourceIdentifier;
30 import org.opendaylight.yangtools.yang.model.repo.api.YangSchemaSourceRepresentation;
31 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
33 @RunWith(MockitoJUnitRunner.StrictStubs.class)
34 public class SoftSchemaSourceCacheTest {
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 inMemorySchemaSourceCacheTest() {
53 try (var cache = new SoftSchemaSourceCache<>(registry, REPRESENTATION)) {
59 public void inMemorySchemaSourceCacheOfferAndGetSourcestest() throws Exception {
60 try (var cache = new SoftSchemaSourceCache<>(registry, REPRESENTATION)) {
61 final String content = "content";
62 final YangTextSchemaSource source = new TestingYangSource("test", "2012-12-12", content);
64 final var sourceIdentifier = new SourceIdentifier("test", "2012-12-12");
65 final var checkedSource = cache .getSource(sourceIdentifier);
66 assertNotNull(checkedSource);
67 final var yangSchemaSourceRepresentation = checkedSource.get();
68 assertNotNull(yangSchemaSourceRepresentation);
69 assertEquals(sourceIdentifier, yangSchemaSourceRepresentation.getIdentifier());
74 public void inMemorySchemaSourceCacheNullGetSourcestest() throws Exception {
75 try (var cache = new SoftSchemaSourceCache<>(registry, REPRESENTATION)) {
76 final var sourceIdentifier = new SourceIdentifier("test", "2012-12-12");
77 final var checkedSource = cache.getSource(sourceIdentifier);
78 assertNotNull(checkedSource);
79 assertThrows(ExecutionException.class, () -> checkedSource.get());
84 public void inMemorySchemaSourceCache3test() throws InterruptedException, ExecutionException {
85 try (var cache1 = new SoftSchemaSourceCache<>(registry, REPRESENTATION)) {
86 try (var cache2 = new SoftSchemaSourceCache<>(registry, REPRESENTATION)) {
87 final String content = "content";
88 final YangTextSchemaSource source = new TestingYangSource("test", "2012-12-12", content);
92 final var sourceIdentifier = new SourceIdentifier("test", "2012-12-12");
93 final var checkedSource = cache1.getSource(sourceIdentifier);
94 final var checkedSource2 = cache2.getSource(sourceIdentifier);
95 assertNotNull(checkedSource);
96 assertNotNull(checkedSource2);
98 assertEquals(checkedSource.get(), checkedSource2.get());
103 private static class TestingYangSource extends YangTextSchemaSource {
104 private final String content;
106 TestingYangSource(final String name, final String revision, final String content) {
107 super(new SourceIdentifier(name, revision));
108 this.content = content;
112 public InputStream openStream() {
113 return new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
117 public Optional<String> getSymbolicName() {
118 return Optional.empty();
122 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
123 return toStringHelper;