528ce5063dd1a0abae44deae120cfd33b937c356
[ccsdk/sli.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights
6  *                      reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.ccsdk.sli.northbound.daeximoffsitebackup;
23
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.fail;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
29 import com.google.common.util.concurrent.FluentFuture;
30 import java.io.File;
31 import java.lang.reflect.Field;
32 import java.lang.reflect.InvocationTargetException;
33 import java.lang.reflect.Method;
34 import java.util.Arrays;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Properties;
38 import java.util.concurrent.ExecutionException;
39 import org.eclipse.jdt.annotation.NonNull;
40 import org.eclipse.jdt.annotation.Nullable;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.opendaylight.mdsal.binding.api.DataBroker;
44 import org.opendaylight.mdsal.binding.api.ReadWriteTransaction;
45 import org.opendaylight.mdsal.binding.api.RpcProviderService;
46 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.northbound.daeximoffsitebackup.rev180926.DaeximOffsiteBackupService;
47 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.northbound.daeximoffsitebackup.rev180926.RetrieveDataInput;
48 import org.opendaylight.yangtools.yang.binding.Augmentation;
49
50 public class DaeximOffsiteBackupProviderTest {
51     public DataBroker dataBroker;
52     public ReadWriteTransaction writeTransaction;
53     public FluentFuture checkedFuture;
54     public RpcProviderService rpcRegistry;
55     public DaeximOffsiteBackupProvider provider;
56     public Properties resProps;
57
58     @Before
59     public void setup() {
60         resProps = new Properties();
61         resProps.put("error-code", "200");
62         resProps.put("error-message", "Success");
63         dataBroker = mock(DataBroker.class);
64         writeTransaction = mock(ReadWriteTransaction.class);
65         checkedFuture = mock(FluentFuture.class);
66         rpcRegistry = mock(RpcProviderService.class);
67         when(rpcRegistry.registerRpcImplementation(any(), any(DaeximOffsiteBackupService.class))).thenReturn(null);
68         try {
69             when(checkedFuture.get()).thenReturn(null);
70         }
71         catch(InterruptedException | ExecutionException e) {
72             e.printStackTrace();
73         }
74         when(writeTransaction.commit()).thenReturn(checkedFuture);
75         when(dataBroker.newReadWriteTransaction()).thenReturn(writeTransaction);
76
77         provider = new DaeximOffsiteBackupProvider(dataBroker, rpcRegistry);
78     }
79
80     @Test
81     public void initializeTest() {
82         provider.initialize();
83     }
84
85     @Test
86     public void closeTest() {
87         try {
88             provider.close();
89         }
90         catch(Exception e) {
91             e.printStackTrace();
92         }
93     }
94
95     @Test
96     public void onDataTreeChangedTest() {
97         provider.onDataTreeChanged(null);
98         // onDataTreeChanged is an empty stub
99     }
100
101     @Test
102     public void backupDataTest() {
103         try {
104             assertNotNull(provider.backupData(null));
105         }
106         catch(Exception e) {
107             fail();
108         }
109         try {
110             assertNotNull(provider.backupData(null));
111         }
112         catch(Exception e) {
113             fail();
114         }
115         try {
116             assertNotNull(provider.backupData(null));
117         }
118         catch(Exception e) {
119             fail();
120         }
121         try {
122             assertNotNull(provider.backupData(null));
123         }
124         catch(Exception e) {
125             fail();
126         }
127         try {
128             assertNotNull(provider.backupData(null));
129         }
130         catch(Exception e) {
131             fail();
132         }
133     }
134
135     @Test
136     public void retrieveDataTest() {
137         RetrieveDataInput input = new RetrieveDataInput() {
138             @Override
139             public <E extends Augmentation<RetrieveDataInput>> @Nullable E augmentation(Class<E> augmentationType) {
140                 return null;
141             }
142
143             @Override
144             public String getPodName() {
145                 return "Some Pod";
146             }
147
148             @Override
149             public String getTimestamp() {
150                 return "Some Timestamp";
151             }
152
153             @Override
154             public @NonNull Map<Class<? extends Augmentation<RetrieveDataInput>>, Augmentation<RetrieveDataInput>> augmentations() {
155                 return null;
156             }
157
158         };
159
160         try {
161             assertNotNull(provider.retrieveData(input));
162         }
163         catch(Exception e) {
164             fail();
165         }
166         try {
167             assertNotNull(provider.retrieveData(input));
168         }
169         catch(Exception e) {
170             fail();
171         }
172         try {
173             assertNotNull(provider.retrieveData(input));
174         }
175         catch(Exception e) {
176             fail();
177         }
178         try {
179             assertNotNull(provider.retrieveData(input));
180         }
181         catch(Exception e) {
182             fail();
183         }
184         try {
185             assertNotNull(provider.retrieveData(input));
186         }
187         catch(Exception e) {
188             fail();
189         }
190     }
191
192     @Test
193     public void archiveOperationsTest() {
194         List<String> files = Arrays.asList("src/test/resources/fileToZip1", "src/test/resources/fileToZip2");
195         String archive = "src/test/resources/zippedArchive.zip";
196         try {
197             Method method = provider.getClass().getDeclaredMethod("createArchive", List.class, String.class);
198             method.setAccessible(true);
199             method.invoke(provider, files, archive);
200
201         }
202         catch(NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
203             e.printStackTrace();
204             fail();
205         }
206
207         try {
208             Field field = provider.getClass().getDeclaredField("DAEXIM_DIR");
209             field.setAccessible(true);
210             field.set(provider, "");
211             Method method = provider.getClass().getDeclaredMethod("extractArchive", String.class);
212             method.setAccessible(true);
213             method.invoke(provider, archive);
214         }
215         catch(NoSuchMethodException | IllegalAccessException | InvocationTargetException | NoSuchFieldException e) {
216             fail();
217         }
218         finally {
219             File zip = new File(archive);
220             zip.delete();
221         }
222     }
223 }