Merge "Include owner in createLockRef"
[music.git] / music-core / src / test / java / org / onap / music / datastore / jsonobjects / JsonUpdateTest.java
1 /*******************************************************************************
2  * ============LICENSE_START==========================================
3  * org.onap.music
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
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.*;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
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;
41
42 public class JsonUpdateTest {
43     
44     JsonUpdate ju = null;
45     
46     @Before
47     public void init() {
48         ju = new JsonUpdate();
49     }
50     
51
52     @Test
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"));
59     }
60
61     @Test
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"));
68     }
69
70     @Test
71     public void testGetKeyspaceName() {
72         String keyspace = "keyspace";
73         ju.setKeyspaceName(keyspace);
74         assertEquals(keyspace,ju.getKeyspaceName());
75     }
76
77     @Test
78     public void testGetTableName() {
79         String table = "table";
80         ju.setTableName(table);
81         assertEquals(table,ju.getTableName());
82    }
83
84     @Test
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"));
90     }
91
92     @Test
93     public void testGetTtl() {
94         ju.setTtl("2000");
95         assertEquals("2000",ju.getTtl());
96     }
97
98     @Test
99     public void testGetTimestamp() {
100         ju.setTimestamp("20:00");
101         assertEquals("20:00",ju.getTimestamp());
102
103     }
104
105     @Test
106     public void testGetValues() {
107         Map<String,Object> cons = new HashMap<>();
108         cons.put("val1","one");
109         cons.put("val2","two");
110         ju.setValues(cons);
111         assertEquals("one",ju.getValues().get("val1"));
112     }
113     
114     @Test
115     public void testSerialize() {
116         assertTrue(ju.serialize() instanceof byte[]);
117     }
118
119     @Test
120     public void testRowIdString() {
121         ju.setRowIdString("testing");
122         assertEquals("testing", ju.getRowIdString());
123     }
124
125     @Test
126     public void testPrimaryKeyValue() {
127         ju.setPrimarKeyValue("primeKey");
128         assertEquals("primeKey", ju.getPrimarKeyValue());
129     }
130
131     @Test
132     public void testGenUpdatePreparedQueryObj() throws Exception {
133         JsonUpdate ju = Mockito.spy(JsonUpdate.class);
134         MultivaluedMap<String, String> rowParams = Mockito.mock(MultivaluedMap.class);
135  
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);
145         
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<>();
151         colList.add(cmd);
152         Mockito.when(tmd.getPrimaryKey()).thenReturn(colList);
153         Mockito.when(cmd.getType()).thenReturn(DataType.varchar());
154         
155         RowIdentifier rowId = Mockito.mock(RowIdentifier.class);
156         Mockito.doReturn(rowId).when(ju).getRowIdentifier(Mockito.anyString(), Mockito.anyString(), Mockito.any(),
157                 Mockito.any());
158         
159         Mockito.when(rowId.getRowIdString()).thenReturn("col1");
160         Mockito.when(rowId.getPrimaryKeyValue()).thenReturn("val1");
161
162         
163         assertEquals("UPDATE keyspace.table  SET vector_ts=?,col1= ? WHERE col1;",
164                 ju.genUpdatePreparedQueryObj(rowParams).getQuery());
165     }
166 }