Skip to content

Commit b1338ad

Browse files
author
Dennis Labordus
committed
Small improvements.
Signed-off-by: Dennis Labordus <[email protected]>
1 parent ebfa642 commit b1338ad

File tree

5 files changed

+52
-48
lines changed

5 files changed

+52
-48
lines changed

repository-basex/src/test/java/org/lfenergy/compas/scl/data/basex/repository/CompasSclDataBaseXRepositoryTest.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,23 @@
88
import org.junit.jupiter.api.extension.ExtendWith;
99
import org.lfenergy.compas.scl.data.basex.client.BaseXClientFactory;
1010
import org.lfenergy.compas.scl.data.basex.client.BaseXServerJUnitExtension;
11+
import org.lfenergy.compas.scl.data.model.SclType;
1112
import org.lfenergy.compas.scl.data.repository.AbstractCompasSclDataRepository;
1213
import org.lfenergy.compas.scl.data.repository.CompasSclDataRepository;
1314
import org.lfenergy.compas.scl.data.util.SclDataModelMarshaller;
1415
import org.mockito.junit.jupiter.MockitoExtension;
16+
import org.slf4j.Logger;
17+
import org.slf4j.LoggerFactory;
18+
19+
import java.io.IOException;
20+
import java.util.Arrays;
1521

1622
import static org.lfenergy.compas.scl.data.basex.client.BaseXServerUtil.createClientFactory;
1723

1824
@ExtendWith({MockitoExtension.class, BaseXServerJUnitExtension.class})
1925
class CompasSclDataBaseXRepositoryTest extends AbstractCompasSclDataRepository {
26+
private static final Logger LOGGER = LoggerFactory.getLogger(CompasSclDataBaseXRepositoryTest.class);
27+
2028
private static BaseXClientFactory factory;
2129
private CompasSclDataBaseXRepository repository;
2230

@@ -28,10 +36,21 @@ protected CompasSclDataRepository getRepository() {
2836
@BeforeAll
2937
static void beforeAll() {
3038
factory = createClientFactory(BaseXServerJUnitExtension.getPortNumber());
39+
40+
// To make it possible to re-run the test over and over (in your IDE),
41+
// Create all the database, because this will cause to old ones to be removed.
42+
Arrays.stream(SclType.values())
43+
.forEach(type -> {
44+
try {
45+
factory.createClient().executeXQuery("db:create('" + type + "')");
46+
} catch (IOException exp) {
47+
LOGGER.warn("Error re-creating database {}", type, exp);
48+
}
49+
});
3150
}
3251

3352
@BeforeEach
34-
void beforeEach() throws Exception {
53+
void beforeEach() {
3554
repository = new CompasSclDataBaseXRepository(factory, new SclDataModelMarshaller());
3655
}
3756
}

repository-postgresql/src/main/java/org/lfenergy/compas/scl/data/repository/postgres/CompasSclDataPostgreSQLRepository.java

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ public List<Item> list(SclType type) {
8787
}
8888
}
8989
} catch (SQLException exp) {
90-
throw new CompasSclDataServiceException(POSTGRES_SELECT_ERROR_CODE,
91-
"Error select meta info from database!", exp);
90+
throw new CompasSclDataServiceException(POSTGRES_SELECT_ERROR_CODE, "Error listing scl entries from database!", exp);
9291
}
9392
return items;
9493
}
@@ -98,7 +97,7 @@ public List<Item> list(SclType type) {
9897
public List<Item> listVersionsByUUID(SclType type, UUID id) {
9998
var sql = "select id, name, major_version, minor_version, patch_version"
10099
+ " from scl_file"
101-
+ " where id = ?"
100+
+ " where id = ?"
102101
+ " and type = ?"
103102
+ " order by major_version, minor_version, patch_version";
104103

@@ -120,8 +119,7 @@ public List<Item> listVersionsByUUID(SclType type, UUID id) {
120119
}
121120
}
122121
} catch (SQLException exp) {
123-
throw new CompasSclDataServiceException(POSTGRES_SELECT_ERROR_CODE,
124-
"Error select meta info from database!", exp);
122+
throw new CompasSclDataServiceException(POSTGRES_SELECT_ERROR_CODE, "Error selecting versions from database!", exp);
125123
}
126124
return items;
127125
}
@@ -140,8 +138,8 @@ public String findByUUID(SclType type, UUID id) {
140138
public String findByUUID(SclType type, UUID id, Version version) {
141139
var sql = "select scl_data "
142140
+ " from scl_file "
143-
+ " where id = ?"
144-
+ " and type = ?"
141+
+ " where id = ?"
142+
+ " and type = ?"
145143
+ " and major_version = ? "
146144
+ " and minor_version = ? "
147145
+ " and patch_version = ? ";
@@ -161,8 +159,7 @@ public String findByUUID(SclType type, UUID id, Version version) {
161159
throw new CompasNoDataFoundException(message);
162160
}
163161
} catch (SQLException exp) {
164-
throw new CompasSclDataServiceException(POSTGRES_SELECT_ERROR_CODE,
165-
"Error select scl data from database!", exp);
162+
throw new CompasSclDataServiceException(POSTGRES_SELECT_ERROR_CODE, "Error select scl data from database!", exp);
166163
}
167164
}
168165

