e85ed8cdd075dd4b5a8fb04f47343d33ce11bf3e
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2019 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.assertNotNull;
26 import static org.junit.Assert.assertNull;
27 import java.io.IOException;
28 import java.util.List;
29 import java.util.concurrent.TimeUnit;
30 import org.junit.BeforeClass;
31 import org.junit.Test;
32 import org.onap.ccsdk.features.sdnr.wt.common.database.HtDatabaseClient;
33 import org.onap.ccsdk.features.sdnr.wt.common.database.config.HostInfo;
34 import org.onap.ccsdk.features.sdnr.wt.common.database.queries.QueryBuilders;
35 import org.onap.ccsdk.features.sdnr.wt.common.database.requests.DeleteByQueryRequest;
36 import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.DatabaseDataProvider;
37 import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.elasticsearch.impl.ElasticSearchDataProvider;
38 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.HtDatabaseMaintenance;
39 import org.onap.ccsdk.features.sdnr.wt.dataprovider.test.util.HostInfoForTest;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Entity;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.MaintenanceBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.MaintenanceEntity;
43
44 /**
45  * - Handling of inital values for Maintenance mode.
46  */
47 public class TestMaintenanceServiceData {
48
49     private static DatabaseDataProvider dbProvider;
50     private static HtDatabaseClient dbRawProvider;
51     private static HtDatabaseMaintenance service = null;
52
53     private static final String NODEID = "tmsnode1";
54     private static final String NODEID2 = "tmsnode2";
55
56     @BeforeClass
57     public static void init() throws Exception {
58
59         HostInfo[] hosts = HostInfoForTest.get();
60         dbProvider = new ElasticSearchDataProvider(hosts);
61         dbProvider.waitForYellowDatabaseStatus(30, TimeUnit.SECONDS);
62         dbRawProvider = HtDatabaseClient.getClient(hosts);
63         service = dbProvider.getHtDatabaseMaintenance();
64     }
65
66     @Test
67     public void test() throws InterruptedException {
68         clearDbEntity(Entity.Maintenancemode);
69         MaintenanceEntity obj = service.createIfNotExists(NODEID);
70         assertNotNull("Create first id", obj);
71         obj = service.createIfNotExists(NODEID2);
72         assertNotNull("Create second id", obj);
73         obj = service.getMaintenance(NODEID);
74         assertNotNull(obj);
75         List<MaintenanceEntity> list = service.getAll();
76         assertEquals("Verify for two ids", 2, list.size());
77         service.deleteIfNotRequired(NODEID);
78         obj = service.getMaintenance(NODEID);
79         assertNull("Check if first id was removed", obj);
80
81         obj = service.setMaintenance(createMaintenance(NODEID, true));
82
83
84     }
85
86     /**
87      * @param nodeId
88      * @param active
89      * @return
90      */
91     private static MaintenanceEntity createMaintenance(String nodeId, Boolean active) {
92         return new MaintenanceBuilder().setNodeId(nodeId).setActive(active).setProblem("problem")
93                 .setObjectIdRef("idref").build();
94     }
95
96     /**
97      * Delete
98      *
99      * @param entity
100      */
101     private static void clearDbEntity(Entity entity) {
102         DeleteByQueryRequest query = new DeleteByQueryRequest(entity.getName());
103         query.setQuery(QueryBuilders.matchAllQuery().toJSON());
104         try {
105             dbRawProvider.deleteByQuery(query);
106         } catch (IOException e) {
107             e.printStackTrace();
108         }
109         TestCRUDforDatabase.trySleep(1000);
110     }
111
112 }