import-unix-user-and-email-aliases #81

Merged
hsh-michaelhoennig merged 14 commits from import-unix-user-and-email-aliases into master 2024-08-01 13:12:58 +02:00
2 changed files with 80 additions and 17 deletions
Showing only changes of commit 9a05cad38e - Show all commits

View File

@ -4,6 +4,7 @@ import com.opencsv.CSVParserBuilder;
import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;
import lombok.SneakyThrows;
import net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAsset;
import net.hostsharing.hsadminng.rbac.context.ContextBasedTest;
import net.hostsharing.hsadminng.rbac.rbacobject.RbacObject;
import net.hostsharing.hsadminng.rbac.test.JpaAttempt;
@ -35,6 +36,7 @@ import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.UUID;
import java.util.stream.Collectors;
import static java.lang.Boolean.parseBoolean;
@ -122,7 +124,9 @@ public class CsvDataImport extends ContextBasedTest {
@SneakyThrows
public static String[] parseCsvLine(final String csvLine) {
try (final var reader = new CSVReader(new StringReader(csvLine))) {
return ofNullable(reader.readNext()).orElse(emptyArray(String.class));
return stream(ofNullable(reader.readNext()).orElse(emptyArray(String.class)))
.map(String::trim)
.toArray(String[]::new);
}
}
@ -138,16 +142,11 @@ public class CsvDataImport extends ContextBasedTest {
public <T extends RbacObject> T persist(final Integer id, final T entity) {
try {
final var asString = entity.toString();
if ( asString.contains("'null null, null'") || asString.equals("person()")) {
errors.add("skipping to persist empty record-id " + id + " #" + entity.hashCode() + ": " + entity);
return entity;
if (entity instanceof HsHostingAssetRawEntity ha ) { // && ha.getType() == HsHostingAssetType.EMAIL_ALIAS) {
//noinspection unchecked
return (T) persistViaSql(id, ha);
}
//System.out.println("persisting #" + entity.hashCode() + ": " + entity);
em.persist(entity);
// uncomment for debugging purposes
// em.flush(); // makes it slow, but produces better error messages
// System.out.println("persisted #" + entity.hashCode() + " as " + entity.getUuid());
return persistViaEM(id, entity);
} catch (Exception exc) {
errors.add("failed to persist #" + entity.hashCode() + ": " + entity);
errors.add(exc.toString());
@ -155,6 +154,62 @@ public class CsvDataImport extends ContextBasedTest {
return entity;
}
public <T extends RbacObject> T persistViaEM(final Integer id, final T entity) {
//System.out.println("persisting #" + entity.hashCode() + ": " + entity);
em.persist(entity);
// uncomment for debugging purposes
// em.flush(); // makes it slow, but produces better error messages
// System.out.println("persisted #" + entity.hashCode() + " as " + entity.getUuid());
return entity;
}
@SneakyThrows
public RbacObject<HsHostingAsset> persistViaSql(final Integer id, final HsHostingAssetRawEntity entity) {
if (entity.getUuid() == null) {
entity.setUuid(UUID.randomUUID());
}
final var query = em.createNativeQuery("""
insert into hs_hosting_asset(
uuid,
type,
bookingitemuuid,
parentassetuuid,
assignedtoassetuuid,
alarmcontactuuid,
identifier,
caption,
config,
version)
values (
:uuid,
:type,
:bookingitemuuid,
:parentassetuuid,
:assignedtoassetuuid,
:alarmcontactuuid,
:identifier,
:caption,
cast(:config as jsonb),
:version)
""")
.setParameter("uuid", entity.getUuid())
.setParameter("type", entity.getType().name())
.setParameter("bookingitemuuid", ofNullable(entity.getBookingItem()).map(RbacObject::getUuid).orElse(null))
.setParameter("parentassetuuid", ofNullable(entity.getParentAsset()).map(RbacObject::getUuid).orElse(null))
.setParameter("assignedtoassetuuid", ofNullable(entity.getAssignedToAsset()).map(RbacObject::getUuid).orElse(null))
.setParameter("alarmcontactuuid", ofNullable(entity.getAlarmContact()).map(RbacObject::getUuid).orElse(null))
.setParameter("identifier", entity.getIdentifier())
.setParameter("caption", entity.getCaption())
.setParameter("config", entity.getConfig().toString())
.setParameter("version", entity.getVersion());
logError(() -> {
final var count = query.executeUpdate();
assertThat(count).isEqualTo(1);
});
return entity;
}
protected <E> String toFormattedString(final Map<Integer, E> map) {
if ( map.isEmpty() ) {
return "{}";
@ -287,7 +342,7 @@ class Record {
}
String getString(final String columnName) {
return row[columns.indexOf(columnName)];
return row[columns.indexOf(columnName)].trim();
}
boolean isEmpty(final String columnName) {

View File

@ -132,7 +132,6 @@ public class ImportHostingAssets extends ImportOfficeData {
void verifyIpNumbers() {
assumeThatWeAreImportingControlledTestData();
// no contacts yet => mostly null values
assertThat(firstOfEachType(5, IPV4_NUMBER)).isEqualToIgnoringWhitespace("""
{
1000363=HsHostingAssetRawEntity(IPV4_NUMBER, 83.223.95.34),
@ -160,7 +159,6 @@ public class ImportHostingAssets extends ImportOfficeData {
void verifyHives() {
assumeThatWeAreImportingControlledTestData();
// no contacts yet => mostly null values
assertThat(toFormattedString(first(5, hives))).isEqualToIgnoringWhitespace("""
{
2000001=Hive[hive_id=1, hive_name=h00, inet_addr_id=358, serverRef=null],
@ -232,7 +230,6 @@ public class ImportHostingAssets extends ImportOfficeData {
void verifyPacketComponents() {
assumeThatWeAreImportingControlledTestData();
// no contacts yet => mostly null values
assertThat(firstOfEachType(5, CLOUD_SERVER, MANAGED_SERVER, MANAGED_WEBSPACE))
.isEqualToIgnoringWhitespace("""
{
@ -283,7 +280,6 @@ public class ImportHostingAssets extends ImportOfficeData {
void verifyUnixUsers() {
assumeThatWeAreImportingControlledTestData();
// no contacts yet => mostly null values
assertThat(firstOfEachType(15, UNIX_USER)).isEqualToIgnoringWhitespace("""
{
4005803=HsHostingAssetRawEntity(UNIX_USER, lug00, LUGs, MANAGED_WEBSPACE:lug00, { "SSD hard quota": 0, "SSD soft quota": 0, "locked": false, "shell": "/bin/bash", "userid": 102090}),
@ -320,7 +316,6 @@ public class ImportHostingAssets extends ImportOfficeData {
void verifyEmailAliases() {
assumeThatWeAreImportingControlledTestData();
// no contacts yet => mostly null values
assertThat(firstOfEachType(15, EMAIL_ALIAS)).isEqualToIgnoringWhitespace("""
{
5002403=HsHostingAssetRawEntity(EMAIL_ALIAS, lug00, lug00, MANAGED_WEBSPACE:lug00, { "target": "[michael.mellis@example.com]"}),
@ -456,7 +451,6 @@ public class ImportHostingAssets extends ImportOfficeData {
void verifyPersistedUnixUsersWithUserId() {
assumeThatWeAreImportingControlledTestData();
// no contacts yet => mostly null value
assertThat(firstOfEachType(15, UNIX_USER)).isEqualToIgnoringWhitespace("""
{
4005803=HsHostingAssetRawEntity(UNIX_USER, lug00, LUGs, MANAGED_WEBSPACE:lug00, { "SSD hard quota": 0, "SSD soft quota": 0, "locked": false, "password": null, "shell": "/bin/bash", "userid": 102090}),
@ -477,6 +471,20 @@ public class ImportHostingAssets extends ImportOfficeData {
""");
}
@Test
@Order(19910)
void verifyBookingItemsAreActuallyPersisted() {
final var biCount = (Integer) em.createNativeQuery("SELECT count(*) FROM hs_booking_item", Integer.class).getSingleResult();
assertThat(biCount).isGreaterThan(isImportingControlledTestData() ? 5 : 500);
}
@Test
@Order(19920)
void verifyHostingAssetsAreActuallyPersisted() {
final var haCount = (Integer) em.createNativeQuery("SELECT count(*) FROM hs_hosting_asset", Integer.class).getSingleResult();
assertThat(haCount).isGreaterThan(isImportingControlledTestData() ? 20 : 10000);
}
// ============================================================================================
@Test