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.v3po.translate.v3po.util.cache;
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.v3po.translate.ModificationCache;
25 import io.fd.honeycomb.v3po.translate.v3po.util.cache.exceptions.check.i.DumpEmptyException;
26 import io.fd.honeycomb.v3po.translate.v3po.util.cache.exceptions.execution.DumpExecutionFailedException;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.mockito.Mock;
30 import org.mockito.MockitoAnnotations;
31 import org.openvpp.jvpp.dto.IpDetails;
32 import org.openvpp.jvpp.dto.IpDetailsReplyDump;
34 public class DumpCacheManagerTest {
36 private static final String KEY = "cacheKey";
39 private EntityDumpExecutor<IpDetailsReplyDump, Void> executor;
41 private DumpCacheManager<IpDetailsReplyDump, Void> managerPositive;
42 private DumpCacheManager<IpDetailsReplyDump, Void> managerPositiveWithPostProcessing;
43 private DumpCacheManager<IpDetailsReplyDump, Void> managerNegative;
44 private ModificationCache cache;
48 MockitoAnnotations.initMocks(this);
50 new DumpCacheManager.DumpCacheManagerBuilder<IpDetailsReplyDump, Void>()
51 .withExecutor(executor)
52 .withNonEmptyPredicate(createPositivePredicate())
55 managerPositiveWithPostProcessing = new DumpCacheManager.DumpCacheManagerBuilder<IpDetailsReplyDump, Void>()
56 .withExecutor(executor)
57 .withNonEmptyPredicate(createPositivePredicate())
58 .withPostProcessingFunction(createPostProcessor())
62 new DumpCacheManager.DumpCacheManagerBuilder<IpDetailsReplyDump, Void>()
63 .withExecutor(executor)
64 .withNonEmptyPredicate(createNegativePredicate())
67 cache = new ModificationCache();
71 * This test verify full dump-caching cycle
74 public void testCaching() throws DumpExecutionFailedException {
77 Optional<IpDetailsReplyDump> stage1Optional = managerNegative.getDump(KEY, cache);
79 //this is first call so instance should be from executor
80 assertEquals(false, stage1Optional.isPresent());
81 assertEquals(false, cache.containsKey(KEY));
83 //rebind executor with other data
84 IpDetailsReplyDump stage2LoadedDump = new IpDetailsReplyDump();
85 when(executor.executeDump()).thenReturn(stage2LoadedDump);
87 Optional<IpDetailsReplyDump> stage2Optional = managerPositive.getDump(KEY, cache);
89 assertEquals(true, stage2Optional.isPresent());
90 assertEquals(stage2LoadedDump, stage2Optional.get());
92 //rebind executor with other data
93 IpDetailsReplyDump stage3LoadedDump = new IpDetailsReplyDump();
94 when(executor.executeDump()).thenReturn(stage3LoadedDump);
96 Optional<IpDetailsReplyDump> stage3Optional = managerPositive.getDump(KEY, cache);
97 assertEquals(true, stage3Optional.isPresent());
98 //check if it returns instance cached from previous stage
99 assertEquals(stage2LoadedDump, stage3Optional.get());
103 public void testPostprocessing() throws DumpExecutionFailedException {
104 IpDetailsReplyDump dump = new IpDetailsReplyDump();
105 IpDetails details = new IpDetails();
106 details.swIfIndex = 2;
107 dump.ipDetails.add(details);
109 when(executor.executeDump()).thenReturn(dump);
111 Optional<IpDetailsReplyDump> optionalDump = managerPositiveWithPostProcessing.getDump(KEY, cache);
113 assertEquals(true, optionalDump.isPresent());
114 assertEquals(1, optionalDump.get().ipDetails.size());
115 assertEquals(7, optionalDump.get().ipDetails.get(0).swIfIndex);
118 private EntityDumpNonEmptyCheck<IpDetailsReplyDump> createNegativePredicate() {
120 throw new DumpEmptyException("Empty dump", new IllegalArgumentException());
124 private EntityDumpNonEmptyCheck<IpDetailsReplyDump> createPositivePredicate() {
130 private EntityDumpPostProcessingFunction<IpDetailsReplyDump> createPostProcessor() {
131 return ipDetailsReplyDump -> {
132 IpDetailsReplyDump modified = new IpDetailsReplyDump();
134 for (IpDetails detail : ipDetailsReplyDump.ipDetails) {
135 IpDetails modifiedDetail = new IpDetails();
136 modifiedDetail.swIfIndex = detail.swIfIndex + 5;
138 modified.ipDetails.add(modifiedDetail);