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