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.vpp.util.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 io.fd.honeycomb.translate.util.read.cache.CacheKeyFactory;
27 import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager;
28 import io.fd.honeycomb.translate.util.read.cache.EntityDumpExecutor;
29 import io.fd.honeycomb.translate.util.read.cache.EntityDumpPostProcessingFunction;
30 import io.fd.honeycomb.translate.util.read.cache.IdentifierCacheKeyFactory;
31 import io.fd.vpp.jvpp.core.dto.IpDetails;
32 import io.fd.vpp.jvpp.core.dto.IpDetailsReplyDump;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.opendaylight.yangtools.yang.binding.DataObject;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 public class DumpCacheManagerTest {
42 private interface DataObj extends DataObject {}
45 private EntityDumpExecutor<IpDetailsReplyDump, Void> executor;
47 private InstanceIdentifier<DataObj> identifier;
49 private DumpCacheManager<IpDetailsReplyDump, Void> managerPositive;
50 private DumpCacheManager<IpDetailsReplyDump, Void> managerPositiveWithPostProcessing;
51 private DumpCacheManager<IpDetailsReplyDump, Void> managerNegative;
52 private ModificationCache cache;
53 private CacheKeyFactory cacheKeyFactory;
57 MockitoAnnotations.initMocks(this);
59 new DumpCacheManager.DumpCacheManagerBuilder<IpDetailsReplyDump, Void>()
60 .withExecutor(executor)
63 managerPositiveWithPostProcessing =
64 new DumpCacheManager.DumpCacheManagerBuilder<IpDetailsReplyDump, Void>()
65 .withExecutor(executor)
66 .withPostProcessingFunction(createPostProcessor())
70 new DumpCacheManager.DumpCacheManagerBuilder<IpDetailsReplyDump, Void>()
71 .withExecutor(executor)
74 cache = new ModificationCache();
75 identifier = InstanceIdentifier.create(DataObj.class);
76 //manager uses this implementation by default, so it can be used to test behaviour
77 cacheKeyFactory = new IdentifierCacheKeyFactory();
82 * This test verify full dump-caching cycle
85 public void testCaching() throws ReadFailedException {
86 final IpDetailsReplyDump stage1Data = new IpDetailsReplyDump();
87 final String key = cacheKeyFactory.createKey(identifier);
90 // executor cant return null data
91 when(executor.executeDump(identifier, NO_PARAMS)).thenReturn(new IpDetailsReplyDump());
93 final Optional<IpDetailsReplyDump> stage1Optional = managerNegative.getDump(identifier, cache, NO_PARAMS);
95 // this is first call so instance should be from executor
96 // and it should be cached after calling executor
97 assertEquals(true, stage1Optional.isPresent());
98 assertEquals(stage1Data, stage1Optional.get());
99 assertEquals(true, cache.containsKey(key));
100 assertEquals(stage1Data, cache.get(key));
102 //rebind executor with other data
103 IpDetailsReplyDump stage2LoadedDump = new IpDetailsReplyDump();
104 when(executor.executeDump(identifier, NO_PARAMS)).thenReturn(stage2LoadedDump);
106 final Optional<IpDetailsReplyDump> stage2Optional = managerPositive.getDump(identifier, cache, NO_PARAMS);
108 assertEquals(true, stage2Optional.isPresent());
109 assertEquals(stage2LoadedDump, stage2Optional.get());
111 //rebind executor with other data
112 IpDetailsReplyDump stage3LoadedDump = new IpDetailsReplyDump();
113 when(executor.executeDump(identifier, NO_PARAMS)).thenReturn(stage3LoadedDump);
115 final Optional<IpDetailsReplyDump> stage3Optional = managerPositive.getDump(identifier, cache, NO_PARAMS);
116 assertEquals(true, stage3Optional.isPresent());
117 //check if it returns instance cached from previous stage
118 assertEquals(stage2LoadedDump, stage3Optional.get());
122 public void testPostprocessing() throws ReadFailedException {
123 IpDetailsReplyDump dump = new IpDetailsReplyDump();
124 IpDetails details = new IpDetails();
125 details.swIfIndex = 2;
126 dump.ipDetails.add(details);
128 when(executor.executeDump(identifier, null)).thenReturn(dump);
130 Optional<IpDetailsReplyDump> optionalDump =
131 managerPositiveWithPostProcessing.getDump(identifier, cache, NO_PARAMS);
133 assertEquals(true, optionalDump.isPresent());
134 assertEquals(1, optionalDump.get().ipDetails.size());
135 assertEquals(7, optionalDump.get().ipDetails.get(0).swIfIndex);
138 private EntityDumpPostProcessingFunction<IpDetailsReplyDump> createPostProcessor() {
139 return ipDetailsReplyDump -> {
140 IpDetailsReplyDump modified = new IpDetailsReplyDump();
142 for (IpDetails detail : ipDetailsReplyDump.ipDetails) {
143 IpDetails modifiedDetail = new IpDetails();
144 modifiedDetail.swIfIndex = detail.swIfIndex + 5;
146 modified.ipDetails.add(modifiedDetail);