RA: Add capability to assign new numbers for range
[ccsdk/sli/adaptors.git] / resource-assignment / provider / src / test / java / jtest / org / onap / ccsdk / sli / adaptors / ra / DataSetup.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 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 jtest.org.onap.ccsdk.sli.adaptors.ra;
23
24 import java.util.Date;
25 import jtest.util.org.onap.ccsdk.sli.adaptors.ra.TestDb;
26 import jtest.util.org.onap.ccsdk.sli.adaptors.ra.TestTable;
27
28 public class DataSetup {
29
30     private TestDb testDb;
31
32     private TestTable resource = null;
33     private TestTable allocationItem = null;
34
35     private static final String[] RESOURCE_COLUMNS =
36         {"asset_id", "resource_name", "resource_type", "lt_used", "rr_used"};
37
38     private static final String[] ALLOCATION_ITEM_COLUMNS = {"resource_id", "application_id", "resource_set_id",
39             "resource_union_id", "resource_share_group_list", "lt_used", "rr_used", "allocation_time"};
40
41     private void initTables() {
42         if (resource == null) {
43             resource = testDb.table("RESOURCE", "resource_id", RESOURCE_COLUMNS);
44         }
45         if (allocationItem == null) {
46             allocationItem = testDb.table("ALLOCATION_ITEM", "allocation_item_id", ALLOCATION_ITEM_COLUMNS);
47         }
48     }
49
50     public void cleanup() {
51         initTables();
52         allocationItem.delete("true");
53         resource.delete("true");
54     }
55
56     public void setupLimitItem(String resourceName, String assetId, String resourceSetId, String resourceUnionId,
57             long used) {
58         initTables();
59
60         Long rid = resource.getId("asset_id = '" + assetId + "' AND resource_name = '" + resourceName + "'");
61         if (rid == null) {
62             resource.add(assetId, resourceName, "Limit", used, null);
63             rid = resource.getLastId();
64         }
65         allocationItem.add(rid, "SDNC", resourceSetId, resourceUnionId, null, used, null, new Date());
66     }
67
68     public void setupRangeItem(String resourceName, String assetId, String resourceSetId, String resourceUnionId,
69             String used) {
70         initTables();
71
72         Long rid = resource.getId("asset_id = '" + assetId + "' AND resource_name = '" + resourceName + "'");
73         if (rid == null) {
74             resource.add(assetId, resourceName, "Range", null, used);
75             rid = resource.getLastId();
76         }
77         allocationItem.add(rid, "SDNC", resourceSetId, resourceUnionId, null, null, used, new Date());
78     }
79
80     public boolean checkRangeItem(String resourceName, String assetId, String resourceSetId, String used) {
81         String where = "resource_id = (SELECT resource_id FROM RESOURCE WHERE resource_name = '" + resourceName
82                 + "' AND asset_id = '" + assetId + "') AND resource_set_id = '" + resourceSetId + "'";
83         Object usedInDb = allocationItem.getColumn("rr_used", where);
84         return used.equals(usedInDb);
85     }
86
87     public void setTestDb(TestDb testDb) {
88         this.testDb = testDb;
89     }
90 }