65d0dcaf29fd18cd7c69ce215d154f7df3c03ba5
[ccsdk/sli/northbound.git] / daexim-offsite-backup / provider / src / test / java / org / onap / ccsdk / sli / northbound / daeximoffsitebackup / DaeximOffsiteBackupProviderTest.java
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 org.eclipse.jdt.annotation.Nullable;
25 import org.junit.Before;
26 import org.junit.Test;
27
28 import org.opendaylight.controller.md.sal.binding.api.*;
29 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
30 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
31
32 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.northbound.daeximoffsitebackup.rev180926.DaeximOffsiteBackupService;
33
34 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.northbound.daeximoffsitebackup.rev180926.RetrieveDataInput;
35
36 import org.opendaylight.yangtools.yang.binding.Augmentation;
37 import org.opendaylight.yangtools.yang.binding.DataContainer;
38
39 import com.google.common.util.concurrent.CheckedFuture;
40
41 import java.io.File;
42 import java.lang.reflect.Field;
43 import java.lang.reflect.InvocationTargetException;
44 import java.lang.reflect.Method;
45 import java.util.Arrays;
46 import java.util.List;
47 import java.util.Properties;
48 import java.util.concurrent.ExecutionException;
49
50 import static org.mockito.Mockito.*;
51 import static org.junit.Assert.*;
52
53 public class DaeximOffsiteBackupProviderTest {
54     public DataBroker dataBroker;
55     public ReadWriteTransaction writeTransaction;
56     public CheckedFuture<Void, TransactionCommitFailedException> checkedFuture;
57     public RpcProviderRegistry rpcRegistry;
58     public DaeximOffsiteBackupProvider provider;
59     public Properties resProps;
60
61     @Before
62     public void setup() {
63         resProps = new Properties();
64         resProps.put("error-code", "200");
65         resProps.put("error-message", "Success");
66         dataBroker = mock(DataBroker.class);
67         writeTransaction = mock(ReadWriteTransaction.class);
68         checkedFuture = mock(CheckedFuture.class);
69         rpcRegistry = mock(RpcProviderRegistry.class);
70         when(rpcRegistry.addRoutedRpcImplementation(any(), any(DaeximOffsiteBackupService.class))).thenReturn(null);
71         try {
72             when(checkedFuture.get()).thenReturn(null);
73         }
74         catch(InterruptedException | ExecutionException e) {
75             e.printStackTrace();
76         }
77         when(writeTransaction.submit()).thenReturn(checkedFuture);
78         when(dataBroker.newReadWriteTransaction()).thenReturn(writeTransaction);
79
80         provider = new DaeximOffsiteBackupProvider(dataBroker, rpcRegistry);
81     }
82
83     @Test
84     public void initializeTest() {
85         provider.initialize();
86     }
87
88     @Test
89     public void closeTest() {
90         try {
91             provider.close();
92         }
93         catch(Exception e) {
94             e.printStackTrace();
95         }
96     }
97
98     @Test
99     public void onDataTreeChangedTest() {
100         provider.onDataTreeChanged(null);
101         // onDataTreeChanged is an empty stub
102     }
103
104     @Test
105     public void backupDataTest() {
106         try {
107             assertNotNull(provider.backupData(null));
108         }
109         catch(Exception e) {
110             fail();
111         }
112         try {
113             assertNotNull(provider.backupData(null));
114         }
115         catch(Exception e) {
116             fail();
117         }
118         try {
119             assertNotNull(provider.backupData(null));
120         }
121         catch(Exception e) {
122             fail();
123         }
124         try {
125             assertNotNull(provider.backupData(null));
126         }
127         catch(Exception e) {
128             fail();
129         }
130         try {
131             assertNotNull(provider.backupData(null));
132         }
133         catch(Exception e) {
134             fail();
135         }
136     }
137
138     @Test
139     public void retrieveDataTest() {
140         RetrieveDataInput input = new RetrieveDataInput() {
141             @Override
142             public <E extends Augmentation<RetrieveDataInput>> @Nullable E augmentation(Class<E> augmentationType) {
143                 return null;
144             }
145
146             @Override
147             public String getPodName() {
148                 return "Some Pod";
149             }
150
151             @Override
152             public String getTimestamp() {
153                 return "Some Timestamp";
154             }
155
156             @Override
157             public Class<? extends DataContainer> getImplementedInterface() {
158                 return null;
159             }
160         };
161         try {
162             assertNotNull(provider.retrieveData(input));
163         }
164         catch(Exception e) {
165             fail();
166         }
167         try {
168             assertNotNull(provider.retrieveData(input));
169         }
170         catch(Exception e) {
171             fail();
172         }
173         try {
174             assertNotNull(provider.retrieveData(input));
175         }
176         catch(Exception e) {
177             fail();
178         }
179         try {
180             assertNotNull(provider.retrieveData(input));
181         }
182         catch(Exception e) {
183             fail();
184         }
185         try {
186             assertNotNull(provider.retrieveData(input));
187         }
188         catch(Exception e) {
189             fail();
190         }
191     }
192
193     @Test
194     public void archiveOperationsTest() {
195         List<String> files = Arrays.asList("src/test/resources/fileToZip1", "src/test/resources/fileToZip2");
196         String archive = "src/test/resources/zippedArchive.zip";
197         try {
198             Method method = provider.getClass().getDeclaredMethod("createArchive", List.class, String.class);
199             method.setAccessible(true);
200             method.invoke(provider, files, archive);
201
202         }
203         catch(NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
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 }