Skip to content

Commit

Permalink
Merge pull request #224 from com-pas/develop
Browse files Browse the repository at this point in the history
New release
  • Loading branch information
Dennis Labordus authored May 30, 2022
2 parents c415905 + 00a5859 commit 1f6979a
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public String findByUUID(SclFileType type, UUID id, Version version) {
throw new IllegalStateException("Mock method using Mockito. Only needed to startup.");
}

@Override
public boolean hasDuplicateSclName(SclFileType type, String name) {
throw new IllegalStateException("Mock method using Mockito. Only needed to startup.");
}

@Override
public void create(SclFileType type, UUID id, String name, String scl, Version version, String who) {
throw new IllegalStateException("Mock method using Mockito. Only needed to startup.");
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ SPDX-License-Identifier: Apache-2.0

<compas.core.version>0.9.0</compas.core.version>

<quarkus.platform.version>2.9.0.Final</quarkus.platform.version>
<quarkus.platform.version>2.9.2.Final</quarkus.platform.version>
<jaxb-impl.version>2.3.6</jaxb-impl.version>
<microprofile-openapi-api.version>3.0</microprofile-openapi-api.version>
<slf4j.version>1.7.36</slf4j.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ public String findByUUID(SclFileType type, UUID id, Version version) {
return result.get(0);
}

@Override
public boolean hasDuplicateSclName(SclFileType type, String name) {
return false;
}

@Override
public SclMetaInfo findMetaInfoByUUID(SclFileType type, UUID id) {
// This find method always searches for the latest version.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,29 @@ public String findByUUID(SclFileType type, UUID id, Version version) {
}
}

@Override
public boolean hasDuplicateSclName(SclFileType type, String name) {
var sql = "SELECT DISTINCT ON (id) * "
+ FROM_CLAUSE
+ "WHERE type=? "
+ "ORDER BY id, major_version desc, minor_version desc, patch_version desc";

try (var connection = dataSource.getConnection();
var stmt = connection.prepareStatement(sql)) {
stmt.setString(1, type.name());

try (var resultSet = stmt.executeQuery()) {
while (resultSet.next()) {
var usedName = resultSet.getString(NAME_FIELD);
if (usedName.equals(name)) return true;
}
}
return false;
} catch (SQLException exp) {
throw new CompasSclDataServiceException(POSTGRES_SELECT_ERROR_CODE, "Error selecting latest versions from database!", exp);
}
}

