9cbbda8ade0f530bf255e5a68132326bdc73086d
[hc2vpp.git] /
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package io.fd.honeycomb.v3po.translate.v3po.util.cache;
18
19
20 import static org.junit.Assert.assertEquals;
21 import static org.mockito.Mockito.when;
22
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;
33
34 public class DumpCacheManagerTest {
35
36     private static final String KEY = "cacheKey";
37
38     @Mock
39     private EntityDumpExecutor<IpDetailsReplyDump, Void> executor;
40
41     private DumpCacheManager<IpDetailsReplyDump, Void> managerPositive;
42     private DumpCacheManager<IpDetailsReplyDump, Void> managerPositiveWithPostProcessing;
43     private DumpCacheManager<IpDetailsReplyDump, Void> managerNegative;
44     private ModificationCache cache;
45
46     @Before
47     public void init() {
48         MockitoAnnotations.initMocks(this);
49         managerPositive =
50                 new DumpCacheManager.DumpCacheManagerBuilder<IpDetailsReplyDump, Void>()
51                         .withExecutor(executor)
52                         .withNonEmptyPredicate(createPositivePredicate())
53                         .build();
54
55         managerPositiveWithPostProcessing = new DumpCacheManager.DumpCacheManagerBuilder<IpDetailsReplyDump, Void>()
56                 .withExecutor(executor)
57                 .withNonEmptyPredicate(createPositivePredicate())
58                 .withPostProcessingFunction(createPostProcessor())
59                 .build();
60
61         managerNegative =
62                 new DumpCacheManager.DumpCacheManagerBuilder<IpDetailsReplyDump, Void>()
63                         .withExecutor(executor)
64                         .withNonEmptyPredicate(createNegativePredicate())
65                         .build();
66
67         cache = new ModificationCache();
68     }
69
70     /**
71      * This test verify full dump-caching cycle
72      */
73     @Test
74     public void testCaching() throws DumpExecutionFailedException {
75
76
77         Optional<IpDetailsReplyDump> stage1Optional = managerNegative.getDump(KEY, cache);
78
79         //this is first call so instance should be from executor
80         assertEquals(false, stage1Optional.isPresent());
81         assertEquals(false, cache.containsKey(KEY));
82
83         //rebind executor with other data
84         IpDetailsReplyDump stage2LoadedDump = new IpDetailsReplyDump();
85         when(executor.executeDump()).thenReturn(stage2LoadedDump);
86
87         Optional<IpDetailsReplyDump> stage2Optional = managerPositive.getDump(KEY, cache);
88
89         assertEquals(true, stage2Optional.isPresent());
90         assertEquals(stage2LoadedDump, stage2Optional.get());
91
92         //rebind executor with other data
93         IpDetailsReplyDump stage3LoadedDump = new IpDetailsReplyDump();
94         when(executor.executeDump()).thenReturn(stage3LoadedDump);
95
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());
100     }
101
102     @Test
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);
108
109         when(executor.executeDump()).thenReturn(dump);
110
111         Optional<IpDetailsReplyDump> optionalDump = managerPositiveWithPostProcessing.getDump(KEY, cache);
112
113         assertEquals(true, optionalDump.isPresent());
114         assertEquals(1, optionalDump.get().ipDetails.size());
115         assertEquals(7, optionalDump.get().ipDetails.get(0).swIfIndex);
116     }
117
118     private EntityDumpNonEmptyCheck<IpDetailsReplyDump> createNegativePredicate() {
119         return data -> {
120             throw new DumpEmptyException("Empty dump", new IllegalArgumentException());
121         };
122     }
123
124     private EntityDumpNonEmptyCheck<IpDetailsReplyDump> createPositivePredicate() {
125         return data -> {
126             //DO NOTHING
127         };
128     }
129
130     private EntityDumpPostProcessingFunction<IpDetailsReplyDump> createPostProcessor() {
131         return ipDetailsReplyDump -> {
132             IpDetailsReplyDump modified = new IpDetailsReplyDump();
133
134             for (IpDetails detail : ipDetailsReplyDump.ipDetails) {
135                 IpDetails modifiedDetail = new IpDetails();
136                 modifiedDetail.swIfIndex = detail.swIfIndex + 5;
137
138                 modified.ipDetails.add(modifiedDetail);
139             }
140
141             return modified;
142         };
143     }
144 }