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.util.read.cache.DumpCacheManager;
26 import io.fd.honeycomb.translate.util.read.cache.EntityDumpExecutor;
27 import io.fd.honeycomb.translate.util.read.cache.EntityDumpPostProcessingFunction;
28 import io.fd.honeycomb.translate.util.read.cache.exceptions.execution.DumpExecutionFailedException;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.mockito.Mock;
32 import org.mockito.MockitoAnnotations;
33 import org.openvpp.jvpp.core.dto.IpDetails;
34 import org.openvpp.jvpp.core.dto.IpDetailsReplyDump;
36 public class DumpCacheManagerTest {
38 private static final String KEY = "cacheKey";
41 private EntityDumpExecutor<IpDetailsReplyDump, Void> executor;
43 private DumpCacheManager<IpDetailsReplyDump, Void> managerPositive;
44 private DumpCacheManager<IpDetailsReplyDump, Void> managerPositiveWithPostProcessing;
45 private DumpCacheManager<IpDetailsReplyDump, Void> managerNegative;
46 private ModificationCache cache;
50 MockitoAnnotations.initMocks(this);
52 new DumpCacheManager.DumpCacheManagerBuilder<IpDetailsReplyDump, Void>()
53 .withExecutor(executor)
56 managerPositiveWithPostProcessing = new DumpCacheManager.DumpCacheManagerBuilder<IpDetailsReplyDump, Void>()
57 .withExecutor(executor)
58 .withPostProcessingFunction(createPostProcessor())
62 new DumpCacheManager.DumpCacheManagerBuilder<IpDetailsReplyDump, Void>()
63 .withExecutor(executor)
66 cache = new ModificationCache();
70 * This test verify full dump-caching cycle
73 public void testCaching() throws DumpExecutionFailedException {
74 final IpDetailsReplyDump stage1Data = new IpDetailsReplyDump();
76 // executor cant return null data
77 when(executor.executeDump(NO_PARAMS)).thenReturn(new IpDetailsReplyDump());
79 final Optional<IpDetailsReplyDump> stage1Optional = managerNegative.getDump(KEY, cache, NO_PARAMS);
81 // this is first call so instance should be from executor
82 // and it should be cached after calling executor
83 assertEquals(true, stage1Optional.isPresent());
84 assertEquals(stage1Data, stage1Optional.get());
85 assertEquals(true, cache.containsKey(KEY));
86 assertEquals(stage1Data, cache.get(KEY));
88 //rebind executor with other data
89 IpDetailsReplyDump stage2LoadedDump = new IpDetailsReplyDump();
90 when(executor.executeDump(NO_PARAMS)).thenReturn(stage2LoadedDump);
92 final Optional<IpDetailsReplyDump> stage2Optional = managerPositive.getDump(KEY, cache, NO_PARAMS);
94 assertEquals(true, stage2Optional.isPresent());
95 assertEquals(stage2LoadedDump, stage2Optional.get());
97 //rebind executor with other data
98 IpDetailsReplyDump stage3LoadedDump = new IpDetailsReplyDump();
99 when(executor.executeDump(NO_PARAMS)).thenReturn(stage3LoadedDump);
101 final Optional<IpDetailsReplyDump> stage3Optional = managerPositive.getDump(KEY, cache, NO_PARAMS);
102 assertEquals(true, stage3Optional.isPresent());
103 //check if it returns instance cached from previous stage
104 assertEquals(stage2LoadedDump, stage3Optional.get());
108 public void testPostprocessing() throws DumpExecutionFailedException {
109 IpDetailsReplyDump dump = new IpDetailsReplyDump();
110 IpDetails details = new IpDetails();
111 details.swIfIndex = 2;
112 dump.ipDetails.add(details);
114 when(executor.executeDump(null)).thenReturn(dump);
116 Optional<IpDetailsReplyDump> optionalDump = managerPositiveWithPostProcessing.getDump(KEY, cache, NO_PARAMS);
118 assertEquals(true, optionalDump.isPresent());
119 assertEquals(1, optionalDump.get().ipDetails.size());
120 assertEquals(7, optionalDump.get().ipDetails.get(0).swIfIndex);
123 private EntityDumpPostProcessingFunction<IpDetailsReplyDump> createPostProcessor() {
124 return ipDetailsReplyDump -> {
125 IpDetailsReplyDump modified = new IpDetailsReplyDump();
127 for (IpDetails detail : ipDetailsReplyDump.ipDetails) {
128 IpDetails modifiedDetail = new IpDetails();
129 modifiedDetail.swIfIndex = detail.swIfIndex + 5;
131 modified.ipDetails.add(modifiedDetail);