Skip to content

Commit

Permalink
Merge branch 'work/using-react-with-bootstrap4'
Browse files Browse the repository at this point in the history
  • Loading branch information
steinarb committed Jul 4, 2024
2 parents 7d255b7 + 7f8ad34 commit dbca829
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 164 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<parent>
<groupId>no.priv.bang.pom</groupId>
<artifactId>bang-authservice-client-pom</artifactId>
<version>2.0.0</version>
<version>2.0.2</version>
</parent>
<groupId>no.priv.bang.ukelonn</groupId>
<artifactId>ukelonn</artifactId>
Expand Down
7 changes: 6 additions & 1 deletion ukelonn.backend/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<!-- Copyright 2016-2023 Steinar Bang -->
<!-- Copyright 2016-2024 Steinar Bang -->
<!-- -->
<!-- Licensed under the Apache License, Version 2.0 (the "License"); -->
<!-- you may not use this file except in compliance with the License. -->
Expand Down Expand Up @@ -54,6 +54,11 @@
<artifactId>authservice.definitions</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>no.priv.bang.karaf</groupId>
<artifactId>karaf.liquibase.runner</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>no.priv.bang.authservice</groupId>
<artifactId>authservice.db.liquibase</artifactId>
Expand Down
7 changes: 6 additions & 1 deletion ukelonn.db.liquibase.production/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<!-- Copyright 2016-2023 Steinar Bang -->
<!-- Copyright 2016-2024 Steinar Bang -->
<!-- -->
<!-- Licensed under the Apache License, Version 2.0 (the "License"); -->
<!-- you may not use this file except in compliance with the License. -->
Expand Down Expand Up @@ -59,6 +59,11 @@
<artifactId>org.osgi.service.component.annotations</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>no.priv.bang.karaf</groupId>
<artifactId>karaf.liquibase.runner</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>no.priv.bang.authservice</groupId>
<artifactId>authservice.db.liquibase</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package no.priv.bang.ukelonn.db.liquibase.production;

import static liquibase.command.core.helpers.DbUrlConnectionArgumentsCommandStep.DATABASE_ARG;

import java.sql.SQLException;
import java.util.Map;

Expand All @@ -30,15 +28,7 @@
import org.osgi.service.log.Logger;

import liquibase.Scope;
import liquibase.Scope.ScopedRunner;
import liquibase.ThreadLocalScopeManager;
import liquibase.changelog.ChangeLogParameters;
import liquibase.command.CommandScope;
import liquibase.command.core.UpdateCommandStep;
import liquibase.command.core.helpers.DatabaseChangelogCommandStep;
import liquibase.database.DatabaseFactory;
import liquibase.database.jvm.JdbcConnection;
import liquibase.resource.ClassLoaderResourceAccessor;
import no.priv.bang.ukelonn.db.liquibase.UkelonnLiquibase;

@Component(immediate=true, property = "name=ukelonndb")
Expand All @@ -64,27 +54,16 @@ public void prepare(DataSource datasource) throws SQLException {
try {
var liquibase = createUkelonnLiquibase();
liquibase.createInitialSchema(datasource);
insertInitialDataInDatabase(datasource);
insertInitialDataInDatabase(datasource, liquibase);
liquibase.updateSchema(datasource);
} catch (Exception e) {
logger.error("Failed to create ukelonn database schema in the PostgreSQL ukelonn database", e);
}
}

