610f5476cde305925da3c986bb1c05e51b4c2a76
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2021 highstreet technologies GmbH Intellectual Property.
6  * All rights 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.features.sdnr.wt.dataprovider.test;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.fail;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.when;
28 import com.google.common.util.concurrent.FluentFuture;
29 import java.io.File;
30 import java.io.IOException;
31 import java.nio.file.Files;
32 import java.nio.file.StandardOpenOption;
33 import java.util.Optional;
34 import java.util.concurrent.ExecutionException;
35 import org.eclipse.jdt.annotation.NonNull;
36 import org.junit.AfterClass;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39 import org.onap.ccsdk.features.sdnr.wt.dataprovider.http.MsServlet;
40 import org.onap.ccsdk.features.sdnr.wt.dataprovider.impl.DataProviderServiceImpl;
41 import org.onap.ccsdk.features.sdnr.wt.dataprovider.test.util.HostInfoForTest;
42 import org.onap.ccsdk.features.sdnr.wt.yang.mapper.YangToolsMapper;
43 import org.opendaylight.mdsal.binding.api.DataBroker;
44 import org.opendaylight.mdsal.binding.api.ReadTransaction;
45 import org.opendaylight.mdsal.binding.api.RpcProviderService;
46 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.Keystore;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadTlsKeyEntryInput;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadTlsKeyEntryInputBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ReadTlsKeyEntryOutput;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.entity.input.Pagination;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.entity.input.PaginationBuilder;
53 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
54 import org.opendaylight.yangtools.yang.binding.DataObject;
55 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
56 import org.opendaylight.yangtools.yang.common.RpcResult;
57 import org.opendaylight.yangtools.yang.common.Uint32;
58 import org.opendaylight.yangtools.yang.common.Uint64;
59
60 public class TestGetTlsKeys {
61
62     private static final int DBPORT = HostInfoForTest.get()[0].port;
63     private static RpcProviderService rpcProviderService = mock(RpcProviderService.class);
64     private static MsServlet msServlet = mock(MsServlet.class);
65     private static DataBroker dataBroker = mock(DataBroker.class);
66     private static DataProviderServiceImpl dataProvider = null;
67
68     @BeforeClass
69     public static void init() {
70         String configContent = "[es]\n" + "esHosts=" + String.format("http://localhost:%d", DBPORT) + "\n"
71                 + "esArchiveLifetimeSeconds=2592000\n" + "esCluster=\n" + "esArchiveCheckIntervalSeconds=0\n"
72                 + "esNode=elasticsearchnode\n" + "esAuthUsername=${SDNRDBUSERNAME}\n"
73                 + "esAuthPassword=${SDNRDBPASSWORD}\n" + "esTrustAllCerts=${SDNRDBTRUSTALLCERTS}\n" + "";
74         try {
75             finish();
76             Files.createDirectories(new File("etc").toPath());
77             Files.write(new File(DataProviderServiceImpl.CONFIGURATIONFILE).toPath(), configContent.getBytes(),
78                     StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW);
79         } catch (IOException e) {
80             e.printStackTrace();
81             fail(e.getMessage());
82         }
83
84         when(dataBroker.newReadOnlyTransaction())
85                 .thenReturn(new ReadHelperTransaction("src/test/resources/tlskeys/keys1.json", Keystore.class));
86         try {
87             dataProvider = new DataProviderServiceImpl(rpcProviderService, msServlet, dataBroker);
88         } catch (Exception e) {
89             e.printStackTrace();
90             fail(e.getMessage());
91         }
92
93     }
94
95     @AfterClass
96     public static void finish() {
97         File f = new File(DataProviderServiceImpl.CONFIGURATIONFILE);
98         if (f.exists()) {
99             f.delete();
100         }
101         File folder = new File("etc");
102         if (folder.exists()) {
103             folder.delete();
104         }
105     }
106
107     @Test
108     public void test() {
109          Pagination pagination = new PaginationBuilder().setPage(Uint64.valueOf(1)).setSize(Uint32.valueOf(20)).build();
110         ReadTlsKeyEntryInput firstPageInput = new ReadTlsKeyEntryInputBuilder().setPagination(pagination).build();
111         RpcResult<ReadTlsKeyEntryOutput> output = null;
112         try {
113             output = dataProvider.readTlsKeyEntry(firstPageInput).get();
114         } catch (InterruptedException | ExecutionException e) {
115             fail(e.getMessage());
116         }
117         ReadTlsKeyEntryOutput result = output.getResult();
118         assertEquals(4,result.getPagination().getTotal().longValue());
119         assertEquals(4, result.getData().size());
120         assertEquals(1, result.getPagination().getPage().longValue());
121         assertEquals(4, result.getPagination().getSize().longValue());
122     }
123     @Test
124     public void test2() {
125          Pagination pagination = new PaginationBuilder().setPage(Uint64.valueOf(1)).setSize(Uint32.valueOf(2)).build();
126         ReadTlsKeyEntryInput firstPageInput = new ReadTlsKeyEntryInputBuilder().setPagination(pagination).build();
127         RpcResult<ReadTlsKeyEntryOutput> output = null;
128         try {
129             output = dataProvider.readTlsKeyEntry(firstPageInput).get();
130         } catch (InterruptedException | ExecutionException e) {
131             fail(e.getMessage());
132         }
133         ReadTlsKeyEntryOutput result = output.getResult();
134         assertEquals(4,result.getPagination().getTotal().longValue());
135         assertEquals(2, result.getData().size());
136         assertEquals(1, result.getPagination().getPage().longValue());
137         assertEquals(2, result.getPagination().getSize().longValue());
138     }
139
140     @Test
141     public void test3() {
142          Pagination pagination = new PaginationBuilder().setPage(Uint64.valueOf(2)).setSize(Uint32.valueOf(3)).build();
143         ReadTlsKeyEntryInput firstPageInput = new ReadTlsKeyEntryInputBuilder().setPagination(pagination).build();
144         RpcResult<ReadTlsKeyEntryOutput> output = null;
145         try {
146             output = dataProvider.readTlsKeyEntry(firstPageInput).get();
147         } catch (InterruptedException | ExecutionException e) {
148             fail(e.getMessage());
149         }
150         ReadTlsKeyEntryOutput result = output.getResult();
151         assertEquals(4,result.getPagination().getTotal().longValue());
152         assertEquals(1, result.getData().size());
153         assertEquals(2, result.getPagination().getPage().longValue());
154         assertEquals(1, result.getPagination().getSize().longValue());
155     }
156
157     private static class ReadHelperTransaction implements ReadTransaction {
158         private final String filename;
159         private final YangToolsMapper mapper;
160         private final Class<?> clazz;
161
162         public ReadHelperTransaction(String filename, Class<?> clazz) {
163             this.filename = filename;
164             this.clazz = clazz;
165             this.mapper = new YangToolsMapper();
166         }
167
168         @Override
169         public @NonNull Object getIdentifier() {
170             // TODO Auto-generated method stub
171             return null;
172         }
173
174         @SuppressWarnings("unchecked")
175         @Override
176         public <T extends DataObject> @NonNull FluentFuture<Optional<T>> read(@NonNull LogicalDatastoreType store,
177                 @NonNull InstanceIdentifier<T> path) {
178
179             T result = null;
180             try {
181                 result = (T) this.mapper.readValue(new File(filename), this.clazz);
182             } catch (IOException e) {
183                 return FluentFutures.immediateFluentFuture(Optional.empty());
184             }
185             return FluentFutures.immediateFluentFuture(Optional.of(result));
186         }
187
188         @Override
189         public @NonNull FluentFuture<Boolean> exists(@NonNull LogicalDatastoreType store,
190                 @NonNull InstanceIdentifier<?> path) {
191             // TODO Auto-generated method stub
192             return null;
193         }
194
195         @Override
196         public void close() {
197             // TODO Auto-generated method stub
198
199         }
200     }
201 }