Skip to content

Commit

Permalink
update micronaut, add testuser to sql migration, saving new WidgetCon…
Browse files Browse the repository at this point in the history
…figValue
  • Loading branch information
stCarolas committed Mar 6, 2024
1 parent b7c2f33 commit de3f972
Show file tree
Hide file tree
Showing 14 changed files with 124 additions and 25 deletions.
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FROM eclipse-temurin:17-jdk-jammy
FROM fedora:39
WORKDIR /app
COPY target/oda-config-service-0.1.jar /app
COPY target/oda-config-service /app

CMD ["java","--add-opens","java.base/java.time=ALL-UNNAMED","-jar","oda-config-service-0.1.jar"]
CMD ["./oda-config-service"]
1 change: 1 addition & 0 deletions TODO
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
default paymentpage config
8 changes: 8 additions & 0 deletions local.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version=`date +%s`
imagename="ghcr.io/opendonationassistant/oda-config-service:$version"

export JAVA_HOME=/usr/lib/jvm/graalvm-jdk-21.0.2+13.1
mvn clean package -Dpackaging=native-image
podman build . -t $imagename
podman save $imagename | sudo k3s ctr image import -
kubectl get deploy config-service -o json | jq ".spec.template.spec.containers[0].image = \"$imagename\"" | kubectl apply -f -
17 changes: 10 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
<parent>
<groupId>io.micronaut.platform</groupId>
<artifactId>micronaut-parent</artifactId>
<version>4.1.0</version>
<version>4.2.3</version>
</parent>
<!--#endregion-->
<!--#region properties-->
<properties>
<packaging>jar</packaging>
<jdk.version>17</jdk.version>
<release.version>17</release.version>
<micronaut.version>4.1.0</micronaut.version>
<micronaut.version>4.2.3</micronaut.version>
<micronaut.runtime>netty</micronaut.runtime>
<micronaut.test.resources.enabled>true</micronaut.test.resources.enabled>
<micronaut.aot.enabled>false</micronaut.aot.enabled>
Expand Down Expand Up @@ -101,15 +101,11 @@
</dependency>
<!-- #endregion -->
<!-- #region test -->
<dependency>
<groupId>io.micronaut.testresources</groupId>
<artifactId>micronaut-test-resources-core</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.10.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
Expand All @@ -121,6 +117,12 @@
<artifactId>postgresql</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.micronaut.testresources</groupId>
<artifactId>micronaut-test-resources-core</artifactId>
<version>2.3.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.micronaut.testresources</groupId>
<artifactId>micronaut-test-resources-extensions-core</artifactId>
Expand Down Expand Up @@ -182,6 +184,7 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<useIncrementalCompilation>false</useIncrementalCompilation>
<annotationProcessorPaths combine.children="append">
<path>
<groupId>io.micronaut</groupId>
Expand Down
21 changes: 20 additions & 1 deletion src/main/java/io/github/stcarolas/oda/Application.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
package io.github.stcarolas.oda;

import io.micronaut.context.ApplicationContext;
import io.micronaut.context.ApplicationContextBuilder;
import io.micronaut.context.ApplicationContextConfigurer;
import io.micronaut.context.annotation.ContextConfigurer;
import io.micronaut.runtime.Micronaut;

