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