1 /*******************************************************************************
2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (c) 2018 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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ============LICENSE_END=============================================
22 * ====================================================================
23 *******************************************************************************/
25 package org.onap.music.datastore.jsonobjects;
27 import static org.junit.Assert.*;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.HashMap;
31 import java.util.List;
33 import javax.ws.rs.core.MultivaluedMap;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.mockito.Mockito;
37 import org.onap.music.datastore.jsonobjects.JsonUpdate.RowIdentifier;
38 import com.datastax.driver.core.ColumnMetadata;
39 import com.datastax.driver.core.DataType;
40 import com.datastax.driver.core.TableMetadata;
42 public class JsonUpdateTest {
48 ju = new JsonUpdate();
53 public void testGetConditions() {
54 Map<String,Object> mapSo = new HashMap<>();
55 mapSo.put("key1","one");
56 mapSo.put("key2","two");
57 ju.setConditions(mapSo);
58 assertEquals("one",ju.getConditions().get("key1"));
62 public void testGetRow_specification() {
63 Map<String,Object> mapSo = new HashMap<>();
64 mapSo.put("key1","one");
65 mapSo.put("key2","two");
66 ju.setRow_specification(mapSo);
67 assertEquals("one",ju.getRow_specification().get("key1"));
71 public void testGetKeyspaceName() {
72 String keyspace = "keyspace";
73 ju.setKeyspaceName(keyspace);
74 assertEquals(keyspace,ju.getKeyspaceName());
78 public void testGetTableName() {
79 String table = "table";
80 ju.setTableName(table);
81 assertEquals(table,ju.getTableName());
85 public void testGetConsistencyInfo() {
86 Map<String, String> mapSs = new HashMap<>();
87 mapSs.put("k1", "one");
88 ju.setConsistencyInfo(mapSs);
89 assertEquals("one",ju.getConsistencyInfo().get("k1"));
93 public void testGetTtl() {
95 assertEquals("2000",ju.getTtl());
99 public void testGetTimestamp() {
100 ju.setTimestamp("20:00");
101 assertEquals("20:00",ju.getTimestamp());
106 public void testGetValues() {
107 Map<String,Object> cons = new HashMap<>();
108 cons.put("val1","one");
109 cons.put("val2","two");
111 assertEquals("one",ju.getValues().get("val1"));
115 public void testSerialize() {
116 assertTrue(ju.serialize() instanceof byte[]);
120 public void testRowIdString() {
121 ju.setRowIdString("testing");
122 assertEquals("testing", ju.getRowIdString());
126 public void testPrimaryKeyValue() {
127 ju.setPrimarKeyValue("primeKey");
128 assertEquals("primeKey", ju.getPrimarKeyValue());
132 public void testGenUpdatePreparedQueryObj() throws Exception {
133 JsonUpdate ju = Mockito.spy(JsonUpdate.class);
134 MultivaluedMap<String, String> rowParams = Mockito.mock(MultivaluedMap.class);
136 ju.setKeyspaceName("keyspace");
137 ju.setTableName("table");
138 ju.setPrimarKeyValue("primaryKeyValue");
139 Map<String, String> consistencyInfo = new HashMap<>();
140 consistencyInfo.put("type", "critical");
141 ju.setConsistencyInfo(consistencyInfo);
142 Map<String, Object> values = new HashMap<>();
143 values.put("col1", "val1");
144 ju.setValues(values);
146 TableMetadata tmd = Mockito.mock(TableMetadata.class);
147 Mockito.doReturn(tmd).when(ju).returnColumnMetadata(Mockito.anyString(), Mockito.anyString());
148 ColumnMetadata cmd = Mockito.mock(ColumnMetadata.class);
149 Mockito.when(tmd.getColumn("col1")).thenReturn(cmd);
150 List<ColumnMetadata> colList = new ArrayList<>();
152 Mockito.when(tmd.getPrimaryKey()).thenReturn(colList);
153 Mockito.when(cmd.getType()).thenReturn(DataType.varchar());
155 RowIdentifier rowId = Mockito.mock(RowIdentifier.class);
156 Mockito.doReturn(rowId).when(ju).getRowIdentifier(Mockito.anyString(), Mockito.anyString(), Mockito.any(),
159 Mockito.when(rowId.getRowIdString()).thenReturn("col1");
160 Mockito.when(rowId.getPrimaryKeyValue()).thenReturn("val1");
163 assertEquals("UPDATE keyspace.table SET vector_ts=?,col1= ? WHERE col1;",
164 ju.genUpdatePreparedQueryObj(rowParams).getQuery());