e699da45ef08a3abd319fdbad52bd1c99eb61f2e
[yangtools.git] / yang / yang-repo-spi / src / test / java / org / opendaylight / yangtools / yang / model / repo / spi / SoftSchemaSourceCacheTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.model.repo.spi;
9
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;
16
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;
33
34 @RunWith(MockitoJUnitRunner.StrictStubs.class)
35 public class SoftSchemaSourceCacheTest {
36     public static final Class<YangSchemaSourceRepresentation> REPRESENTATION = YangSchemaSourceRepresentation.class;
37     public static final long LIFETIME = 1000L;
38     public static final TimeUnit UNITS = TimeUnit.MILLISECONDS;
39
40     @Mock
41     public SchemaSourceRegistry registry;
42     @Mock
43     public SchemaSourceRegistration<?> registration;
44
45     @Before
46     public void setUp() {
47         doNothing().when(registration).close();
48         doReturn(registration).when(registry).registerSchemaSource(any(SchemaSourceProvider.class),
49             any(PotentialSchemaSource.class));
50     }
51
52     @Test
53     public void inMemorySchemaSourceCacheTest() {
54         try (var cache = new SoftSchemaSourceCache<>(registry, REPRESENTATION)) {
55             assertNotNull(cache);
56         }
57     }
58
59     @Test
60     public void inMemorySchemaSourceCacheOfferAndGetSourcestest() throws Exception {
61         try (var cache = new SoftSchemaSourceCache<>(registry, REPRESENTATION)) {
62             final String content = "content";
63             final YangTextSchemaSource source = new TestingYangSource("test", "2012-12-12", content);
64             cache.offer(source);
65             final var sourceIdentifier = RevisionSourceIdentifier.create("test", Revision.of("2012-12-12"));
66             final var checkedSource = cache .getSource(sourceIdentifier);
67             assertNotNull(checkedSource);
68             final var yangSchemaSourceRepresentation = checkedSource.get();
69             assertNotNull(yangSchemaSourceRepresentation);
70             assertEquals(sourceIdentifier, yangSchemaSourceRepresentation.getIdentifier());
71         }
72     }
73
74     @Test
75     public void inMemorySchemaSourceCacheNullGetSourcestest() throws Exception {
76         try (var cache = new SoftSchemaSourceCache<>(registry, REPRESENTATION)) {
77             final var sourceIdentifier = RevisionSourceIdentifier.create("test", Revision.of("2012-12-12"));
78             final var checkedSource = cache.getSource(sourceIdentifier);
79             assertNotNull(checkedSource);
80             assertThrows(ExecutionException.class, () -> checkedSource.get());
81         }
82     }
83
84     @Test
85     public void inMemorySchemaSourceCache3test() throws InterruptedException, ExecutionException {
86         try (var cache1 = new SoftSchemaSourceCache<>(registry, REPRESENTATION)) {
87             try (var cache2 = new SoftSchemaSourceCache<>(registry, REPRESENTATION)) {
88                 final String content = "content";
89                 final YangTextSchemaSource source = new TestingYangSource("test", "2012-12-12", content);
90                 cache1.offer(source);
91                 cache2.offer(source);
92
93                 final var sourceIdentifier = RevisionSourceIdentifier.create("test", Revision.of("2012-12-12"));
94                 final var checkedSource = cache1.getSource(sourceIdentifier);
95                 final var checkedSource2 = cache2.getSource(sourceIdentifier);
96                 assertNotNull(checkedSource);
97                 assertNotNull(checkedSource2);
98
99                 assertEquals(checkedSource.get(), checkedSource2.get());
100             }
101         }
102     }
103
104     private static class TestingYangSource extends YangTextSchemaSource {
105         private final String content;
106
107         TestingYangSource(final String name, final String revision, final String content) {
108             super(RevisionSourceIdentifier.create(name, Revision.ofNullable(revision)));
109             this.content = content;
110         }
111
112         @Override
113         public InputStream openStream() {
114             return new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
115         }
116
117         @Override
118         public Optional<String> getSymbolicName() {
119             return Optional.empty();
120         }
121
122         @Override
123         protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
124             return toStringHelper;
125         }
126     }
127 }