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 org.junit.jupiter.api.Test;
21 import org.junit.jupiter.api.extension.ExtendWith;
22 import org.mockito.Mock;
23 import org.mockito.junit.jupiter.MockitoExtension;
24 import org.opendaylight.yangtools.concepts.Registration;
25 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
26 import org.opendaylight.yangtools.yang.model.api.source.YangSourceRepresentation;
27 import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
29 @ExtendWith(MockitoExtension.class)
30 class SoftSchemaSourceCacheTest {
31 private static final Class<YangSourceRepresentation> REPRESENTATION = YangSourceRepresentation.class;
34 private SchemaSourceRegistry registry;
36 private Registration registration;
39 void inMemorySchemaSourceCacheTest() {
40 try (var cache = new SoftSchemaSourceCache<>(registry, REPRESENTATION)) {
46 void inMemorySchemaSourceCacheOfferAndGetSourcestest() throws Exception {
47 doNothing().when(registration).close();
48 doReturn(registration).when(registry).registerSchemaSource(any(), any());
50 try (var cache = new SoftSchemaSourceCache<>(registry, REPRESENTATION)) {
51 final var content = "content";
52 final var source = new TestingYangSource("test", "2012-12-12", content);
54 final var sourceIdentifier = new SourceIdentifier("test", "2012-12-12");
55 final var checkedSource = cache .getSource(sourceIdentifier);
56 assertNotNull(checkedSource);
57 final var yangSchemaSourceRepresentation = checkedSource.get();
58 assertNotNull(yangSchemaSourceRepresentation);
59 assertEquals(sourceIdentifier, yangSchemaSourceRepresentation.sourceId());
64 void inMemorySchemaSourceCacheNullGetSourcestest() throws Exception {
65 try (var cache = new SoftSchemaSourceCache<>(registry, REPRESENTATION)) {
66 final var sourceIdentifier = new SourceIdentifier("test", "2012-12-12");
67 final var checkedSource = cache.getSource(sourceIdentifier);
68 assertNotNull(checkedSource);
69 assertThrows(ExecutionException.class, checkedSource::get);
74 void inMemorySchemaSourceCache3test() throws InterruptedException, ExecutionException {
75 doNothing().when(registration).close();
76 doReturn(registration).when(registry).registerSchemaSource(any(), any());
78 try (var cache1 = new SoftSchemaSourceCache<>(registry, REPRESENTATION)) {
79 try (var cache2 = new SoftSchemaSourceCache<>(registry, REPRESENTATION)) {
80 final var content = "content";
81 final var source = new TestingYangSource("test", "2012-12-12", content);
85 final var sourceIdentifier = new SourceIdentifier("test", "2012-12-12");
86 final var checkedSource = cache1.getSource(sourceIdentifier);
87 final var checkedSource2 = cache2.getSource(sourceIdentifier);
88 assertNotNull(checkedSource);
89 assertNotNull(checkedSource2);
91 assertEquals(checkedSource.get(), checkedSource2.get());
96 private static class TestingYangSource extends YangTextSource {
97 private final String content;
99 TestingYangSource(final String name, final String revision, final String content) {
100 super(new SourceIdentifier(name, revision));
101 this.content = content;
105 public StringReader openStream() {
106 return new StringReader(content);
110 public String symbolicName() {
115 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
116 return toStringHelper;