d070c30e23a78085353201c8c1199eeda5e643cb
[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.CheckedFuture;
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.Properties;
37 import java.util.concurrent.ExecutionException;
38 import org.eclipse.jdt.annotation.Nullable;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
42 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
43 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
44 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
45 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.northbound.daeximoffsitebackup.rev180926.DaeximOffsiteBackupService;
46 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.northbound.daeximoffsitebackup.rev180926.RetrieveDataInput;
47 import org.opendaylight.yangtools.yang.binding.Augmentation;
48
49 public class DaeximOffsiteBackupProviderTest {
50     public DataBroker dataBroker;
51     public ReadWriteTransaction writeTransaction;
52     public CheckedFuture<Void, TransactionCommitFailedException> checkedFuture;
53     public RpcProviderRegistry rpcRegistry;
54     public DaeximOffsiteBackupProvider provider;
55     public Properties resProps;
56
57     @Before
58     public void setup() {
59         resProps = new Properties();
60         resProps.put("error-code", "200");
61         resProps.put("error-message", "Success");
62         dataBroker = mock(DataBroker.class);
63         writeTransaction = mock(ReadWriteTransaction.class);
64         checkedFuture = mock(CheckedFuture.class);
65         rpcRegistry = mock(RpcProviderRegistry.class);
66         when(rpcRegistry.addRoutedRpcImplementation(any(), any(DaeximOffsiteBackupService.class))).thenReturn(null);
67         try {
68             when(checkedFuture.get()).thenReturn(null);
69         }
70         catch(InterruptedException | ExecutionException e) {
71             e.printStackTrace();
72         }
73         when(writeTransaction.submit()).thenReturn(checkedFuture);
74         when(dataBroker.newReadWriteTransaction()).thenReturn(writeTransaction);
75
76         provider = new DaeximOffsiteBackupProvider(dataBroker, rpcRegistry);
77     }
78
79     @Test
80     public void initializeTest() {
81         provider.initialize();
82     }
83
84     @Test
85     public void closeTest() {
86         try {
87             provider.close();
88         }
89         catch(Exception e) {
90             e.printStackTrace();
91         }
92     }
93
94     @Test
95     public void onDataTreeChangedTest() {
96         provider.onDataTreeChanged(null);
97         // onDataTreeChanged is an empty stub
98     }
99
100     @Test
101     public void backupDataTest() {
102         try {
103             assertNotNull(provider.backupData(null));
104         }
105         catch(Exception e) {
106             fail();
107         }
108         try {
109             assertNotNull(provider.backupData(null));
110         }
111         catch(Exception e) {
112             fail();
113         }
114         try {
115             assertNotNull(provider.backupData(null));
116         }
117         catch(Exception e) {
118             fail();
119         }
120         try {
121             assertNotNull(provider.backupData(null));
122         }
123         catch(Exception e) {
124             fail();
125         }
126         try {
127             assertNotNull(provider.backupData(null));
128         }
129         catch(Exception e) {
130             fail();
131         }
132     }
133
134     @Test
135     public void retrieveDataTest() {
136         RetrieveDataInput input = new RetrieveDataInput() {
137             @Override
138             public <E extends Augmentation<RetrieveDataInput>> @Nullable E augmentation(Class<E> augmentationType) {
139                 return null;
140             }
141
142             @Override
143             public String getPodName() {
144                 return "Some Pod";
145             }
146
147             @Override
148             public String getTimestamp() {
149                 return "Some Timestamp";
150             }
151
152
153         };
154         try {
155             assertNotNull(provider.retrieveData(input));
156         }
157         catch(Exception e) {
158             fail();
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     }
185
186     @Test
187     public void archiveOperationsTest() {
188         List<String> files = Arrays.asList("src/test/resources/fileToZip1", "src/test/resources/fileToZip2");
189         String archive = "src/test/resources/zippedArchive.zip";
190         try {
191             Method method = provider.getClass().getDeclaredMethod("createArchive", List.class, String.class);
192             method.setAccessible(true);
193             method.invoke(provider, files, archive);
194
195         }
196         catch(NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
197             fail();
198         }
199
200         try {
201             Field field = provider.getClass().getDeclaredField("DAEXIM_DIR");
202             field.setAccessible(true);
203             field.set(provider, "");
204             Method method = provider.getClass().getDeclaredMethod("extractArchive", String.class);
205             method.setAccessible(true);
206             method.invoke(provider, archive);
207         }
208         catch(NoSuchMethodException | IllegalAccessException | InvocationTargetException | NoSuchFieldException e) {
209             fail();
210         }
211         finally {
212             File zip = new File(archive);
213             zip.delete();
214         }
215     }
216 }