@Override
public SclMetaInfo findMetaInfoByUUID(SclFileType type, UUID id) {
var sql = SELECT_METADATA_CLAUSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@
package org.lfenergy.compas.scl.data.repository.postgresql;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.lfenergy.compas.scl.data.model.Version;
import org.lfenergy.compas.scl.data.repository.AbstractCompasSclDataRepository;
import org.lfenergy.compas.scl.data.repository.CompasSclDataRepository;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.UUID;

import static org.junit.jupiter.api.Assertions.assertTrue;

@ExtendWith({MockitoExtension.class, PostgreSQLServerJUnitExtension.class})
class CompasSclDataPostgreSQLRepositoryTest extends AbstractCompasSclDataRepository {
private CompasSclDataPostgreSQLRepository repository;
Expand All @@ -22,4 +28,17 @@ protected CompasSclDataRepository getRepository() {
void beforeEach() {
repository = new CompasSclDataPostgreSQLRepository(PostgreSQLServerJUnitExtension.getDataSource());
}

/*
* TODO: Method beneath needs to be moved to AbstractCompasSclDataRepository
* when hasDuplicateSclName has been implemented by CompasSclDataBaseXRepository. */
@Test
void hasDuplicateSclName_WhenUsingSclNameThatHasBeenUsedYet_ThenDuplicateIsFound() {
var expectedVersion = new Version(1, 0, 0);
var uuid = UUID.randomUUID();
var scl = readSCL(uuid, expectedVersion, NAME_1);
getRepository().create(TYPE, uuid, NAME_1, scl, expectedVersion, WHO);

assertTrue(getRepository().hasDuplicateSclName(TYPE, NAME_1));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class CompasSclDataServiceErrorCode {
public static final String HEADER_NOT_FOUND_ERROR_CODE = "SDS-0004";
public static final String NO_SCL_ELEMENT_FOUND_ERROR_CODE = "SDS-0005";
public static final String NO_DATA_FOUND_ERROR_CODE = "SDS-0006";
public static final String DUPLICATE_SCL_NAME_ERROR_CODE = "SDS-0007";

public static final String BASEX_CLIENT_CREATION_ERROR_CODE = "SDS-1000";
public static final String BASEX_QUERY_ERROR_CODE = "SDS-1001";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ public interface CompasSclDataRepository {
@Transactional(SUPPORTS)
String findByUUID(SclFileType type, UUID id, Version version);

/**
* Return the specific version of a specific SCL Entry.
*
* @param type The type of SCL to search for the specific SCL.
* @param name The name of the SCL used for checking duplicates.
* @return True if name is already used by another SCL File of the same File type, otherwise false.
*/
@Transactional(SUPPORTS)
boolean hasDuplicateSclName(SclFileType type, String name);

/**
* Create a new entry for the passed UUID with the version number passed.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

import static org.junit.jupiter.api.Assertions.*;
import static org.lfenergy.compas.scl.data.SclDataServiceConstants.*;
import static org.lfenergy.compas.scl.data.exception.CompasSclDataServiceErrorCode.HEADER_NOT_FOUND_ERROR_CODE;
import static org.lfenergy.compas.scl.data.exception.CompasSclDataServiceErrorCode.NO_DATA_FOUND_ERROR_CODE;
import static org.lfenergy.compas.scl.data.exception.CompasSclDataServiceErrorCode.*;

public abstract class AbstractCompasSclDataRepository {
protected static final SclFileType TYPE = SclFileType.SCD;
Expand Down Expand Up @@ -180,6 +179,16 @@ void findByUUIDWithVersion_WhenTwoVersionsOfARecordAdded_ThenCorrectRecordIsFoun
assertEquals(expectedVersion.toString(), getVersionFromHeader(foundScl));
}

@Test
void hasDuplicateSclName_WhenUsingSclNameThatHasNotBeenUsedYet_ThenNoDuplicateIsFound() {
var expectedVersion = new Version(1, 0, 0);
var uuid = UUID.randomUUID();
var scl = readSCL(uuid, expectedVersion, NAME_1);
getRepository().create(TYPE, uuid, NAME_1, scl, expectedVersion, WHO);

assertFalse(getRepository().hasDuplicateSclName(TYPE, "Some other name"));
}

@Test
void findMetaInfoByUUID_WhenTwoVersionsOfARecordAdded_ThenLastRecordIsFound() {
var version = new Version(1, 0, 0);
Expand Down Expand Up @@ -320,7 +329,7 @@ private String getVersionFromHeader(String sclData) {
.orElse("");
}

private String readSCL(UUID uuid, Version version, String name) {
protected String readSCL(UUID uuid, Version version, String name) {
var inputStream = getClass().getResourceAsStream("/scl/scl.scd");
assert inputStream != null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import static javax.transaction.Transactional.TxType.REQUIRED;
import static javax.transaction.Transactional.TxType.SUPPORTS;
import static org.lfenergy.compas.scl.data.SclDataServiceConstants.*;
import static org.lfenergy.compas.scl.data.exception.CompasSclDataServiceErrorCode.DUPLICATE_SCL_NAME_ERROR_CODE;
import static org.lfenergy.compas.scl.data.exception.CompasSclDataServiceErrorCode.NO_SCL_ELEMENT_FOUND_ERROR_CODE;

/**
Expand Down Expand Up @@ -120,6 +121,10 @@ public String create(SclFileType type, String name, String who, String comment,
throw new CompasException(NO_SCL_ELEMENT_FOUND_ERROR_CODE, "No valid SCL found in the passed SCL Data.");
}

if (repository.hasDuplicateSclName(type, name)) {
throw new CompasException(DUPLICATE_SCL_NAME_ERROR_CODE, "Given name of SCL File already used.");
}

// A unique ID is generated to store it under.
var id = UUID.randomUUID();
// When the SCL is created the version will be set to 1.0.0
Expand Down Expand Up @@ -158,6 +163,14 @@ public String update(SclFileType type, UUID id, ChangeSetType changeSetType, Str
}

var currentSclMetaInfo = repository.findMetaInfoByUUID(type, id);
var newFileName = getFilenameFromXML(scl);

if (newFileName.isPresent()
&& !newFileName.get().equals(currentSclMetaInfo.getName())
&& repository.hasDuplicateSclName(type, newFileName.get())) {
throw new CompasException(DUPLICATE_SCL_NAME_ERROR_CODE, "Given name of SCL File already used.");
}

// We always add a new version to the database, so add version record to the SCL and create a new record.
var version = new Version(currentSclMetaInfo.getVersion());
version = version.getNextVersion(changeSetType);
Expand All @@ -167,7 +180,7 @@ public String update(SclFileType type, UUID id, ChangeSetType changeSetType, Str
createHistoryItem(header, "SCL updated", who, comment, version);

// Update or add the Compas Private Element to the SCL File.
var newSclName = getFilenameFromXML(scl).orElse(currentSclMetaInfo.getName());
var newSclName = newFileName.orElse(currentSclMetaInfo.getName());
setSclCompasPrivateElement(scl, newSclName, type);

var newSclData = converter.convertToString(scl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
import static java.util.Collections.emptyList;
import static org.junit.jupiter.api.Assertions.*;
import static org.lfenergy.compas.scl.data.SclDataServiceConstants.*;
import static org.lfenergy.compas.scl.data.exception.CompasSclDataServiceErrorCode.NO_DATA_FOUND_ERROR_CODE;
import static org.lfenergy.compas.scl.data.exception.CompasSclDataServiceErrorCode.NO_SCL_ELEMENT_FOUND_ERROR_CODE;
import static org.lfenergy.compas.scl.data.exception.CompasSclDataServiceErrorCode.*;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
Expand Down Expand Up @@ -119,6 +118,7 @@ void create_WhenCalledWithOutCompasExtension_ThenSCLReturnedWithCorrectCompasExt

var scl = readSCL();

when(compasSclDataRepository.hasDuplicateSclName(SCL_TYPE, name)).thenReturn(false);
doNothing().when(compasSclDataRepository).create(eq(SCL_TYPE), any(UUID.class), eq(name), anyString(), eq(INITIAL_VERSION), eq(who));

scl = compasSclDataService.create(SCL_TYPE, name, who, comment, scl);
Expand All @@ -127,6 +127,7 @@ void create_WhenCalledWithOutCompasExtension_ThenSCLReturnedWithCorrectCompasExt
assertCompasExtenions(scl, name);
assertHistoryItem(scl, INITIAL_VERSION, comment);
verify(compasSclDataRepository, times(1)).create(eq(SCL_TYPE), any(UUID.class), eq(name), anyString(), eq(INITIAL_VERSION), eq(who));
verify(compasSclDataRepository, times(1)).hasDuplicateSclName(SCL_TYPE, name);
}

@Test
Expand All @@ -138,6 +139,7 @@ void create_WhenCalledWithCompasExtension_ThenSCLReturnedWithCorrectCompasExtens
var scl = readSCL();
scl = createCompasPrivate(scl, "JUSTANOTHERNAME");

when(compasSclDataRepository.hasDuplicateSclName(SCL_TYPE, name)).thenReturn(false);
doNothing().when(compasSclDataRepository).create(eq(SCL_TYPE), any(UUID.class), eq(name), anyString(), eq(INITIAL_VERSION), eq(who));

scl = compasSclDataService.create(SCL_TYPE, name, who, comment, scl);
Expand All @@ -146,6 +148,23 @@ void create_WhenCalledWithCompasExtension_ThenSCLReturnedWithCorrectCompasExtens
assertCompasExtenions(scl, name);
assertHistoryItem(scl, INITIAL_VERSION, comment);
verify(compasSclDataRepository, times(1)).create(eq(SCL_TYPE), any(UUID.class), eq(name), anyString(), eq(INITIAL_VERSION), eq(who));
verify(compasSclDataRepository, times(1)).hasDuplicateSclName(SCL_TYPE, name);
}

@Test
void create_WhenCalledWithDuplicateSclName_ThenCompasExceptionThrown() throws IOException {
var name = "JUSTSOMENAME";
var comment = "";
var who = "User A";

var scl = readSCL();

when(compasSclDataRepository.hasDuplicateSclName(SCL_TYPE, name)).thenReturn(true);
var exception = assertThrows(CompasException.class, () -> {
compasSclDataService.create(SCL_TYPE, name, who, comment, scl);
});
assertEquals(DUPLICATE_SCL_NAME_ERROR_CODE, exception.getErrorCode());
verify(compasSclDataRepository, times(1)).hasDuplicateSclName(SCL_TYPE, name);
}

@Test
Expand Down Expand Up @@ -183,6 +202,7 @@ void update_WhenCalledWithoutCompasElements_ThenSCLReturnedWithCorrectCompasExte
assertHistoryItem(scl, nextVersion, null);
verify(compasSclDataRepository, times(1)).create(eq(SCL_TYPE), eq(uuid), eq(previousName), anyString(), eq(nextVersion), eq(who));
verify(compasSclDataRepository, times(1)).findMetaInfoByUUID(SCL_TYPE, uuid);
verify(compasSclDataRepository, never()).hasDuplicateSclName(SCL_TYPE, previousName);
}

@Test
Expand All @@ -200,6 +220,7 @@ void update_WhenCalledWithCompasElementsAndNewName_ThenSCLReturnedWithCorrectCom
var sclMetaInfo = new SclMetaInfo(uuid.toString(), previousName, INITIAL_VERSION.toString());
when(compasSclDataRepository.findMetaInfoByUUID(SCL_TYPE, uuid)).thenReturn(sclMetaInfo);
doNothing().when(compasSclDataRepository).create(eq(SCL_TYPE), eq(uuid), eq(newName), anyString(), eq(nextVersion), eq(who));
when(compasSclDataRepository.hasDuplicateSclName(SCL_TYPE, newName)).thenReturn(false);

scl = compasSclDataService.update(SCL_TYPE, uuid, changeSet, who, null, scl);

Expand All @@ -208,6 +229,30 @@ void update_WhenCalledWithCompasElementsAndNewName_ThenSCLReturnedWithCorrectCom
assertHistoryItem(scl, nextVersion, null);
verify(compasSclDataRepository, times(1)).create(eq(SCL_TYPE), eq(uuid), eq(newName), anyString(), eq(nextVersion), eq(who));
verify(compasSclDataRepository, times(1)).findMetaInfoByUUID(SCL_TYPE, uuid);
verify(compasSclDataRepository, times(1)).hasDuplicateSclName(SCL_TYPE, newName);
}

@Test
void update_WhenCalledWithCompasElementsAndDuplicateNewName_ThenCompasExceptionThrown() throws IOException {
var previousName = "Previous SCL Filename";
var newName = "New SCL Filename";
var uuid = UUID.randomUUID();
var changeSet = ChangeSetType.MAJOR;
var who = "User A";
var nextVersion = INITIAL_VERSION.getNextVersion(changeSet);

var scl = createCompasPrivate(readSCL(), newName);

var sclMetaInfo = new SclMetaInfo(uuid.toString(), previousName, INITIAL_VERSION.toString());
when(compasSclDataRepository.findMetaInfoByUUID(SCL_TYPE, uuid)).thenReturn(sclMetaInfo);
when(compasSclDataRepository.hasDuplicateSclName(SCL_TYPE, newName)).thenReturn(true);

var exception = assertThrows(CompasException.class, () -> {
compasSclDataService.update(SCL_TYPE, uuid, changeSet, who, null, scl);
});
assertEquals(DUPLICATE_SCL_NAME_ERROR_CODE, exception.getErrorCode());
verify(compasSclDataRepository, times(1)).findMetaInfoByUUID(SCL_TYPE, uuid);
verify(compasSclDataRepository, times(1)).hasDuplicateSclName(SCL_TYPE, newName);
}

@Test
Expand All @@ -232,6 +277,7 @@ void update_WhenCalledWithCompasElementsAndSameName_ThenSCLReturnedWithCorrectCo
assertHistoryItem(scl, nextVersion, null);
verify(compasSclDataRepository, times(1)).create(eq(SCL_TYPE), eq(uuid), eq(previousName), anyString(), eq(nextVersion), eq(who));
verify(compasSclDataRepository, times(1)).findMetaInfoByUUID(SCL_TYPE, uuid);
verify(compasSclDataRepository, never()).hasDuplicateSclName(SCL_TYPE, previousName);
}

@Test
Expand Down

0 comments on commit 1f6979a

Please sign in to comment.