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.DumpCacheManager;
27 import io.fd.honeycomb.translate.util.read.cache.EntityDumpExecutor;
28 import io.fd.honeycomb.translate.util.read.cache.EntityDumpPostProcessingFunction;
29 import io.fd.vpp.jvpp.core.dto.IpDetails;
30 import io.fd.vpp.jvpp.core.dto.IpDetailsReplyDump;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.mockito.Mock;
34 import org.mockito.MockitoAnnotations;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 public class DumpCacheManagerTest {
39 private static final String KEY = "cacheKey";
42 private EntityDumpExecutor<IpDetailsReplyDump, Void> executor;
45 private InstanceIdentifier identifier;
47 private DumpCacheManager<IpDetailsReplyDump, Void> managerPositive;
48 private DumpCacheManager<IpDetailsReplyDump, Void> managerPositiveWithPostProcessing;
49 private DumpCacheManager<IpDetailsReplyDump, Void> managerNegative;
50 private ModificationCache cache;
54 MockitoAnnotations.initMocks(this);
56 new DumpCacheManager.DumpCacheManagerBuilder<IpDetailsReplyDump, Void>()
57 .withExecutor(executor)
60 managerPositiveWithPostProcessing = new DumpCacheManager.DumpCacheManagerBuilder<IpDetailsReplyDump, Void>()
61 .withExecutor(executor)
62 .withPostProcessingFunction(createPostProcessor())
66 new DumpCacheManager.DumpCacheManagerBuilder<IpDetailsReplyDump, Void>()
67 .withExecutor(executor)
70 cache = new ModificationCache();
74 * This test verify full dump-caching cycle
77 public void testCaching() throws ReadFailedException {
78 final IpDetailsReplyDump stage1Data = new IpDetailsReplyDump();
80 // executor cant return null data
81 when(executor.executeDump(identifier, NO_PARAMS)).thenReturn(new IpDetailsReplyDump());
83 final Optional<IpDetailsReplyDump> stage1Optional = managerNegative.getDump(identifier, KEY, cache, NO_PARAMS);
85 // this is first call so instance should be from executor
86 // and it should be cached after calling executor
87 assertEquals(true, stage1Optional.isPresent());
88 assertEquals(stage1Data, stage1Optional.get());
89 assertEquals(true, cache.containsKey(KEY));
90 assertEquals(stage1Data, cache.get(KEY));
92 //rebind executor with other data
93 IpDetailsReplyDump stage2LoadedDump = new IpDetailsReplyDump();
94 when(executor.executeDump(identifier, NO_PARAMS)).thenReturn(stage2LoadedDump);
96 final Optional<IpDetailsReplyDump> stage2Optional = managerPositive.getDump(identifier, KEY, cache, NO_PARAMS);
98 assertEquals(true, stage2Optional.isPresent());
99 assertEquals(stage2LoadedDump, stage2Optional.get());
101 //rebind executor with other data
102 IpDetailsReplyDump stage3LoadedDump = new IpDetailsReplyDump();
103 when(executor.executeDump(identifier, NO_PARAMS)).thenReturn(stage3LoadedDump);
105 final Optional<IpDetailsReplyDump> stage3Optional = managerPositive.getDump(identifier, KEY, cache, NO_PARAMS);
106 assertEquals(true, stage3Optional.isPresent());
107 //check if it returns instance cached from previous stage
108 assertEquals(stage2LoadedDump, stage3Optional.get());
112 public void testPostprocessing() throws ReadFailedException {
113 IpDetailsReplyDump dump = new IpDetailsReplyDump();
114 IpDetails details = new IpDetails();
115 details.swIfIndex = 2;
116 dump.ipDetails.add(details);
118 when(executor.executeDump(identifier, null)).thenReturn(dump);
120 Optional<IpDetailsReplyDump> optionalDump =
121 managerPositiveWithPostProcessing.getDump(identifier, KEY, cache, NO_PARAMS);
123 assertEquals(true, optionalDump.isPresent());
124 assertEquals(1, optionalDump.get().ipDetails.size());
125 assertEquals(7, optionalDump.get().ipDetails.get(0).swIfIndex);
128 private EntityDumpPostProcessingFunction<IpDetailsReplyDump> createPostProcessor() {
129 return ipDetailsReplyDump -> {
130 IpDetailsReplyDump modified = new IpDetailsReplyDump();
132 for (IpDetails detail : ipDetailsReplyDump.ipDetails) {
133 IpDetails modifiedDetail = new IpDetails();
134 modifiedDetail.swIfIndex = detail.swIfIndex + 5;
136 modified.ipDetails.add(modifiedDetail);