HONEYCOMB-58 - Routing Api
[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 io.fd.honeycomb.translate.util.read.cache.EntityDumpExecutor.NO_PARAMS;
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.translate.ModificationCache;
25 import io.fd.honeycomb.translate.read.ReadFailedException;
26 import io.fd.honeycomb.translate.util.read.cache.CacheKeyFactory;
27 import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager;
28 import io.fd.honeycomb.translate.util.read.cache.EntityDumpExecutor;
29 import io.fd.honeycomb.translate.util.read.cache.EntityDumpPostProcessingFunction;
30 import io.fd.honeycomb.translate.util.read.cache.IdentifierCacheKeyFactory;
31 import io.fd.vpp.jvpp.core.dto.IpDetails;
32 import io.fd.vpp.jvpp.core.dto.IpDetailsReplyDump;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.opendaylight.yangtools.yang.binding.DataObject;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39
40 public class DumpCacheManagerTest {
41
42     private interface DataObj extends DataObject {}
43
44     @Mock
45     private EntityDumpExecutor<IpDetailsReplyDump, Void> executor;
46
47     private InstanceIdentifier<DataObj> identifier;
48
49     private DumpCacheManager<IpDetailsReplyDump, Void> managerPositive;
50     private DumpCacheManager<IpDetailsReplyDump, Void> managerPositiveWithPostProcessing;
51     private DumpCacheManager<IpDetailsReplyDump, Void> managerNegative;
52     private ModificationCache cache;
53     private CacheKeyFactory cacheKeyFactory;
54
55     @Before
56     public void init() {
57         MockitoAnnotations.initMocks(this);
58         managerPositive =
59                 new DumpCacheManager.DumpCacheManagerBuilder<IpDetailsReplyDump, Void>()
60                         .withExecutor(executor)
61                         .build();
62
63         managerPositiveWithPostProcessing =
64                 new DumpCacheManager.DumpCacheManagerBuilder<IpDetailsReplyDump, Void>()
65                         .withExecutor(executor)
66                         .withPostProcessingFunction(createPostProcessor())
67                         .build();
68
69         managerNegative =
70                 new DumpCacheManager.DumpCacheManagerBuilder<IpDetailsReplyDump, Void>()
71                         .withExecutor(executor)
72                         .build();
73
74         cache = new ModificationCache();
75         identifier = InstanceIdentifier.create(DataObj.class);
76         //manager uses this implementation by default, so it can be used to test behaviour
77         cacheKeyFactory = new IdentifierCacheKeyFactory();
78
79     }
80
81     /**
82      * This test verify full dump-caching cycle
83      */
84     @Test
85     public void testCaching() throws ReadFailedException {
86         final IpDetailsReplyDump stage1Data = new IpDetailsReplyDump();
87         final String key = cacheKeyFactory.createKey(identifier);
88
89
90         // executor cant return null data
91         when(executor.executeDump(identifier, NO_PARAMS)).thenReturn(new IpDetailsReplyDump());
92
93         final Optional<IpDetailsReplyDump> stage1Optional = managerNegative.getDump(identifier, cache, NO_PARAMS);
94
95         // this is first call so instance should be from executor
96         // and it should be cached after calling executor
97         assertEquals(true, stage1Optional.isPresent());
98         assertEquals(stage1Data, stage1Optional.get());
99         assertEquals(true, cache.containsKey(key));
100         assertEquals(stage1Data, cache.get(key));
101
102         //rebind executor with other data
103         IpDetailsReplyDump stage2LoadedDump = new IpDetailsReplyDump();
104         when(executor.executeDump(identifier, NO_PARAMS)).thenReturn(stage2LoadedDump);
105
106         final Optional<IpDetailsReplyDump> stage2Optional = managerPositive.getDump(identifier, cache, NO_PARAMS);
107
108         assertEquals(true, stage2Optional.isPresent());
109         assertEquals(stage2LoadedDump, stage2Optional.get());
110
111         //rebind executor with other data
112         IpDetailsReplyDump stage3LoadedDump = new IpDetailsReplyDump();
113         when(executor.executeDump(identifier, NO_PARAMS)).thenReturn(stage3LoadedDump);
114
115         final Optional<IpDetailsReplyDump> stage3Optional = managerPositive.getDump(identifier, cache, NO_PARAMS);
116         assertEquals(true, stage3Optional.isPresent());
117         //check if it returns instance cached from previous stage
118         assertEquals(stage2LoadedDump, stage3Optional.get());
119     }
120
121     @Test
122     public void testPostprocessing() throws ReadFailedException {
123         IpDetailsReplyDump dump = new IpDetailsReplyDump();
124         IpDetails details = new IpDetails();
125         details.swIfIndex = 2;
126         dump.ipDetails.add(details);
127
128         when(executor.executeDump(identifier, null)).thenReturn(dump);
129
130         Optional<IpDetailsReplyDump> optionalDump =
131                 managerPositiveWithPostProcessing.getDump(identifier, cache, NO_PARAMS);
132
133         assertEquals(true, optionalDump.isPresent());
134         assertEquals(1, optionalDump.get().ipDetails.size());
135         assertEquals(7, optionalDump.get().ipDetails.get(0).swIfIndex);
136     }
137
138     private EntityDumpPostProcessingFunction<IpDetailsReplyDump> createPostProcessor() {
139         return ipDetailsReplyDump -> {
140             IpDetailsReplyDump modified = new IpDetailsReplyDump();
141
142             for (IpDetails detail : ipDetailsReplyDump.ipDetails) {
143                 IpDetails modifiedDetail = new IpDetails();
144                 modifiedDetail.swIfIndex = detail.swIfIndex + 5;
145
146                 modified.ipDetails.add(modifiedDetail);
147             }
148
149             return modified;
150         };
151     }
152 }