-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f4664cf
commit cce3e9d
Showing
13 changed files
with
371 additions
and
13 deletions.
There are no files selected for viewing
183 changes: 183 additions & 0 deletions
183
...tegrationTest/java/ch/admin/bar/siard2/cmd/usecases/keys/MsSqlDownloadSiardProjectIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
package ch.admin.bar.siard2.cmd.usecases.keys; | ||
|
||
import ch.admin.bar.siard2.cmd.SiardFromDb; | ||
import ch.admin.bar.siard2.cmd.utils.CollectionsHelper; | ||
import ch.admin.bar.siard2.cmd.utils.siard.SiardArchivesHandler; | ||
import ch.admin.bar.siard2.cmd.utils.siard.model.header.Metadata; | ||
import ch.admin.bar.siard2.cmd.utils.siard.model.utils.Id; | ||
import ch.admin.bar.siard2.cmd.utils.siard.model.utils.QualifiedColumnId; | ||
import ch.admin.bar.siard2.cmd.utils.siard.model.utils.QualifiedForeignKeyId; | ||
import ch.admin.bar.siard2.cmd.utils.siard.model.utils.QualifiedPrimaryKeyId; | ||
import ch.admin.bar.siard2.cmd.utils.siard.model.utils.QualifiedTableId; | ||
import ch.admin.bar.siard2.cmd.utils.siard.model.utils.StringWrapper; | ||
import lombok.val; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.Assert; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.testcontainers.containers.MSSQLServerContainer; | ||
import org.testcontainers.utility.DockerImageName; | ||
|
||
import java.io.IOException; | ||
import java.sql.SQLException; | ||
import java.util.Optional; | ||
|
||
public class MsSqlDownloadSiardProjectIT { | ||
|
||
/** | ||
* +-------------------+ +-------------------+ +---------------------+ | ||
* | Schema1 | | Schema2 | | Schema2 | | ||
* +-------------------+ +-------------------+ +---------------------+ | ||
* | | | | | | | ||
* | Teams | | Members | | TeamMembers | | ||
* |-------------------| |-------------------| |---------------------| | ||
* | TeamName (PK) | | MemberID (PK) | | TeamMembersID (PK) | | ||
* | Location (PK) | | MemberName | | TeamName (FK) | | ||
* | | | | | Location (FK) | | ||
* | | | | | MemberID (FK) | | ||
* +-------------------+ +-------------------+ +---------------------+ | ||
*/ | ||
public final static String CREATE_SIMPLE_TEAMS_EXAMPLE = "usecases/keys/create-simple-teams-example_mssql.sql"; | ||
|
||
public final static Id<Metadata.Schema> SCHEMA_1 = Id.of("Schema1"); | ||
public final static Id<Metadata.Schema> SCHEMA_2 = Id.of("Schema2"); | ||
|
||
public final static Id<Metadata.Table> TABLE_TEAMS = Id.of("Teams"); | ||
public final static Id<Metadata.Table> TABLE_MEMBERS = Id.of("Members"); | ||
public final static Id<Metadata.Table> TABLE_TEAM_MEMBERS = Id.of("TeamMembers"); | ||
|
||
public final static Id<Metadata.Column> COLUMN_TEAM_NAME = Id.of("TeamName"); | ||
public final static Id<Metadata.Column> COLUMN_LOCATION = Id.of("Location"); | ||
public final static Id<Metadata.Column> COLUMN_MEMBER_ID = Id.of("MemberID"); | ||
public final static Id<Metadata.Column> COLUMN_MEMBER_NAME = Id.of("MemberName"); | ||
public final static Id<Metadata.Column> COLUMN_TEAM_MEMBERS_ID = Id.of("TeamMembersID"); | ||
|
||
|
||
|
||
private static final QualifiedTableId TEAM_MEMBERS = QualifiedTableId.builder() | ||
.schemaId(SCHEMA_2) | ||
.tableId(Id.of("TeamMembers")) | ||
.build(); | ||
|
||
private static final QualifiedTableId TEAMS = QualifiedTableId.builder() | ||
.schemaId(SCHEMA_1) | ||
.tableId(Id.of("Teams")) | ||
.build(); | ||
|
||
private static final QualifiedTableId MEMBERS = QualifiedTableId.builder() | ||
.schemaId(SCHEMA_2) | ||
.tableId(Id.of("Members")) | ||
.build(); | ||
|
||
private static final QualifiedForeignKeyId TEAM_MEMBERS_2_TEAMS_FOREIGN_KEY = QualifiedForeignKeyId.builder() | ||
.qualifiedTableId(TEAM_MEMBERS) | ||
.foreignKeyId(Id.of("FK_TeamMembers_TeamID")) | ||
.build(); | ||
|
||
private static final QualifiedForeignKeyId TEAM_MEMBERS_2_MEMBERS_FOREIGN_KEY = QualifiedForeignKeyId.builder() | ||
.qualifiedTableId(TEAM_MEMBERS) | ||
.foreignKeyId(Id.of("FK_TeamMembers_MemberID")) | ||
.build(); | ||
|
||
private static final QualifiedPrimaryKeyId TEAMS_PRIMARY_KEY = QualifiedPrimaryKeyId.builder() | ||
.qualifiedTableId(TEAMS) | ||
.primaryKeyId(Id.of("PK_Teams")) | ||
.build(); | ||
|
||
private static final QualifiedPrimaryKeyId MEMBERS_PRIMARY_KEY = QualifiedPrimaryKeyId.builder() | ||
.qualifiedTableId(MEMBERS) | ||
.primaryKeyId(Id.of("PK_Members")) | ||
.build(); | ||
|
||
private static final QualifiedPrimaryKeyId TEAM_MEMBERS_PRIMARY_KEY = QualifiedPrimaryKeyId.builder() | ||
.qualifiedTableId(TEAM_MEMBERS) | ||
.primaryKeyId(Id.of("PK_TeamMembers")) | ||
.build(); | ||
|
||
|
||
@Rule | ||
public SiardArchivesHandler siardArchivesHandler = new SiardArchivesHandler(); | ||
|
||
@Rule | ||
public MSSQLServerContainer<?> db = new MSSQLServerContainer<>(DockerImageName.parse("mcr.microsoft.com/mssql/server:2017-CU12")) | ||
.acceptLicense() | ||
.withInitScript(CREATE_SIMPLE_TEAMS_EXAMPLE); | ||
|
||
@Test | ||
public void uploadAndDownload_expectNoExceptions() throws IOException, SQLException, ClassNotFoundException { | ||
// given | ||
val actualArchive = siardArchivesHandler.prepareEmpty(); | ||
|
||
// when | ||
SiardFromDb siardFromDb = new SiardFromDb(new String[]{ | ||
"-o", | ||
"-j:" + db.getJdbcUrl(), | ||
"-u:" + db.getUsername(), | ||
"-p:" + db.getPassword(), | ||
"-s:" + actualArchive.getPathToArchiveFile() | ||
}); | ||
|
||
// then | ||
Assert.assertEquals(SiardFromDb.iRETURN_OK, siardFromDb.getReturn()); | ||
|
||
actualArchive.preserveArchive(); | ||
|
||
val metadataExplorer = actualArchive.exploreMetadata(); | ||
metadataExplorer.tryFindByColumnId(QualifiedColumnId.builder() | ||
.schemaId(SCHEMA_1) | ||
.tableId(TABLE_TEAMS) | ||
.columnId(COLUMN_TEAM_NAME) | ||
.build()); | ||
|
||
Assertions.assertThat(metadataExplorer.tryFindPrimaryKey(TEAMS)) | ||
.contains(Metadata.PrimaryKey.builder() | ||
.name(TEAMS_PRIMARY_KEY.getPrimaryKeyId()) | ||
.columns(CollectionsHelper.setOf( | ||
COLUMN_LOCATION, | ||
COLUMN_TEAM_NAME | ||
)) | ||
.build()); | ||
|
||
Assertions.assertThat(metadataExplorer.tryFindPrimaryKey(MEMBERS)) | ||
.contains(Metadata.PrimaryKey.builder() | ||
.name(MEMBERS_PRIMARY_KEY.getPrimaryKeyId()) | ||
.columns(CollectionsHelper.setOf(COLUMN_MEMBER_ID)) | ||
.build()); | ||
|
||
Assertions.assertThat(metadataExplorer.tryFindPrimaryKey(TEAM_MEMBERS)) | ||
.contains(Metadata.PrimaryKey.builder() | ||
.name(TEAM_MEMBERS_PRIMARY_KEY.getPrimaryKeyId()) | ||
.columns(CollectionsHelper.setOf(COLUMN_TEAM_MEMBERS_ID)) | ||
.build()); | ||
|
||
Assertions.assertThat(metadataExplorer.findForeignKey(TEAM_MEMBERS_2_TEAMS_FOREIGN_KEY)) | ||
.isEqualTo(Metadata.ForeignKey.builder() | ||
.name(TEAM_MEMBERS_2_TEAMS_FOREIGN_KEY.getForeignKeyId()) | ||
.referencedSchema(TEAMS.getSchemaId()) | ||
.referencedTable(TEAMS.getTableId()) | ||
.references(CollectionsHelper.setOf(Metadata.Reference.builder() | ||
.column(COLUMN_TEAM_NAME) | ||
.referenced(COLUMN_TEAM_NAME) | ||
.build(), | ||
Metadata.Reference.builder() | ||
.column(COLUMN_LOCATION) | ||
.referenced(COLUMN_LOCATION) | ||
.build())) | ||
.deleteAction(Optional.of(StringWrapper.of("CASCADE"))) | ||
.updateAction(Optional.of(StringWrapper.of("CASCADE"))) | ||
.build()); | ||
|
||
Assertions.assertThat(metadataExplorer.findForeignKey(TEAM_MEMBERS_2_MEMBERS_FOREIGN_KEY)) | ||
.isEqualTo(Metadata.ForeignKey.builder() | ||
.name(TEAM_MEMBERS_2_MEMBERS_FOREIGN_KEY.getForeignKeyId()) | ||
.referencedSchema(MEMBERS.getSchemaId()) | ||
.referencedTable(MEMBERS.getTableId()) | ||
.references(CollectionsHelper.setOf(Metadata.Reference.builder() | ||
.column(COLUMN_MEMBER_ID) | ||
.referenced(COLUMN_MEMBER_ID) | ||
.build())) | ||
.deleteAction(Optional.of(StringWrapper.of("RESTRICT"))) // FIXME: Should be "NO ACTION" (RESTRICT is unknown in MS SQL) | ||
.updateAction(Optional.of(StringWrapper.of("RESTRICT"))) // FIXME: Should be "NO ACTION" (RESTRICT is unknown in MS SQL) | ||
.build()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
...ationTest/java/ch/admin/bar/siard2/cmd/utils/siard/model/utils/QualifiedForeignKeyId.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package ch.admin.bar.siard2.cmd.utils.siard.model.utils; | ||
|
||
import ch.admin.bar.siard2.cmd.utils.siard.model.header.Metadata; | ||
import lombok.Builder; | ||
import lombok.NonNull; | ||
import lombok.Value; | ||
|
||
@Value | ||
@Builder | ||
public class QualifiedForeignKeyId { | ||
@NonNull Id<Metadata.Schema> schemaId; | ||
@NonNull Id<Metadata.Table> tableId; | ||
@NonNull Id<Metadata.ForeignKey> foreignKeyId; | ||
|
||
public QualifiedTableId getQualifiedTableId() { | ||
return QualifiedTableId.builder() | ||
.schemaId(schemaId) | ||
.tableId(tableId) | ||
.build(); | ||
} | ||
|
||
public static class QualifiedForeignKeyIdBuilder { | ||
public QualifiedForeignKeyIdBuilder qualifiedTableId(final QualifiedTableId qualifiedTableId) { | ||
schemaId(qualifiedTableId.getSchemaId()); | ||
tableId(qualifiedTableId.getTableId()); | ||
|
||
return this; | ||
} | ||
} | ||
} |
Oops, something went wrong.