这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions microservices-modules/micronaut-configuration/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">
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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung.micronaut</groupId>
<artifactId>micronaut-configuration</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.baeldung.micronaut.environments;

import io.micronaut.context.env.Environment;
import io.micronaut.runtime.Micronaut;

public class ServerApplication {

public static void main(String[] args) {
Micronaut.build(args)
.defaultEnvironments(Environment.DEVELOPMENT)
.mainClass(ServerApplication.class)
.start();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.baeldung.micronaut.environments.services;

import java.util.Set;

import io.micronaut.context.ApplicationContext;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;

@Singleton
public class HostResolver {

@Inject
ApplicationContext applicationContext;

public String getHost() {
// avoid this messy code by using property files
Set<String> activeEnvironments = applicationContext.getEnvironment()
.getActiveNames();

if (activeEnvironments.contains("dev") || activeEnvironments.contains("local")) {
return "localhost";
} else if (activeEnvironments.contains("production")) {
return "my-service.us-west-2.amazonaws.com";
} else {
throw new RuntimeException("Unsupported environment: " + activeEnvironments);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.baeldung.micronaut.environments.services.eventsourcing;

public interface EventSourcingService {

// this method would be void, but for the purpose of our tutorial,
// we have it returning something, so that we can easily test it.
String sendEvent(String event);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.baeldung.micronaut.environments.services.eventsourcing;

import io.micronaut.context.annotation.Requires;
import jakarta.inject.Singleton;

@Singleton
@Requires(env = "production")
public class KafkaEventSourcingService implements EventSourcingService {

@Override
public String sendEvent(String event) {
return "using kafka to send message: [" + event + "]";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.baeldung.micronaut.environments.services.eventsourcing;

import io.micronaut.context.annotation.Requires;
import io.micronaut.context.env.Environment;
import jakarta.inject.Singleton;

@Singleton
@Requires(env = Environment.DEVELOPMENT)
public class VoidEventSourcingService implements EventSourcingService {

@Override
public String sendEvent(String event) {
return "void service. [" + event + "] was not sent";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.baeldung.micronaut.environments.services.logging;

import jakarta.inject.Singleton;

@Singleton
public class ConsoleLoggingServiceImpl implements LoggingService {

@Override
public String log(String message) {
return "logging to console: [" + message + "]";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.baeldung.micronaut.environments.services.logging;

import io.micronaut.context.annotation.Replaces;
import io.micronaut.context.annotation.Requires;
import jakarta.inject.Singleton;

@Singleton
@Requires(env = { "production", "canary-production" })
@Replaces(LoggingService.class)
public class FileLoggingServiceImpl implements LoggingService {

@Override
public String log(String message) {
return "logging to some file: [" + message + "]";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.baeldung.micronaut.environments.services.logging;

public interface LoggingService {

// this method would be void, but for the purpose of our tutorial,
// we have it returning something, so that we can easily test it.
String log(String message);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
service:
test:
property: 'something-in-dev'
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.baeldung.micronaut.environments.services.eventsourcing;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

import io.micronaut.context.ApplicationContext;
import io.micronaut.context.annotation.Property;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import jakarta.inject.Inject;

@MicronautTest(environments = { "dev" })
public class DevEnvironmentEventSourcingUnitTest {

@Inject
EventSourcingService eventSourcingService;
@Inject
ApplicationContext applicationContext;
@Property(name = "service.test.property")
String testProperty;

@Test
public void whenEnvironmentIsSetToDev_thenActiveEnvironmentsAreTestAndDev() {
assertThat(applicationContext.getEnvironment()
.getActiveNames()).containsExactlyInAnyOrder("test", "dev");
}

@Test
public void whenEnvironmentIsSetToDev_thenTestPropertyGetsValueFromDev() {
assertThat(testProperty).isEqualTo("something-in-dev");
}

@Test
public void givenEnvironmentIsSetToDev_whenSendEvent_thenVoidServiceIsUsed() {
String devEvent = eventSourcingService.sendEvent("something");

assertThat(devEvent).isEqualTo("void service. [something] was not sent");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.baeldung.micronaut.environments.services.eventsourcing;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import org.junit.jupiter.api.Test;

import com.baeldung.micronaut.environments.ServerApplication;

import io.micronaut.context.ApplicationContext;
import io.micronaut.context.exceptions.NoSuchBeanException;
import io.micronaut.runtime.Micronaut;

public class InvalidEnvironmentEventSourcingUnitTest {

@Test
public void whenEnvironmentIsNotSet_thenEventSourcingServiceBeanIsNotCreated() {
ApplicationContext applicationContext = Micronaut.run(ServerApplication.class);
applicationContext.start();

assertThat(applicationContext.getEnvironment()
.getActiveNames()).containsExactly("test");
assertThatThrownBy(() -> applicationContext.getBean(EventSourcingService.class)).isInstanceOf(NoSuchBeanException.class)
.hasMessageContaining("None of the required environments [production] are active: [test]");
}

@Test
public void whenEnvironmentIsNotSet_thenTestPropertyGetsValueFromDeductedEnvironment() {
ApplicationContext applicationContext = Micronaut.run(ServerApplication.class);
applicationContext.start();

assertThat(applicationContext.getEnvironment()
.getActiveNames()).containsExactly("test");
assertThat(applicationContext.getProperty("service.test.property", String.class)).isNotEmpty();
assertThat(applicationContext.getProperty("service.test.property", String.class)
.get()).isEqualTo("something-in-test");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.baeldung.micronaut.environments.services.eventsourcing;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import org.junit.jupiter.api.Test;

import io.micronaut.context.ApplicationContext;
import io.micronaut.context.exceptions.NonUniqueBeanException;

public class MultipleEnvironmentsEventSourcingUnitTest {

@Test
public void whenEnvironmentIsSetToBothProductionAndDev_thenEventSourcingServiceBeanHasConflict() {
ApplicationContext applicationContext = ApplicationContext.builder("dev", "production")
.build();
applicationContext.start();

assertThat(applicationContext.getEnvironment()
.getActiveNames()).containsExactly("test", "dev", "production");
assertThatThrownBy(() -> applicationContext.getBean(EventSourcingService.class)).isInstanceOf(NonUniqueBeanException.class)
.hasMessageStartingWith("Multiple possible bean candidates found: [");
}

@Test
public void whenEnvironmentIsSetToBothProductionAndDev_thenTestPropertyGetsValueBasedOnPriority() {
ApplicationContext applicationContext = ApplicationContext.builder("dev", "production")
.build();
applicationContext.start();

assertThat(applicationContext.getEnvironment()
.getActiveNames()).containsExactly("test", "dev", "production");
assertThat(applicationContext.getProperty("service.test.property", String.class)).isNotEmpty();
assertThat(applicationContext.getProperty("service.test.property", String.class)
.get()).isEqualTo("something-in-dev");
}

@Test
public void whenEnvironmentIsSetToBothProductionAndDev_thenMissingPropertyIsEmpty() {
ApplicationContext applicationContext = ApplicationContext.builder("dev", "production")
.build();
applicationContext.start();

assertThat(applicationContext.getEnvironment()
.getActiveNames()).containsExactly("test", "dev", "production");
assertThat(applicationContext.getProperty("service.dummy.property", String.class)).isEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.baeldung.micronaut.environments.services.eventsourcing;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

import io.micronaut.context.ApplicationContext;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import jakarta.inject.Inject;

@MicronautTest(environments = { "production" })
public class ProductionEnvironmentEventSourcingUnitTest {

@Inject
EventSourcingService eventSourcingService;
@Inject
ApplicationContext applicationContext;

@Test
public void whenEnvironmentIsSetToProduction_thenActiveEnvironmentsAreTestAndProduction() {
assertThat(applicationContext.getEnvironment()
.getActiveNames()).containsExactlyInAnyOrder("test", "production");
}

@Test
public void givenEnvironmentIsSetToProduction_whenSendEvent_thenKafkaServiceIsUsed() {
String devEvent = eventSourcingService.sendEvent("something");

assertThat(devEvent).isEqualTo("using kafka to send message: [something]");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.baeldung.micronaut.environments.services.hostresolver;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

import com.baeldung.micronaut.environments.services.HostResolver;

import io.micronaut.context.ApplicationContext;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import jakarta.inject.Inject;

@MicronautTest(environments = { "dev" })
public class DevEnvironmentHostResolverUnitTest {

@Inject
HostResolver hostResolver;
@Inject
ApplicationContext applicationContext;

@Test
public void whenEnvironmentIsSetToDev_thenActiveEnvironmentsAreTestAndDev() {
assertThat(applicationContext.getEnvironment()
.getActiveNames()).containsExactlyInAnyOrder("test", "dev");
}

@Test
public void givenEnvironmentIsSetToDev_whenGetHost_thenTheResolverReturnsLocalhost() {
String devHost = hostResolver.getHost();

assertThat(devHost).isEqualTo("localhost");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.baeldung.micronaut.environments.services.hostresolver;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import org.junit.jupiter.api.Test;

import com.baeldung.micronaut.environments.services.HostResolver;

import io.micronaut.context.ApplicationContext;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import jakarta.inject.Inject;

@MicronautTest(environments = { "no-mans-land" })
public class InvalidEnvironmentHostResolverUnitTest {

@Inject
HostResolver hostResolver;
@Inject
ApplicationContext applicationContext;

@Test
public void whenEnvironmentIsSetToIrrelevant_thenActiveEnvironmentsAreTheExpectedOnes() {
assertThat(applicationContext.getEnvironment()
.getActiveNames()).containsExactlyInAnyOrder("test", "no-mans-land");
}

@Test
public void givenEnvironmentIsSetToIrrelevant_whenGetHost_throwsUnsupportedEnvironmentException() {
assertThatThrownBy(() -> hostResolver.getHost()).isInstanceOf(RuntimeException.class)
.hasMessageContaining("Unsupported environment: ");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.baeldung.micronaut.environments.services.hostresolver;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

import com.baeldung.micronaut.environments.services.HostResolver;

import io.micronaut.context.ApplicationContext;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import jakarta.inject.Inject;

@MicronautTest(environments = { "local" })
public class LocalEnvironmentHostResolverUnitTest {

@Inject
HostResolver hostResolver;
@Inject
ApplicationContext applicationContext;

@Test
public void whenEnvironmentIsSetToLocal_thenActiveEnvironmentsAreTestAndLocal() {
assertThat(applicationContext.getEnvironment()
.getActiveNames()).containsExactlyInAnyOrder("test", "local");
}

@Test
public void givenEnvironmentIsSetToLocal_whenGetHost_thenTheResolverReturnsLocalhost() {
String localHost = hostResolver.getHost();

assertThat(localHost).isEqualTo("localhost");
}
}
Loading