1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
/*
* ============LICENSE_START=======================================================
* Copyright (C) 2021 Nordix Foundation
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* ============LICENSE_END=========================================================
*/
package org.openecomp.sdc.be.dao.cassandra;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import com.datastax.driver.core.Session;
import com.datastax.driver.mapping.Mapper;
import com.datastax.driver.mapping.MappingManager;
import com.datastax.driver.mapping.Result;
import fj.data.Either;
import java.util.List;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.openecomp.sdc.be.dao.api.exception.CassandraDaoInitException;
import org.openecomp.sdc.be.dao.api.exception.CassandraDaoInitExceptionProvider;
import org.openecomp.sdc.be.data.model.ToscaImportByModel;
import org.openecomp.sdc.be.resources.data.auditing.AuditingTypesConstants;
class ToscaModelImportCassandraDaoTest {
@Mock
private CassandraClient cassandraClient;
@InjectMocks
private ToscaModelImportCassandraDao toscaModelImportCassandraDao;
@BeforeEach
public void setUp() throws Exception {
MockitoAnnotations.openMocks(this);
}
@Test
void findAllByModelTest() {
final var toscaImportByModelAccessorMock = mock(ToscaImportByModelAccessor.class);
toscaModelImportCassandraDao = new ToscaModelImportCassandraDao(toscaImportByModelAccessorMock, null);
final var modelId = "modelId";
final ToscaImportByModel toscaImportByModel1 = new ToscaImportByModel();
final ToscaImportByModel toscaImportByModel2 = new ToscaImportByModel();
final List<ToscaImportByModel> expectedImportModelList = List.of(toscaImportByModel1, toscaImportByModel2);
final Result<ToscaImportByModel> result = mock(Result.class);
when(result.all()).thenReturn(expectedImportModelList);
when(toscaImportByModelAccessorMock.findAllByModel(modelId)).thenReturn(result);
final List<ToscaImportByModel> actualImportModelList = toscaModelImportCassandraDao.findAllByModel(modelId);
assertEquals(expectedImportModelList.size(), actualImportModelList.size());
assertTrue(actualImportModelList.contains(toscaImportByModel1));
assertTrue(actualImportModelList.contains(toscaImportByModel2));
}
@Test
void importAllTest() {
final var toscaImportByModelAccessorMock = mock(ToscaImportByModelAccessor.class);
final Mapper<ToscaImportByModel> toscaImportByModelMapperMock = mock(Mapper.class);
toscaModelImportCassandraDao = new ToscaModelImportCassandraDao(toscaImportByModelAccessorMock, toscaImportByModelMapperMock);
final var modelId = "modelId";
final var toscaImportByModel1 = createToscaModelByImport(modelId, "path/model/1");
final var toscaImportByModel2 = createToscaModelByImport(modelId, "path/model/2");
final var toscaImportByModel3 = createToscaModelByImport(modelId, "path/model/3");
final var toscaImportByModelWrongModel = createToscaModelByImport("otherModel", "path/wrong-model/1");
final var importModelList = List.of(toscaImportByModel1, toscaImportByModel2, toscaImportByModel3);
final var toscaImportByModelDatabase1 = createToscaModelByImport(modelId, "toscaImportByModelDatabase1");
final Result<ToscaImportByModel> findAllByModelResult = mock(Result.class);
when(findAllByModelResult.all()).thenReturn(List.of(toscaImportByModel1, toscaImportByModelDatabase1));
when(toscaImportByModelAccessorMock.findAllByModel(modelId)).thenReturn(findAllByModelResult);
toscaModelImportCassandraDao.importAll(modelId, importModelList);
verify(toscaImportByModelMapperMock).save(toscaImportByModel1);
verify(toscaImportByModelMapperMock).save(toscaImportByModel2);
verify(toscaImportByModelMapperMock).save(toscaImportByModel3);
verify(toscaImportByModelMapperMock, never()).save(toscaImportByModelWrongModel);
verify(toscaImportByModelMapperMock).delete(toscaImportByModelDatabase1.getModelId(), toscaImportByModelDatabase1.getFullPath());
verify(toscaImportByModelMapperMock, never()).delete(toscaImportByModel1.getModelId(), toscaImportByModel1.getFullPath());
verify(toscaImportByModelMapperMock, never()).delete(toscaImportByModel2.getModelId(), toscaImportByModel2.getFullPath());
verify(toscaImportByModelMapperMock, never()).delete(toscaImportByModel3.getModelId(), toscaImportByModel3.getFullPath());
verify(toscaImportByModelMapperMock, never()).delete(toscaImportByModelWrongModel.getModelId(), toscaImportByModelWrongModel.getFullPath());
}
@Test
void initSuccessTest() {
toscaModelImportCassandraDao = new ToscaModelImportCassandraDao(cassandraClient);
when(cassandraClient.isConnected()).thenReturn(true);
final Session sessionMock = mock(Session.class);
final MappingManager mappingManagerMock = mock(MappingManager.class);
when(cassandraClient.connect(AuditingTypesConstants.ARTIFACT_KEYSPACE)).thenReturn(Either.left(new ImmutablePair<>(sessionMock, mappingManagerMock)));
toscaModelImportCassandraDao.init();
verify(cassandraClient).connect(AuditingTypesConstants.ARTIFACT_KEYSPACE);
verify(mappingManagerMock).mapper(ToscaImportByModel.class);
verify(mappingManagerMock).createAccessor(ToscaImportByModelAccessor.class);
}
@Test
void initTest_clientNotConnected() {
toscaModelImportCassandraDao = new ToscaModelImportCassandraDao(cassandraClient);
when(cassandraClient.isConnected()).thenReturn(false);
toscaModelImportCassandraDao.init();
verify(cassandraClient, never()).connect(anyString());
}
@Test
void initTest_keyspaceConnectionFailure() {
toscaModelImportCassandraDao = new ToscaModelImportCassandraDao(cassandraClient);
when(cassandraClient.isConnected()).thenReturn(true);
when(cassandraClient.connect(AuditingTypesConstants.ARTIFACT_KEYSPACE))
.thenReturn(Either.right(CassandraOperationStatus.KEYSPACE_NOT_CONNECTED));
final CassandraDaoInitException actualException = assertThrows(CassandraDaoInitException.class, () -> toscaModelImportCassandraDao.init());
final CassandraDaoInitException expectedException = CassandraDaoInitExceptionProvider
.keySpaceConnectError(AuditingTypesConstants.ARTIFACT_KEYSPACE, CassandraOperationStatus.KEYSPACE_NOT_CONNECTED).get();
assertEquals(expectedException.getMessage(), actualException.getMessage());
}
private ToscaImportByModel createToscaModelByImport(final String modelId, final String path) {
final var toscaImportByModel3 = new ToscaImportByModel();
toscaImportByModel3.setModelId(modelId);
toscaImportByModel3.setFullPath(path);
return toscaImportByModel3;
}
}
|