public class Application {

@ContextConfigurer
public static class DefaultEnvironmentConfigurer
implements ApplicationContextConfigurer {

@Override
public void configure(ApplicationContextBuilder builder) {
builder.defaultEnvironments("standalone");
}
}

public static void main(String[] args) {
Beans.context = Micronaut.build(args).banner(false).start();
ApplicationContext context = Micronaut
.build(args)
.mainClass(Application.class)
.banner(false)
.start();
Beans.context = context;
}
}
7 changes: 7 additions & 0 deletions src/main/java/io/github/stcarolas/oda/config/ConfigValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,11 @@ public String getId() {
public void setId(String id) {
this.id = id;
}

@Override
public String toString() {
return "{\"_type\"=\"ConfigValue\",\"id\"=\"" + id + "\", name\"=\"" + name + "\", ownerId\"=\"" + ownerId
+ "\", value\"=\"" + value + "}";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.micronaut.core.annotation.NonNull;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import java.util.HashMap;
import java.util.Objects;
import java.util.Optional;

Expand All @@ -25,9 +26,31 @@ public ConfigValueAbstractFactory(@NonNull ConfigRepository repository) {
Objects.requireNonNull(ownerId, "Missing ownerId to search for config");
Objects.requireNonNull(name, "Missing config's name to search for it");
Optional<ConfigValue> value = repository.find(ownerId, name);
if (value.isEmpty() && "widgets".equalsIgnoreCase(name)) {
SaveableConfigValue widgetsValue = new SaveableConfigValue(
"widgets",
ownerId,
new HashMap<>(),
repository
);
widgetsValue.save();
return Optional.of(
new WidgetsConfigValue(
widgetsValue.getId(),
widgetsValue.getOwnerId(),
widgetsValue.getValue(),
repository
)
);
}
return value.map(it ->
"widgets".equals(it.getName())
? new WidgetsConfigValue(it.getId(), it.getOwnerId(), it.getValue(), repository)
? new WidgetsConfigValue(
it.getId(),
it.getOwnerId(),
it.getValue(),
repository
)
: it
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import io.github.stcarolas.oda.config.ConfigRepository;
import io.github.stcarolas.oda.config.SaveableConfigValue;
import io.micronaut.core.util.StringUtils;
import io.micronaut.serde.annotation.Serdeable;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.uuid.Generators;

@Serdeable
public class WidgetsConfigValue extends SaveableConfigValue {

Expand All @@ -16,13 +19,22 @@ public WidgetsConfigValue(
ConfigRepository repository
) {
super("widgets", ownerId, values, repository);
this.setId(id);
this.setId(
StringUtils.isEmpty(id)
? Generators.timeBasedEpochGenerator().generate().toString()
: id
);
this.setName("widgets");

var topicValues = (Map<String, Object>) values.getOrDefault(
if (values == null) {
values = new HashMap<>();
}

var topicValues = new HashMap((Map<String, Object>) values.getOrDefault(
"topic",
new HashMap<>()
);
));

defaultValues(ownerId)
.entrySet()
.stream()
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/application-allinone.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
datasources:
default:
driverClassName: 'org.postgresql.Driver'
db-type: "postgresql"
7 changes: 7 additions & 0 deletions src/main/resources/application-standalone.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
rabbitmq.host: ${RABBITMQ_HOST:`localhost`}
datasources:
default:
url: ${JDBC_URL:`jdbc:postgresql://localhost/postgres?currentSchema=config`}
username: ${JDBC_USER:`postgres`}
password: ${JDBC_PASSWORD:`postgres`}
driverClassName: 'org.postgresql.Driver'
7 changes: 0 additions & 7 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@ micronaut:
jwks:
keycloak:
url: https://auth.oda.digital/realms/ODA/protocol/openid-connect/certs
datasources:
default:
url: ${JDBC_URL}
username: ${JDBC_USER:`postgres`}
password: ${JDBC_PASSWORD:`postgres`}
db-type: "postgresql"
driverClassName: 'org.postgresql.Driver'
endpoints:
flyway:
enabled: true
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/db/migration/V2__add-testuser.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
insert into config(id, name, owner_id, value) values ('018e139a-89e6-75cb-adc2-d02202470b14', 'paymentpage', 'testuser', '{"fio": "Иванов Иван Иванович", "inn": "111111111111", "email": "[email protected]", "nickname": "testuser", "arbitraryText": "приветик, \nспасибо за донатик))", "media.requests.enabled": true}')
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@MicronautTest
@MicronautTest(environments = "allinone")
public class ConfigControllerTest {

Logger log = LoggerFactory.getLogger(ConfigControllerTest.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github.stcarolas.oda.config;

import static java.util.Optional.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -55,7 +54,7 @@ public void testReturnStoredValue() {
var factory = new ConfigValueAbstractFactory(mockRepository);
var expected = Optional.of(config);
assertEquals(expected, factory.findExisting("testuser", "testname"));
verify(mockRepository).find("testuser","testname");
verify(mockRepository).find("testuser", "testname");
}

@Test
Expand Down Expand Up @@ -92,4 +91,26 @@ public void testReturnDefaultValuesForWidgetsIfMissing() throws IOException {
);
assertEquals(expectedValue, config.get().getValue());
}

@Test
public void testMergingSavedWidgetConfigWithDefaultValues() {
Map<String, Object> configValues = Map.of(
"topic",
Map.of("alerts", "sometestvalue")
);
when(mockRepository.find(Mockito.any(), Mockito.any()))
.thenReturn(
Optional.of(new ConfigValue("id", "widgets", "testuser", configValues))
);
var factory = new ConfigValueAbstractFactory(mockRepository);

Optional<ConfigValue> config = factory.findExisting("testuser", "widgets");

assertTrue(config.isPresent());
ConfigValue fact = config.get();
assertEquals(
"sometestvalue",
((Map<String, Object>) fact.getValue().get("topic")).get("alerts")
);
}
}

0 comments on commit de3f972

Please sign in to comment.