boolean insertInitialDataInDatabase(DataSource datasource) {
boolean insertInitialDataInDatabase(DataSource datasource, UkelonnLiquibase liquibase) {
try(var connect = datasource.getConnection()) {
try (var database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connect))) {
Map<String, Object> scopeObjects = Map.of(
Scope.Attr.database.name(), database,
Scope.Attr.resourceAccessor.name(), new ClassLoaderResourceAccessor(getClass().getClassLoader()));

Scope.child(scopeObjects, (ScopedRunner<?>) () -> new CommandScope("update")
.addArgumentValue(DATABASE_ARG, database)
.addArgumentValue(UpdateCommandStep.CHANGELOG_FILE_ARG, initialDataResourceName())
.addArgumentValue(DatabaseChangelogCommandStep.CHANGELOG_PARAMETERS, new ChangeLogParameters(database))
.execute());
}

liquibase.applyLiquibaseChangelist(connect, initialDataResourceName(), getClass().getClassLoader());
return true;
} catch (Exception e) {
logger.error("Failed to fill ukelonn PostgreSQL database with initial data.", e);
Expand All @@ -95,11 +74,11 @@ boolean insertInitialDataInDatabase(DataSource datasource) {
UkelonnLiquibase createUkelonnLiquibase() {
if (ukelonnLiquibaseFactory == null) {
ukelonnLiquibaseFactory = new UkelonnLiquibaseFactory() { // NOSONAR
@Override
public UkelonnLiquibase create() {
return new UkelonnLiquibase();
}
};
@Override
public UkelonnLiquibase create() {
return new UkelonnLiquibase();
}
};
}

return ukelonnLiquibaseFactory.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.osgi.service.jdbc.DataSourceFactory;

import no.priv.bang.osgi.service.mocks.logservice.MockLogService;
import no.priv.bang.ukelonn.db.liquibase.UkelonnLiquibase;

import static no.priv.bang.ukelonn.db.liquibase.production.ProductionLiquibaseRunner.*;

class ProductionLiquibaseRunnerTest {
Expand Down Expand Up @@ -72,12 +74,13 @@ void testPrepareWhenSQLExceptionIsThrown() throws Exception {
@Test
void testInsertInitialDataInDatabaseFailToCreateLiquibase() throws Exception {
var runner = new ProductionLiquibaseRunner();
var liquibase = new UkelonnLiquibase();
var logservice = new MockLogService();
runner.setLogService(logservice);
runner.activate(Collections.emptyMap());
var datasource = mock(DataSource.class);
when(datasource.getConnection()).thenThrow(SQLException.class);
var successfullyinserteddata = runner.insertInitialDataInDatabase(datasource );
var successfullyinserteddata = runner.insertInitialDataInDatabase(datasource, liquibase);
assertFalse(successfullyinserteddata);
}

Expand Down
7 changes: 6 additions & 1 deletion ukelonn.db.liquibase.test/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<!-- Copyright 2016-2023 Steinar Bang -->
<!-- Copyright 2016-2024 Steinar Bang -->
<!-- -->
<!-- Licensed under the Apache License, Version 2.0 (the "License"); -->
<!-- you may not use this file except in compliance with the License. -->
Expand Down Expand Up @@ -42,6 +42,11 @@
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>no.priv.bang.karaf</groupId>
<artifactId>karaf.liquibase.runner</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>no.priv.bang.authservice</groupId>
<artifactId>authservice.db.liquibase</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package no.priv.bang.ukelonn.db.liquibase.test;

import static liquibase.command.core.helpers.DbUrlConnectionArgumentsCommandStep.DATABASE_ARG;

import java.sql.SQLException;
import java.util.Collections;
import java.util.List;
Expand All @@ -33,14 +31,9 @@

import liquibase.Liquibase;
import liquibase.Scope;
import liquibase.Scope.ScopedRunner;
import liquibase.ThreadLocalScopeManager;
import liquibase.changelog.ChangeLogHistoryServiceFactory;
import liquibase.changelog.ChangeLogParameters;
import liquibase.changelog.RanChangeSet;
import liquibase.command.CommandScope;
import liquibase.command.core.UpdateCommandStep;
import liquibase.command.core.helpers.DatabaseChangelogCommandStep;
import liquibase.database.DatabaseFactory;
import liquibase.database.jvm.JdbcConnection;
import liquibase.exception.DatabaseException;
Expand Down Expand Up @@ -69,27 +62,16 @@ public void prepare(DataSource datasource) throws SQLException {
var liquibase = new UkelonnLiquibase();
try {
liquibase.createInitialSchema(datasource);
insertMockData(datasource);
insertMockData(datasource, liquibase);
liquibase.updateSchema(datasource);
} catch (Exception e) {
logger.error("Failed to create derby test database schema", e);
}
}

public boolean insertMockData(DataSource datasource) {
public boolean insertMockData(DataSource datasource, UkelonnLiquibase liquibase) {
try(var connect = datasource.getConnection()) {
try (var database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connect))) {
Map<String, Object> scopeObjects = Map.of(
Scope.Attr.database.name(), database,
Scope.Attr.resourceAccessor.name(), new ClassLoaderResourceAccessor(getClass().getClassLoader()));

Scope.child(scopeObjects, (ScopedRunner<?>) () -> new CommandScope("update")
.addArgumentValue(DATABASE_ARG, database)
.addArgumentValue(UpdateCommandStep.CHANGELOG_FILE_ARG, dummyDataResourceName())
.addArgumentValue(DatabaseChangelogCommandStep.CHANGELOG_PARAMETERS, new ChangeLogParameters(database))
.execute());
}

liquibase.applyLiquibaseChangelist(connect, dummyDataResourceName(), getClass().getClassLoader());
return true;
} catch (Exception e) {
logger.error("Failed to fill derby test database with data.", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.sql.SQLException;
import java.util.Base64;
import java.util.Collections;
import java.util.Map;
import java.util.Properties;

import javax.sql.DataSource;
Expand All @@ -40,22 +39,11 @@
import org.junit.jupiter.api.Test;
import org.ops4j.pax.jdbc.derby.impl.DerbyDataSourceFactory;
import org.osgi.service.jdbc.DataSourceFactory;
import liquibase.Scope;
import liquibase.Scope.ScopedRunner;
import liquibase.changelog.ChangeLogParameters;
import liquibase.command.CommandScope;
import liquibase.command.core.UpdateCommandStep;
import liquibase.command.core.helpers.DatabaseChangelogCommandStep;
import liquibase.database.DatabaseFactory;
import liquibase.database.jvm.JdbcConnection;
import liquibase.exception.DatabaseException;
import liquibase.exception.LiquibaseException;
import liquibase.resource.ClassLoaderResourceAccessor;
import no.priv.bang.osgi.service.mocks.logservice.MockLogService;
import no.priv.bang.ukelonn.UkelonnException;
import no.priv.bang.ukelonn.db.liquibase.UkelonnLiquibase;

import static liquibase.command.core.helpers.DbUrlConnectionArgumentsCommandStep.DATABASE_ARG;
import static no.priv.bang.ukelonn.db.liquibase.test.TestLiquibaseRunner.*;

class TestLiquibaseRunnerTest {
Expand Down Expand Up @@ -236,11 +224,12 @@ void testInsert() throws SQLException {
@Test
void testFailToInsertMockData() throws SQLException {
var runner = new TestLiquibaseRunner();
var liquibase = new UkelonnLiquibase();
runner.setLogService(new MockLogService());
var datasource = mock(DataSource.class);
when(datasource.getConnection()).thenThrow(SQLException.class);

var result = runner.insertMockData(datasource);
var result = runner.insertMockData(datasource, liquibase);
assertFalse(result);
}

Expand Down Expand Up @@ -397,7 +386,7 @@ void testCreateHashedPasswords() {
*/
@Disabled("Not an actual unit test. This test is a convenient way to populate a derby network server running on localhost, with the ukelonn schema and test data, using liquibase.")
@Test
void addUkelonnSchemaAndDataToDerbyServer() throws SQLException, LiquibaseException { // NOSONAR This isn't an actual test, see the comments
void addUkelonnSchemaAndDataToDerbyServer() throws Exception { // NOSONAR This isn't an actual test, see the comments
var createUkelonnDatabase = true;
var dataSource = new ClientConnectionPoolDataSource();
dataSource.setServerName("localhost");
Expand All @@ -411,21 +400,7 @@ void addUkelonnSchemaAndDataToDerbyServer() throws SQLException, LiquibaseExcept
liquibase.createInitialSchema(dataSource);

try(var connect = dataSource.getConnection()) {
try (var database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connect))) {
Map<String, Object> scopeObjects = Map.of(
Scope.Attr.database.name(), database,
Scope.Attr.resourceAccessor.name(), new ClassLoaderResourceAccessor(getClass().getClassLoader()));

Scope.child(scopeObjects, (ScopedRunner<?>) () -> new CommandScope("update")
.addArgumentValue(DATABASE_ARG, database)
.addArgumentValue(UpdateCommandStep.CHANGELOG_FILE_ARG, "sql/data/db-changelog.xml")
.addArgumentValue(DatabaseChangelogCommandStep.CHANGELOG_PARAMETERS, new ChangeLogParameters(database))
.execute());
} catch (LiquibaseException e) {
throw e;
} catch (Exception e) {
throw new UkelonnException("Failed to close resource when inserting data");
}
liquibase.applyLiquibaseChangelist(connect, "sql/data/db-changelog.xml", getClass().getClassLoader());
}

liquibase.updateSchema(dataSource);
Expand Down
Loading

0 comments on commit dbca829

Please sign in to comment.