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.v3po.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.v3po.util.cache.exceptions.check.i.DumpEmptyException;
25 import io.fd.honeycomb.translate.v3po.util.cache.exceptions.execution.DumpExecutionFailedException;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.mockito.Mock;
29 import org.mockito.MockitoAnnotations;
30 import org.openvpp.jvpp.core.dto.IpDetails;
31 import org.openvpp.jvpp.core.dto.IpDetailsReplyDump;
33 public class DumpCacheManagerTest {
35 private static final String KEY = "cacheKey";
38 private EntityDumpExecutor<IpDetailsReplyDump, Void> executor;
40 private DumpCacheManager<IpDetailsReplyDump, Void> managerPositive;
41 private DumpCacheManager<IpDetailsReplyDump, Void> managerPositiveWithPostProcessing;
42 private DumpCacheManager<IpDetailsReplyDump, Void> managerNegative;
43 private ModificationCache cache;
47 MockitoAnnotations.initMocks(this);
49 new DumpCacheManager.DumpCacheManagerBuilder<IpDetailsReplyDump, Void>()
50 .withExecutor(executor)
51 .withNonEmptyPredicate(createPositivePredicate())
54 managerPositiveWithPostProcessing = new DumpCacheManager.DumpCacheManagerBuilder<IpDetailsReplyDump, Void>()
55 .withExecutor(executor)
56 .withNonEmptyPredicate(createPositivePredicate())
57 .withPostProcessingFunction(createPostProcessor())
61 new DumpCacheManager.DumpCacheManagerBuilder<IpDetailsReplyDump, Void>()
62 .withExecutor(executor)
63 .withNonEmptyPredicate(createNegativePredicate())
66 cache = new ModificationCache();
70 * This test verify full dump-caching cycle
73 public void testCaching() throws DumpExecutionFailedException {
76 Optional<IpDetailsReplyDump> stage1Optional = managerNegative.getDump(KEY, cache, null);
78 //this is first call so instance should be from executor
79 assertEquals(false, stage1Optional.isPresent());
80 assertEquals(false, cache.containsKey(KEY));
82 //rebind executor with other data
83 IpDetailsReplyDump stage2LoadedDump = new IpDetailsReplyDump();
84 when(executor.executeDump(null)).thenReturn(stage2LoadedDump);
86 Optional<IpDetailsReplyDump> stage2Optional = managerPositive.getDump(KEY, cache, null);
88 assertEquals(true, stage2Optional.isPresent());
89 assertEquals(stage2LoadedDump, stage2Optional.get());
91 //rebind executor with other data
92 IpDetailsReplyDump stage3LoadedDump = new IpDetailsReplyDump();
93 when(executor.executeDump(null)).thenReturn(stage3LoadedDump);
95 Optional<IpDetailsReplyDump> stage3Optional = managerPositive.getDump(KEY, cache, null);
96 assertEquals(true, stage3Optional.isPresent());
97 //check if it returns instance cached from previous stage
98 assertEquals(stage2LoadedDump, stage3Optional.get());
102 public void testPostprocessing() throws DumpExecutionFailedException {
103 IpDetailsReplyDump dump = new IpDetailsReplyDump();
104 IpDetails details = new IpDetails();
105 details.swIfIndex = 2;
106 dump.ipDetails.add(details);
108 when(executor.executeDump(null)).thenReturn(dump);
110 Optional<IpDetailsReplyDump> optionalDump = managerPositiveWithPostProcessing.getDump(KEY, cache, null);
112 assertEquals(true, optionalDump.isPresent());
113 assertEquals(1, optionalDump.get().ipDetails.size());
114 assertEquals(7, optionalDump.get().ipDetails.get(0).swIfIndex);
117 private EntityDumpNonEmptyCheck<IpDetailsReplyDump> createNegativePredicate() {
119 throw new DumpEmptyException("Empty dump", new IllegalArgumentException());
123 private EntityDumpNonEmptyCheck<IpDetailsReplyDump> createPositivePredicate() {
129 private EntityDumpPostProcessingFunction<IpDetailsReplyDump> createPostProcessor() {
130 return ipDetailsReplyDump -> {
131 IpDetailsReplyDump modified = new IpDetailsReplyDump();
133 for (IpDetails detail : ipDetailsReplyDump.ipDetails) {
134 IpDetails modifiedDetail = new IpDetails();
135 modifiedDetail.swIfIndex = detail.swIfIndex + 5;
137 modified.ipDetails.add(modifiedDetail);