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