Added additional test case - DBFieldHandlerTest
[dmaap/dbcapi.git] / src / test / java / org / onap / dmaap / dbcapi / database / DBFieldHandlerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 IBM
8  * ===================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.dmaap.dbcapi.database;
24
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNull;
27
28 import java.sql.PreparedStatement;
29 import java.sql.ResultSet;
30
31 import org.apache.log4j.Logger;
32 import org.junit.Test;
33 import org.onap.dmaap.dbcapi.authentication.AafLurAndFish;
34 import org.onap.dmaap.dbcapi.model.ReplicationType;
35 import org.onap.dmaap.dbcapi.testframework.ReflectionHarness;
36
37 public class DBFieldHandlerTest {
38
39     private static final Logger logger = Logger.getLogger(AafLurAndFish.class);
40
41     private static final String fmt = "%24s: %s%n";
42
43     ReflectionHarness rh = new ReflectionHarness();
44
45     private static class TopicReplicationTypeHandler implements DBFieldHandler.SqlOp {
46         public Object get(ResultSet rs, int index) throws Exception {
47             int val = rs.getInt(index);
48
49             return (ReplicationType.valueOf(val));
50         }
51
52         public void set(PreparedStatement ps, int index, Object val) throws Exception {
53             if (val == null) {
54                 ps.setInt(index, 0);
55                 return;
56             }
57             @SuppressWarnings("unchecked")
58             ReplicationType rep = (ReplicationType) val;
59             ps.setInt(index, rep.getValue());
60         }
61     }
62
63     @Test
64     public void test1() {
65         // rh.reflect( "org.onap.dmaap.dbcapi.aaf.client.MrTopicConnection", "get",
66         // "idNotSet@namespaceNotSet:pwdNotSet" );
67     }
68
69     @Test
70     public void test2() {
71         String v = "Validate";
72         // rh.reflect( "org.onap.dmaap.dbcapi.aaf.client.MrTopicConnection", "set", v );
73     }
74
75     @Test
76     public void test3() {
77         try {
78             DBFieldHandler fh = new DBFieldHandler(String.class, "aString", 1);
79         } catch (Exception e) {
80             logger.error("Error", e);
81         }
82     }
83
84     @Test
85     public void test4() {
86         try {
87             DBFieldHandler fh = new DBFieldHandler(String.class, "aString", 1, null);
88         } catch (Exception e) {
89             logger.error("Error", e);
90         }
91     }
92
93     @Test
94     public void testfesc() {
95         String sampleString = "@xyz,ww;,";
96         String finalString = DBFieldHandler.fesc(sampleString);
97         assertEquals("@axyz@cww@s@c", finalString);
98     }
99
100     @Test
101     public void testfunesc() {
102         String sampleString = "@axyz@cww@s@c";
103         String convertedString = DBFieldHandler.funesc(sampleString);
104         assertEquals("@xyz,ww;,", convertedString);
105     }
106
107     @Test
108     public void testfescWithNull() {
109         String sampleString1 = DBFieldHandler.fesc(null);
110         String sampleString2 = DBFieldHandler.funesc(null);
111         assertNull(null, sampleString1);
112         assertNull(null, sampleString2);
113     }
114 }