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 org.junit.Assert.assertEquals;
20 import static org.mockito.Mockito.when;
22 import com.google.common.base.Optional;
23 import io.fd.honeycomb.translate.ModificationCache;
24 import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager;
25 import io.fd.honeycomb.translate.util.read.cache.EntityDumpExecutor;
26 import io.fd.honeycomb.translate.util.read.cache.EntityDumpNonEmptyCheck;
27 import io.fd.honeycomb.translate.util.read.cache.EntityDumpPostProcessingFunction;
28 import io.fd.honeycomb.translate.util.read.cache.exceptions.check.i.DumpEmptyException;
29 import io.fd.honeycomb.translate.util.read.cache.exceptions.execution.DumpExecutionFailedException;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.mockito.Mock;
33 import org.mockito.MockitoAnnotations;
34 import org.openvpp.jvpp.core.dto.IpDetails;
35 import org.openvpp.jvpp.core.dto.IpDetailsReplyDump;
37 public class DumpCacheManagerTest {
39 private static final String KEY = "cacheKey";
42 private EntityDumpExecutor<IpDetailsReplyDump, Void> executor;
44 private DumpCacheManager<IpDetailsReplyDump, Void> managerPositive;
45 private DumpCacheManager<IpDetailsReplyDump, Void> managerPositiveWithPostProcessing;
46 private DumpCacheManager<IpDetailsReplyDump, Void> managerNegative;
47 private ModificationCache cache;
51 MockitoAnnotations.initMocks(this);
53 new DumpCacheManager.DumpCacheManagerBuilder<IpDetailsReplyDump, Void>()
54 .withExecutor(executor)
55 .withNonEmptyPredicate(createPositivePredicate())
58 managerPositiveWithPostProcessing = new DumpCacheManager.DumpCacheManagerBuilder<IpDetailsReplyDump, Void>()
59 .withExecutor(executor)
60 .withNonEmptyPredicate(createPositivePredicate())
61 .withPostProcessingFunction(createPostProcessor())
65 new DumpCacheManager.DumpCacheManagerBuilder<IpDetailsReplyDump, Void>()
66 .withExecutor(executor)
67 .withNonEmptyPredicate(createNegativePredicate())
70 cache = new ModificationCache();
74 * This test verify full dump-caching cycle
77 public void testCaching() throws DumpExecutionFailedException {
80 Optional<IpDetailsReplyDump> stage1Optional = managerNegative.getDump(KEY, cache, null);
82 //this is first call so instance should be from executor
83 assertEquals(false, stage1Optional.isPresent());
84 assertEquals(false, cache.containsKey(KEY));
86 //rebind executor with other data
87 IpDetailsReplyDump stage2LoadedDump = new IpDetailsReplyDump();
88 when(executor.executeDump(null)).thenReturn(stage2LoadedDump);
90 Optional<IpDetailsReplyDump> stage2Optional = managerPositive.getDump(KEY, cache, null);
92 assertEquals(true, stage2Optional.isPresent());
93 assertEquals(stage2LoadedDump, stage2Optional.get());
95 //rebind executor with other data
96 IpDetailsReplyDump stage3LoadedDump = new IpDetailsReplyDump();
97 when(executor.executeDump(null)).thenReturn(stage3LoadedDump);
99 Optional<IpDetailsReplyDump> stage3Optional = managerPositive.getDump(KEY, cache, null);
100 assertEquals(true, stage3Optional.isPresent());
101 //check if it returns instance cached from previous stage
102 assertEquals(stage2LoadedDump, stage3Optional.get());
106 public void testPostprocessing() throws DumpExecutionFailedException {
107 IpDetailsReplyDump dump = new IpDetailsReplyDump();
108 IpDetails details = new IpDetails();
109 details.swIfIndex = 2;
110 dump.ipDetails.add(details);
112 when(executor.executeDump(null)).thenReturn(dump);
114 Optional<IpDetailsReplyDump> optionalDump = managerPositiveWithPostProcessing.getDump(KEY, cache, null);
116 assertEquals(true, optionalDump.isPresent());
117 assertEquals(1, optionalDump.get().ipDetails.size());
118 assertEquals(7, optionalDump.get().ipDetails.get(0).swIfIndex);
121 private EntityDumpNonEmptyCheck<IpDetailsReplyDump> createNegativePredicate() {
123 throw new DumpEmptyException("Empty dump", new IllegalArgumentException());
127 private EntityDumpNonEmptyCheck<IpDetailsReplyDump> createPositivePredicate() {
133 private EntityDumpPostProcessingFunction<IpDetailsReplyDump> createPostProcessor() {
134 return ipDetailsReplyDump -> {
135 IpDetailsReplyDump modified = new IpDetailsReplyDump();
137 for (IpDetails detail : ipDetailsReplyDump.ipDetails) {
138 IpDetails modifiedDetail = new IpDetails();
139 modifiedDetail.swIfIndex = detail.swIfIndex + 5;
141 modified.ipDetails.add(modifiedDetail);