@@ -171,7 +168,7 @@ public String findByUUID(SclType type, UUID id, Version version) {
171168
public SclMetaInfo findMetaInfoByUUID(SclType type, UUID id) {
172169
var sql = "select id, name, major_version, minor_version, patch_version"
173170
+ " from scl_file"
174-
+ " where id = ?"
171+
+ " where id = ?"
175172
+ " and type = ?"
176173
+ " order by major_version desc, minor_version desc, patch_version desc";
177174
try (var connection = dataSource.getConnection();
@@ -193,8 +190,7 @@ public SclMetaInfo findMetaInfoByUUID(SclType type, UUID id) {
193190
throw new CompasNoDataFoundException(message);
194191
}
195192
} catch (SQLException exp) {
196-
throw new CompasSclDataServiceException(POSTGRES_SELECT_ERROR_CODE,
197-
"Error select meta info from database!", exp);
193+
throw new CompasSclDataServiceException(POSTGRES_SELECT_ERROR_CODE, "Error select meta info from database!", exp);
198194
}
199195
}
200196

@@ -216,34 +212,32 @@ public void create(SclType type, UUID id, String name, String scl, Version versi
216212
stmt.setString(8, scl);
217213
stmt.executeUpdate();
218214
} catch (SQLException exp) {
219-
throw new CompasSclDataServiceException(POSTGRES_INSERT_ERROR_CODE,
220-
"Error inserting SCL to database!", exp);
215+
throw new CompasSclDataServiceException(POSTGRES_INSERT_ERROR_CODE, "Error inserting SCL to database!", exp);
221216
}
222217
}
223218

224219
@Override
225220
@Transactional(REQUIRED)
226221
public void delete(SclType type, UUID id) {
227222
var sql = "delete from scl_file "
228-
+ " where id = ?"
223+
+ " where id = ?"
229224
+ " and type = ?";
230225
try (var connection = dataSource.getConnection();
231226
var stmt = connection.prepareStatement(sql)) {
232227
stmt.setObject(1, id);
233228
stmt.setString(2, type.name());
234229
stmt.executeUpdate();
235230
} catch (SQLException exp) {
236-
throw new CompasSclDataServiceException(POSTGRES_DELETE_ERROR_CODE,
237-
"Error removing SCL from database!", exp);
231+
throw new CompasSclDataServiceException(POSTGRES_DELETE_ERROR_CODE, "Error removing SCL from database!", exp);
238232
}
239233
}
240234

241235
@Override
242236
@Transactional(REQUIRED)
243237
public void delete(SclType type, UUID id, Version version) {
244238
var sql = "delete from scl_file "
245-
+ " where id = ?"
246-
+ " and type = ?"
239+
+ " where id = ?"
240+
+ " and type = ?"
247241
+ " and major_version = ? "
248242
+ " and minor_version = ? "
249243
+ " and patch_version = ? ";
@@ -256,8 +250,7 @@ public void delete(SclType type, UUID id, Version version) {
256250
stmt.setInt(5, version.getPatchVersion());
257251
stmt.executeUpdate();
258252
} catch (SQLException exp) {
259-
throw new CompasSclDataServiceException(POSTGRES_DELETE_ERROR_CODE,
260-
"Error removing SCL (version) from database!", exp);
253+
throw new CompasSclDataServiceException(POSTGRES_DELETE_ERROR_CODE, "Error removing SCL (version) from database!", exp);
261254
}
262255
}
263256
}

