HONEYCOMB-206: change package name to match groupId
[honeycomb.git] / vpp-common / vpp-translate-utils / src / test / java / io / fd / honeycomb / translate / vpp / util / cache / DumpCacheManagerTest.java
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.vpp.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.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;
36
37 public class DumpCacheManagerTest {
38
39     private static final String KEY = "cacheKey";
40
41     @Mock
42     private EntityDumpExecutor<IpDetailsReplyDump, Void> executor;
43
44     private DumpCacheManager<IpDetailsReplyDump, Void> managerPositive;
45     private DumpCacheManager<IpDetailsReplyDump, Void> managerPositiveWithPostProcessing;
46     private DumpCacheManager<IpDetailsReplyDump, Void> managerNegative;
47     private ModificationCache cache;
48
49     @Before
50     public void init() {
51         MockitoAnnotations.initMocks(this);
52         managerPositive =
53                 new DumpCacheManager.DumpCacheManagerBuilder<IpDetailsReplyDump, Void>()
54                         .withExecutor(executor)
55                         .withNonEmptyPredicate(createPositivePredicate())
56                         .build();
57
58         managerPositiveWithPostProcessing = new DumpCacheManager.DumpCacheManagerBuilder<IpDetailsReplyDump, Void>()
59                 .withExecutor(executor)
60                 .withNonEmptyPredicate(createPositivePredicate())
61                 .withPostProcessingFunction(createPostProcessor())
62                 .build();
63
64         managerNegative =
65                 new DumpCacheManager.DumpCacheManagerBuilder<IpDetailsReplyDump, Void>()
66                         .withExecutor(executor)
67                         .withNonEmptyPredicate(createNegativePredicate())
68                         .build();
69
70         cache = new ModificationCache();
71     }
72
73     /**
74      * This test verify full dump-caching cycle
75      */
76     @Test
77     public void testCaching() throws DumpExecutionFailedException {
78
79
80         Optional<IpDetailsReplyDump> stage1Optional = managerNegative.getDump(KEY, cache, null);
81
82         //this is first call so instance should be from executor
83         assertEquals(false, stage1Optional.isPresent());
84         assertEquals(false, cache.containsKey(KEY));
85
86         //rebind executor with other data
87         IpDetailsReplyDump stage2LoadedDump = new IpDetailsReplyDump();
88         when(executor.executeDump(null)).thenReturn(stage2LoadedDump);
89
90         Optional<IpDetailsReplyDump> stage2Optional = managerPositive.getDump(KEY, cache, null);
91
92         assertEquals(true, stage2Optional.isPresent());
93         assertEquals(stage2LoadedDump, stage2Optional.get());
94
95         //rebind executor with other data
96         IpDetailsReplyDump stage3LoadedDump = new IpDetailsReplyDump();
97         when(executor.executeDump(null)).thenReturn(stage3LoadedDump);
98
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());
103     }
104
105     @Test
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);
111
112         when(executor.executeDump(null)).thenReturn(dump);
113
114         Optional<IpDetailsReplyDump> optionalDump = managerPositiveWithPostProcessing.getDump(KEY, cache, null);
115
116         assertEquals(true, optionalDump.isPresent());
117         assertEquals(1, optionalDump.get().ipDetails.size());
118         assertEquals(7, optionalDump.get().ipDetails.get(0).swIfIndex);
119     }
120
121     private EntityDumpNonEmptyCheck<IpDetailsReplyDump> createNegativePredicate() {
122         return data -> {
123             throw new DumpEmptyException("Empty dump", new IllegalArgumentException());
124         };
125     }
126
127     private EntityDumpNonEmptyCheck<IpDetailsReplyDump> createPositivePredicate() {
128         return data -> {
129             //DO NOTHING
130         };
131     }
132
133     private EntityDumpPostProcessingFunction<IpDetailsReplyDump> createPostProcessor() {
134         return ipDetailsReplyDump -> {
135             IpDetailsReplyDump modified = new IpDetailsReplyDump();
136
137             for (IpDetails detail : ipDetailsReplyDump.ipDetails) {
138                 IpDetails modifiedDetail = new IpDetails();
139                 modifiedDetail.swIfIndex = detail.swIfIndex + 5;
140
141                 modified.ipDetails.add(modifiedDetail);
142             }
143
144             return modified;
145         };
146     }
147 }