2 * Copyright (c) 2016 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package io.fd.honeycomb.translate.util.read.cache;
19 import static io.fd.honeycomb.translate.util.read.cache.EntityDumpExecutor.NO_PARAMS;
20 import static org.junit.Assert.assertEquals;
21 import static org.mockito.Mockito.when;
23 import com.google.common.base.Optional;
24 import io.fd.honeycomb.translate.ModificationCache;
25 import io.fd.honeycomb.translate.read.ReadFailedException;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Objects;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.mockito.Mock;
32 import org.mockito.MockitoAnnotations;
33 import org.opendaylight.yangtools.yang.binding.DataObject;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 public class DumpCacheManagerTest {
38 private interface DataObj extends DataObject {}
41 private EntityDumpExecutor<IpDetailsReplyDump, Void> executor;
43 private InstanceIdentifier<DataObj> identifier;
45 private DumpCacheManager<IpDetailsReplyDump, Void> managerPositive;
46 private DumpCacheManager<IpDetailsReplyDump, Void> managerPositiveWithPostProcessing;
47 private DumpCacheManager<IpDetailsReplyDump, Void> managerNegative;
48 private ModificationCache cache;
49 private CacheKeyFactory cacheKeyFactory;
53 MockitoAnnotations.initMocks(this);
55 new DumpCacheManager.DumpCacheManagerBuilder<IpDetailsReplyDump, Void>()
56 .withExecutor(executor)
59 managerPositiveWithPostProcessing =
60 new DumpCacheManager.DumpCacheManagerBuilder<IpDetailsReplyDump, Void>()
61 .withExecutor(executor)
62 .withPostProcessingFunction(createPostProcessor())
66 new DumpCacheManager.DumpCacheManagerBuilder<IpDetailsReplyDump, Void>()
67 .withExecutor(executor)
70 cache = new ModificationCache();
71 identifier = InstanceIdentifier.create(DataObj.class);
72 //manager uses this implementation by default, so it can be used to test behaviour
73 cacheKeyFactory = new IdentifierCacheKeyFactory();
78 * This test verify full dump-caching cycle
81 public void testCaching() throws ReadFailedException {
82 final IpDetailsReplyDump stage1Data = new IpDetailsReplyDump();
83 final String key = cacheKeyFactory.createKey(identifier);
86 // executor cant return null data
87 when(executor.executeDump(identifier, NO_PARAMS)).thenReturn(new IpDetailsReplyDump());
89 final Optional<IpDetailsReplyDump> stage1Optional = managerNegative.getDump(identifier, cache, NO_PARAMS);
91 // this is first call so instance should be from executor
92 // and it should be cached after calling executor
93 assertEquals(true, stage1Optional.isPresent());
94 assertEquals(stage1Data, stage1Optional.get());
95 assertEquals(true, cache.containsKey(key));
96 assertEquals(stage1Data, cache.get(key));
98 //rebind executor with other data
99 IpDetailsReplyDump stage2LoadedDump = new IpDetailsReplyDump();
100 when(executor.executeDump(identifier, NO_PARAMS)).thenReturn(stage2LoadedDump);
102 final Optional<IpDetailsReplyDump> stage2Optional = managerPositive.getDump(identifier, cache, NO_PARAMS);
104 assertEquals(true, stage2Optional.isPresent());
105 assertEquals(stage2LoadedDump, stage2Optional.get());
107 //rebind executor with other data
108 IpDetailsReplyDump stage3LoadedDump = new IpDetailsReplyDump();
109 when(executor.executeDump(identifier, NO_PARAMS)).thenReturn(stage3LoadedDump);
111 final Optional<IpDetailsReplyDump> stage3Optional = managerPositive.getDump(identifier, cache, NO_PARAMS);
112 assertEquals(true, stage3Optional.isPresent());
113 //check if it returns instance cached from previous stage
114 assertEquals(stage2LoadedDump, stage3Optional.get());
118 public void testPostprocessing() throws ReadFailedException {
119 IpDetailsReplyDump dump = new IpDetailsReplyDump();
120 IpDetails details = new IpDetails();
121 details.swIfIndex = 2;
122 dump.ipDetails.add(details);
124 when(executor.executeDump(identifier, null)).thenReturn(dump);
126 Optional<IpDetailsReplyDump> optionalDump =
127 managerPositiveWithPostProcessing.getDump(identifier, cache, NO_PARAMS);
129 assertEquals(true, optionalDump.isPresent());
130 assertEquals(1, optionalDump.get().ipDetails.size());
131 assertEquals(7, optionalDump.get().ipDetails.get(0).swIfIndex);
134 private EntityDumpPostProcessingFunction<IpDetailsReplyDump> createPostProcessor() {
135 return ipDetailsReplyDump -> {
136 IpDetailsReplyDump modified = new IpDetailsReplyDump();
138 for (IpDetails detail : ipDetailsReplyDump.ipDetails) {
139 IpDetails modifiedDetail = new IpDetails();
140 modifiedDetail.swIfIndex = detail.swIfIndex + 5;
142 modified.ipDetails.add(modifiedDetail);
149 private static final class IpDetailsReplyDump {
150 List<IpDetails> ipDetails = new ArrayList<>();
153 public boolean equals(final Object o) {
157 if (o == null || getClass() != o.getClass()) {
160 final IpDetailsReplyDump that = (IpDetailsReplyDump) o;
161 return Objects.equals(ipDetails, that.ipDetails);
165 public int hashCode() {
166 return Objects.hash(ipDetails);
170 private static final class IpDetails {