Merge "Include owner in createLockRef"
[music.git] / music-core / src / test / java / org / onap / music / datastore / jsonobjects / JsonInsertTest.java
1 /*
2  * ============LICENSE_START==========================================
3  * org.onap.music
4  * ===================================================================
5  *  Copyright (c) 2017 AT&T Intellectual Property
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  * 
21  * ============LICENSE_END=============================================
22  * ====================================================================
23  */
24
25 package org.onap.music.datastore.jsonobjects;
26
27 import static org.junit.Assert.assertArrayEquals;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertTrue;
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.Map;
34
35 import org.apache.commons.lang3.SerializationUtils;
36 import org.junit.Test;
37 import org.mockito.Mockito;
38 import org.mockito.internal.util.reflection.FieldSetter;
39 import org.onap.music.datastore.MusicDataStore;
40 import org.onap.music.datastore.MusicDataStoreHandle;
41 import org.onap.music.datastore.PreparedQueryObject;
42 import org.onap.music.exceptions.MusicQueryException;
43 import org.onap.music.exceptions.MusicServiceException;
44 import com.datastax.driver.core.ColumnMetadata;
45 import com.datastax.driver.core.DataType;
46 import com.datastax.driver.core.Session;
47 //import org.mockito.internal.util.reflection.Whitebox;
48 import com.datastax.driver.core.TableMetadata;
49
50
51 public class JsonInsertTest {
52     
53     JsonInsert ji = new JsonInsert();
54
55     @Test
56     public void testGetKeyspaceName() {
57         ji.setKeyspaceName("keyspace");
58         assertEquals("keyspace",ji.getKeyspaceName());
59     }
60
61     @Test
62     public void testGetTableName() {
63         ji.setTableName("table");
64         assertEquals("table",ji.getTableName());
65     }
66
67     @Test
68     public void testGetConsistencyInfo() {
69         Map<String,String> cons = new HashMap<>();
70         cons.put("test","true");
71         ji.setConsistencyInfo(cons);
72         assertEquals("true",ji.getConsistencyInfo().get("test"));
73     }
74
75     @Test
76     public void testGetTtl() {
77         ji.setTtl("ttl");
78         assertEquals("ttl",ji.getTtl());
79     }
80
81     @Test
82     public void testGetTimestamp() {
83         ji.setTimestamp("10:30");
84         assertEquals("10:30",ji.getTimestamp());
85     }
86
87     @Test
88     public void testGetValues() {
89         Map<String,Object> cons = new HashMap<>();
90         cons.put("val1","one");
91         cons.put("val2","two");
92         ji.setValues(cons);
93         assertEquals("one",ji.getValues().get("val1"));
94     }
95
96     @Test
97     public void testGetRowSpecification() {
98         Map<String,Object> cons = new HashMap<>();
99         cons.put("val1","one");
100         cons.put("val2","two");
101         ji.setRowSpecification(cons);
102         assertEquals("two",ji.getRowSpecification().get("val2"));
103     }
104
105     @Test
106     public void testSerialize() {
107         Map<String,Object> cons = new HashMap<>();
108         cons.put("val1","one");
109         cons.put("val2","two");
110         ji.setTimestamp("10:30");
111         ji.setRowSpecification(cons);
112         byte[] test1 = ji.serialize();
113         byte[] ji1 = SerializationUtils.serialize(ji);
114         assertArrayEquals(ji1,test1);
115     }
116
117     @Test
118     public void testObjectMap()
119     {
120         Map<String, byte[]> map = new HashMap<>();
121         ji.setObjectMap(map);
122         assertEquals(map, ji.getObjectMap());
123     }
124     
125     @Test
126     public void testPrimaryKey() {
127         ji.setPrimaryKeyVal("primKey");
128         assertEquals("primKey", ji.getPrimaryKeyVal());
129     }
130     
131     @Test
132     public void testGenInsertPreparedQueryObj() throws Exception {
133         ji.setKeyspaceName("keyspace");
134         ji.setTableName("table");
135         ji.setPrimaryKeyVal("value");
136         Map<String,Object> rowSpec = new HashMap<>();
137         rowSpec.put("val1","one");
138         rowSpec.put("val2","two");
139         ji.setRowSpecification(rowSpec);
140         Map<String,Object> vals = new HashMap<>();
141         vals.put("val1","one");
142         vals.put("val2","two");
143         ji.setValues(vals);
144         
145         Map<String,String> cons = new HashMap<>();
146         cons.put("type","quorum");
147         ji.setConsistencyInfo(cons);
148         
149         MusicDataStore mds = Mockito.mock(MusicDataStore.class);
150         Session session = Mockito.mock(Session.class);
151         Mockito.when(mds.getSession()).thenReturn(session);
152         MusicDataStoreHandle mdsh = Mockito.mock(MusicDataStoreHandle.class);
153         FieldSetter.setField(mdsh, mdsh.getClass().getDeclaredField("mDstoreHandle"), mds);
154         TableMetadata tableMeta = Mockito.mock(TableMetadata.class);
155         Mockito.when(mds.returnColumnMetadata(Mockito.anyString(), Mockito.anyString()))
156             .thenReturn(tableMeta);
157         
158         ColumnMetadata cmd = Mockito.mock(ColumnMetadata.class);
159         List<ColumnMetadata> listcmd = new ArrayList<>();
160         listcmd.add(cmd);
161         Mockito.when(tableMeta.getPrimaryKey()).thenReturn(listcmd);
162         Mockito.when(cmd.getName()).thenReturn("val1");
163         Mockito.when(tableMeta.getColumn("val1")).thenReturn(cmd);
164         Mockito.when(tableMeta.getColumn("val2")).thenReturn(cmd);
165         Mockito.when(cmd.getType()).thenReturn(DataType.text());
166         
167         PreparedQueryObject query = ji.genInsertPreparedQueryObj();
168         System.out.println(query.getQuery());
169         System.out.println(query.getValues());
170
171
172         assertEquals("INSERT INTO keyspace.table (vector_ts,val2,val1) VALUES (?,?,?);", query.getQuery());
173         assertTrue(query.getValues().containsAll(vals.values()));
174     }
175
176 }