Remove RevisionSourceIdentifier
[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.model.repo.api.SourceIdentifier;
30 import org.opendaylight.yangtools.yang.model.repo.api.YangSchemaSourceRepresentation;
31 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
32
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;
38
39     @Mock
40     public SchemaSourceRegistry registry;
41     @Mock
42     public SchemaSourceRegistration<?> registration;
43
44     @Before
45     public void setUp() {
46         doNothing().when(registration).close();
47         doReturn(registration).when(registry).registerSchemaSource(any(SchemaSourceProvider.class),
48             any(PotentialSchemaSource.class));
49     }
50
51     @Test
52     public void inMemorySchemaSourceCacheTest() {
53         try (var cache = new SoftSchemaSourceCache<>(registry, REPRESENTATION)) {
54             assertNotNull(cache);
55         }
56     }
57
58     @Test
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);
63             cache.offer(source);
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());
70         }
71     }
72
73     @Test
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());
80         }
81     }
82
83     @Test
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);
89                 cache1.offer(source);
90                 cache2.offer(source);
91
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);
97
98                 assertEquals(checkedSource.get(), checkedSource2.get());
99             }
100         }
101     }
102
103     private static class TestingYangSource extends YangTextSchemaSource {
104         private final String content;
105
106         TestingYangSource(final String name, final String revision, final String content) {
107             super(new SourceIdentifier(name, revision));
108             this.content = content;
109         }
110
111         @Override
112         public InputStream openStream() {
113             return new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
114         }
115
116         @Override
117         public Optional<String> getSymbolicName() {
118             return Optional.empty();
119         }
120
121         @Override
122         protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
123             return toStringHelper;
124         }
125     }
126 }