Improve testing stability
[sdc.git] / catalog-dao / src / test / java / org / openecomp / sdc / be / dao / cassandra / ArtifactCassandraDaoTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.dao.cassandra;
22
23 import com.datastax.driver.core.ResultSet;
24 import com.datastax.driver.core.Row;
25 import com.datastax.driver.core.Session;
26 import com.datastax.driver.mapping.MappingManager;
27 import fj.data.Either;
28 import org.apache.commons.lang3.tuple.ImmutablePair;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.mockito.InjectMocks;
32 import org.mockito.Mock;
33 import org.mockito.Mockito;
34 import org.mockito.MockitoAnnotations;
35 import org.openecomp.sdc.be.resources.data.DAOArtifactData;
36 import org.openecomp.sdc.be.resources.data.auditing.AuditingTypesConstants;
37
38 public class ArtifactCassandraDaoTest {
39
40         @InjectMocks
41         private ArtifactCassandraDao testSubject;
42         
43         @Mock
44         private CassandraClient client;
45         
46         @Mock
47         private ArtifactAccessor artifactAccessor;
48
49         @Mock
50         private MappingManager mappingManager;
51         
52         @Before
53         public void setUp() throws Exception {
54                 MockitoAnnotations.openMocks(this);
55         }
56
57         @Test(expected = RuntimeException.class)
58         public void testInit() throws Exception {
59                 Mockito.when(client.isConnected()).thenReturn(true);
60                 Either<ImmutablePair<Session, MappingManager>, CassandraOperationStatus> value = Either.right(CassandraOperationStatus.CLUSTER_NOT_CONNECTED);
61                 Mockito.when(client.connect(Mockito.anyString())).thenReturn(value);
62                 testSubject.init();
63         }
64         
65         @Test
66         public void testInitError() throws Exception {
67                 testSubject.init();
68         }
69
70         @Test
71         public void testSaveArtifact() throws Exception {
72                 DAOArtifactData artifact = null;
73                 CassandraOperationStatus result;
74
75                 // default test
76                 result = testSubject.saveArtifact(artifact);
77         }
78
79         @Test
80         public void testGetArtifact() throws Exception {
81                 String artifactId = "";
82                 Either<DAOArtifactData, CassandraOperationStatus> result;
83
84                 // default test
85                 result = testSubject.getArtifact(artifactId);
86         }
87
88         @Test
89         public void testDeleteArtifact() throws Exception {
90                 String artifactId = "";
91                 CassandraOperationStatus result;
92
93                 // default test
94                 result = testSubject.deleteArtifact(artifactId);
95         }
96
97         @Test
98         public void testDeleteAllArtifacts() throws Exception {
99                 CassandraOperationStatus result;
100
101                 // default test
102                 result = testSubject.deleteAllArtifacts();
103         }
104
105         @Test
106         public void testIsTableEmpty() throws Exception {
107                 String tableName = "";
108                 Either<Boolean, CassandraOperationStatus> result;
109
110                 // default test
111                 result = testSubject.isTableEmpty(tableName);
112         }
113
114         @Test
115         public void testGetCountOfArtifactById() throws Exception {
116                 Mockito.when(client.isConnected()).thenReturn(true);
117                 Mockito.when(client.connect(AuditingTypesConstants.ARTIFACT_KEYSPACE)).thenReturn(Either.left(ImmutablePair.of(null,mappingManager)));
118                 Mockito.when(mappingManager.createAccessor(ArtifactAccessor.class)).thenReturn(artifactAccessor);
119                 String uniqeId = "mock";
120                 Either<Long, CassandraOperationStatus> result;
121                 ResultSet value = Mockito.mock(ResultSet.class);
122                 Row value2 = Mockito.mock(Row.class);
123                 Mockito.when(value2.getLong(0)).thenReturn(0L);
124                 Mockito.when(value.one()).thenReturn(value2);
125                 Mockito.when(artifactAccessor.getNumOfArtifactsById(uniqeId)).thenReturn(value);
126                 
127                 // default test
128                 testSubject.init();
129                 result = testSubject.getCountOfArtifactById(uniqeId);
130         }
131         
132         @Test
133         public void testGetCountOfArtifactById1() throws Exception {
134                 Mockito.when(client.isConnected()).thenReturn(true);
135                 Mockito.when(client.connect(AuditingTypesConstants.ARTIFACT_KEYSPACE)).thenReturn(Either.left(ImmutablePair.of(null,mappingManager)));
136                 Mockito.when(mappingManager.createAccessor(ArtifactAccessor.class)).thenReturn(artifactAccessor);
137                 String uniqeId = "mock";
138                 Either<Long, CassandraOperationStatus> result;
139                 ResultSet value = Mockito.mock(ResultSet.class);
140                 Mockito.when(artifactAccessor.getNumOfArtifactsById(uniqeId)).thenReturn(null);
141                 testSubject.init();
142                 // default test
143                 result = testSubject.getCountOfArtifactById(uniqeId);
144         }
145 }