repository/src/main/java/org/lfenergy/compas/scl/data/exception/CompasSclDataServiceErrorCode.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class CompasSclDataServiceErrorCode {
1919
public static final String BASEX_QUERY_ERROR_CODE = "SDS-1001";
2020
public static final String BASEX_COMMAND_ERROR_CODE = "SDS-1002";
2121

22-
public static final String POSTGRES_SELECT_ERROR_CODE = "SDS-2001";
23-
public static final String POSTGRES_INSERT_ERROR_CODE = "SDS-2002";
24-
public static final String POSTGRES_DELETE_ERROR_CODE = "SDS-2003";
22+
public static final String POSTGRES_SELECT_ERROR_CODE = "SDS-2000";
23+
public static final String POSTGRES_INSERT_ERROR_CODE = "SDS-2001";
24+
public static final String POSTGRES_DELETE_ERROR_CODE = "SDS-2002";
2525
}

repository/src/main/java/org/lfenergy/compas/scl/data/repository/CompasSclDataRepository.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ public interface CompasSclDataRepository {
7373
* @param name The name of the SCL to be stored.
7474
* @param scl The SCL XML File content to store.
7575
* @param version The version of the new entry to be created.
76-
* @param username The user that created the new entry.
76+
* @param who The user that created the new entry.
7777
*/
78-
void create(SclType type, UUID id, String name, String scl, Version version, String username);
78+
void create(SclType type, UUID id, String name, String scl, Version version, String who);
7979

8080
/**
8181
* Delete all versions for a specific SCL File using it's ID.

repository/src/test/java/org/lfenergy/compas/scl/data/repository/AbstractCompasSclDataRepository.java

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,11 @@ void findByUUID_WhenCalledWithUnknownUUID_ThenExceptionIsThrown() {
136136
void findByUUID_WhenTwoVersionsOfARecordAdded_ThenLastRecordIsFound() {
137137
var version = new Version(1, 0, 0);
138138
var uuid = UUID.randomUUID();
139-
var name = "SCL-Name";
140139
var scl = readSCL(uuid, version, NAME_1);
141-
getRepository().create(TYPE, uuid, name, scl, version, WHO);
140+
getRepository().create(TYPE, uuid, NAME_1, scl, version, WHO);
142141
version = new Version(1, 1, 0);
143142
scl = readSCL(uuid, version, NAME_2);
144-
getRepository().create(TYPE, uuid, name, scl, version, WHO);
143+
getRepository().create(TYPE, uuid, NAME_2, scl, version, WHO);
145144

146145
var foundScl = getRepository().findByUUID(TYPE, uuid);
147146

@@ -154,9 +153,8 @@ void findByUUID_WhenTwoVersionsOfARecordAdded_ThenLastRecordIsFound() {
154153
void findByUUIDWithVersion_WhenCalledWithUnknownVersion_ThenExceptionIsThrown() {
155154
var version = new Version(1, 0, 0);
156155
var uuid = UUID.randomUUID();
157-
var name = "SCL-Name";
158156
var scl = readSCL(uuid, version, NAME_1);
159-
getRepository().create(TYPE, uuid, name, scl, version, WHO);
157+
getRepository().create(TYPE, uuid, NAME_1, scl, version, WHO);
160158

161159
var unknownVersion = new Version(1, 1, 1);
162160
var exception = assertThrows(CompasNoDataFoundException.class, () -> {
@@ -169,12 +167,11 @@ void findByUUIDWithVersion_WhenCalledWithUnknownVersion_ThenExceptionIsThrown()
169167
void findByUUIDWithVersion_WhenTwoVersionsOfARecordAdded_ThenCorrectRecordIsFound() {
170168
var expectedVersion = new Version(1, 0, 0);
171169
var uuid = UUID.randomUUID();
172-
var name = "SCL-Name";
173170
var scl = readSCL(uuid, expectedVersion, NAME_1);
174-
getRepository().create(TYPE, uuid, name, scl, expectedVersion, WHO);
171+
getRepository().create(TYPE, uuid, NAME_1, scl, expectedVersion, WHO);
175172
var version = new Version(1, 1, 0);
176173
scl = readSCL(uuid, version, NAME_1);
177-
getRepository().create(TYPE, uuid, name, scl, version, WHO);
174+
getRepository().create(TYPE, uuid, NAME_1, scl, version, WHO);
178175

179176
var foundScl = getRepository().findByUUID(TYPE, uuid, expectedVersion);
180177

@@ -215,9 +212,8 @@ void findMetaInfoByUUID_WhenCalledWithUnknownUUID_ThenExceptionIsThrown() {
215212
void createAndFind_WhenSclAdded_ThenScLStoredAndLastVersionCanBeFound() {
216213
var version = new Version(1, 0, 0);
217214
var uuid = UUID.randomUUID();
218-
var name = "SCL-Name";
219215
var scl = readSCL(uuid, version, NAME_1);
220-
getRepository().create(TYPE, uuid, name, scl, version, WHO);
216+
getRepository().create(TYPE, uuid, NAME_1, scl, version, WHO);
221217

222218
var foundScl = getRepository().findByUUID(TYPE, uuid);
223219

@@ -230,13 +226,12 @@ void createAndFind_WhenSclAdded_ThenScLStoredAndLastVersionCanBeFound() {
230226
void createAndFind_WhenMoreVersionOfSclAdded_ThenDefaultSCLLastVersionReturned() {
231227
var version = new Version(1, 0, 0);
232228
var uuid = UUID.randomUUID();
233-
var name = "SCL-Name";
234229
var scl = readSCL(uuid, version, NAME_1);
235-
getRepository().create(TYPE, uuid, name, scl, version, WHO);
230+
getRepository().create(TYPE, uuid, NAME_1, scl, version, WHO);
236231

237232
var nextVersion = version.getNextVersion(ChangeSetType.MAJOR);
238233
var nextScl = readSCL(uuid, nextVersion, NAME_1);
239-
getRepository().create(TYPE, uuid, name, nextScl, nextVersion, WHO);
234+
getRepository().create(TYPE, uuid, NAME_1, nextScl, nextVersion, WHO);
240235

241236
var foundScl = getRepository().findByUUID(TYPE, uuid);
242237

@@ -249,13 +244,12 @@ void createAndFind_WhenMoreVersionOfSclAdded_ThenDefaultSCLLastVersionReturned()
249244
void createAndFind_WhenMoreVersionOfSCLAdded_ThenSCLOldVersionCanBeFound() {
250245
var version = new Version(1, 0, 0);
251246
var uuid = UUID.randomUUID();
252-
var name = "SCL-Name";
253247
var scl = readSCL(uuid, version, NAME_1);
254-
getRepository().create(TYPE, uuid, name, scl, version, WHO);
248+
getRepository().create(TYPE, uuid, NAME_1, scl, version, WHO);
255249

256250
var nextVersion = version.getNextVersion(ChangeSetType.MAJOR);
257251
var nextScl = readSCL(uuid, nextVersion, NAME_1);
258-
getRepository().create(TYPE, uuid, name, nextScl, nextVersion, WHO);
252+
getRepository().create(TYPE, uuid, NAME_1, nextScl, nextVersion, WHO);
259253

260254
var foundScl = getRepository().findByUUID(TYPE, uuid, version);
261255

@@ -268,10 +262,9 @@ void createAndFind_WhenMoreVersionOfSCLAdded_ThenSCLOldVersionCanBeFound() {
268262
void createAndDelete_WhenSclAddedAndDelete_ThenScLStoredAndRemoved() {
269263
var version = new Version(1, 0, 0);
270264
var uuid = UUID.randomUUID();
271-
var name = "SCL-Name";
272265
var scl = readSCL(uuid, version, NAME_1);
273266

274-
getRepository().create(TYPE, uuid, name, scl, version, WHO);
267+
getRepository().create(TYPE, uuid, NAME_1, scl, version, WHO);
275268
var foundScl = getRepository().findByUUID(TYPE, uuid);
276269
assertNotNull(foundScl);
277270
assertEquals(getIdFromHeader(scl), getIdFromHeader(foundScl));
@@ -287,17 +280,16 @@ void createAndDelete_WhenSclAddedAndDelete_ThenScLStoredAndRemoved() {
287280
void createAndDeleteAll_WhenSclAddedAndDelete_ThenScLStoredAndRemoved() {
288281
var version = new Version(1, 0, 0);
289282
var uuid = UUID.randomUUID();
290-
var name = "SCL-Name";
291283
var scl = readSCL(uuid, version, NAME_1);
292284

293-
getRepository().create(TYPE, uuid, name, scl, version, WHO);
285+
getRepository().create(TYPE, uuid, NAME_1, scl, version, WHO);
294286
var foundScl = getRepository().findByUUID(TYPE, uuid);
295287
assertNotNull(foundScl);
296288
assertEquals(getIdFromHeader(scl), getIdFromHeader(foundScl));
297289
assertEquals(getVersionFromHeader(scl), getVersionFromHeader(foundScl));
298290

299291
version = version.getNextVersion(ChangeSetType.MAJOR);
300-
getRepository().create(TYPE, uuid, name, scl, version, WHO);
292+
getRepository().create(TYPE, uuid, NAME_1, scl, version, WHO);
301293
foundScl = getRepository().findByUUID(TYPE, uuid);
302294
assertNotNull(foundScl);
303295
assertEquals(getIdFromHeader(scl), getIdFromHeader(foundScl));

0 commit comments

Comments
 (0)