Skip to content

Commit

Permalink
Merge pull request #14 from it-at-m/MDAS-933_Refactoring_EAI-Komponenten
Browse files Browse the repository at this point in the history
Mdas 933 refactoring eai komponenten
  • Loading branch information
sfi2022 authored Nov 27, 2024
2 parents 1450c7b + c155936 commit c2e74be
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 42 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/build-common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ on:
branches-ignore:
- 'sprint'
- 'main'
paths:
- '${{ github.workspace }}/**'
- '.github/workflows/**'
pull_request:
types: [ opened, reopened ]

Expand Down
4 changes: 4 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Release-Notes
## Sprint 20 (12.11.2024 - 03.12.2024)
- Maven groupid an LHM Standard angepasst.
- S3CredentialProvider und EnvironmentReader als Bean.

## Sprint 18 (1.10.2024 - 22.10.2024)
### Hinzugefügt
- S3 Bucket Authorisierung.
Expand Down
6 changes: 3 additions & 3 deletions mobidam-eai-commons-lib/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>de.muenchen.mobidam</groupId>
<groupId>de.muenchen.oss.mobidam</groupId>
<artifactId>mobidam-eai-commons</artifactId>
<version>0.0.2-SNAPSHOT</version>
</parent>
Expand Down Expand Up @@ -72,10 +72,10 @@
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
*/
package de.muenchen.mobidam.eai.common.config;

import lombok.AllArgsConstructor;

@AllArgsConstructor
public class EnvironmentReader {

public static String getEnvironmentVariable(final String key) {
public String getEnvironmentVariable(final String key) {
return System.getenv().get(key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,14 @@ public class S3CredentialProvider implements Processor {

@NonNull
private final S3BucketCredentialConfig properties;
private final EnvironmentReader environmentReader;

@Override
public void process(Exchange exchange) throws Exception {
String bucketName = verifyBucket(exchange);
S3BucketCredentialConfig.BucketCredentialConfig credentials = verifyCredentials(bucketName, exchange);
String accessKey = EnvironmentReader.getEnvironmentVariable(credentials.getAccessKeyEnvVar());
String secretKey = EnvironmentReader.getEnvironmentVariable(credentials.getSecretKeyEnvVar());
String accessKey = environmentReader.getEnvironmentVariable(credentials.getAccessKeyEnvVar());
String secretKey = environmentReader.getEnvironmentVariable(credentials.getSecretKeyEnvVar());
if (Strings.isNullOrEmpty(accessKey) || Strings.isNullOrEmpty(secretKey)) {
exchange.getMessage()
.setBody(ErrorResponseBuilder.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Bucket not configured: " + bucketName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,47 +27,49 @@
import de.muenchen.mobidam.eai.common.config.S3BucketCredentialConfig;
import de.muenchen.mobidam.eai.common.exception.MobidamException;
import de.muenchen.mobidam.eai.common.s3.S3CredentialProvider;
import java.util.HashMap;
import java.util.Map;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.support.DefaultExchange;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;

import java.util.HashMap;
import java.util.Map;

public class S3CredentialProviderTest {

private EnvironmentReader environmentReader;
private S3BucketCredentialConfig s3CredentialProperties;
private S3CredentialProvider s3CredentialProvider;
private final CamelContext camelContext = new DefaultCamelContext();

@BeforeEach
void setup() {
environmentReader = Mockito.mock(EnvironmentReader.class);
s3CredentialProperties = Mockito.mock(S3BucketCredentialConfig.class);
s3CredentialProvider = new S3CredentialProvider(s3CredentialProperties, environmentReader);
}

@Test
public void test_processWithValidConfiguration() throws Exception {
// Given
String bucketName = "x-itmkm82k";
String envVar = "FOO";
String value = "BAR";

try (MockedStatic<EnvironmentReader> environmentReader = Mockito.mockStatic(EnvironmentReader.class)) {

// Given
String bucketName = "x-itmkm82k";
String envVar = "FOO";
String value = "BAR";

configureEnvironment(bucketName, envVar, value);

Exchange exchange = new DefaultExchange(camelContext);
exchange.getMessage().setHeader(CommonConstants.HEADER_BUCKET_NAME, bucketName);
configureEnvironment(bucketName, envVar, value);

environmentReader.when(() -> EnvironmentReader.getEnvironmentVariable(envVar)).thenReturn(value);
Exchange exchange = new DefaultExchange(camelContext);
exchange.getMessage().setHeader(CommonConstants.HEADER_BUCKET_NAME, bucketName);

// When
s3CredentialProvider.process(exchange);
// When
s3CredentialProvider.process(exchange);

// Then
Assertions.assertEquals(value, exchange.getMessage().getHeader(CommonConstants.HEADER_ACCESS_KEY));
Assertions.assertEquals(value, exchange.getMessage().getHeader(CommonConstants.HEADER_SECRET_KEY));
}
// Then
Assertions.assertEquals(value, exchange.getMessage().getHeader(CommonConstants.HEADER_ACCESS_KEY));
Assertions.assertEquals(value, exchange.getMessage().getHeader(CommonConstants.HEADER_SECRET_KEY));
}

@Test
Expand All @@ -77,7 +79,8 @@ public void test_processWithMissingEnvVars() {
String envVar = "FOO";
String value = "BAR";

configureEnvironment(bucketName, envVar, value);
Mockito.when(environmentReader.getEnvironmentVariable(Mockito.anyString())).thenReturn(null);

Exchange exchange = new DefaultExchange(camelContext);
exchange.getMessage().setHeader(CommonConstants.HEADER_BUCKET_NAME, bucketName);

Expand All @@ -96,6 +99,7 @@ public void test_processWithMissingBucketConfiguration() {
String value = "BAR";

configureEnvironment(bucketName, envVar, value);

Exchange exchange = new DefaultExchange(camelContext);
exchange.getMessage().setHeader(CommonConstants.HEADER_BUCKET_NAME, "invalid_bucket");

Expand All @@ -114,6 +118,7 @@ public void test_processWithMissingBucketName() {
String value = "BAR";

configureEnvironment(bucketName, envVar, value);

Exchange exchange = new DefaultExchange(camelContext);

// Then
Expand All @@ -124,14 +129,12 @@ public void test_processWithMissingBucketName() {
}

private void configureEnvironment(String bucketName, String envVar, String value) {
S3BucketCredentialConfig properties = Mockito.mock(S3BucketCredentialConfig.class);
s3CredentialProvider = new S3CredentialProvider(properties);

Mockito.when(environmentReader.getEnvironmentVariable(envVar)).thenReturn(value);
S3BucketCredentialConfig.BucketCredentialConfig envVars = new S3BucketCredentialConfig.BucketCredentialConfig();
envVars.setAccessKeyEnvVar(envVar);
envVars.setSecretKeyEnvVar(envVar);
Map<String, S3BucketCredentialConfig.BucketCredentialConfig> map = new HashMap<>();
map.put(bucketName, envVars);
Mockito.when(properties.getBucketCredentialConfigs()).thenReturn(map);
Mockito.when(s3CredentialProperties.getBucketCredentialConfigs()).thenReturn(map);
}
}
4 changes: 2 additions & 2 deletions mobidam-eai-commons-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>de.muenchen.mobidam</groupId>
<groupId>de.muenchen.oss.mobidam</groupId>
<artifactId>mobidam-eai-commons</artifactId>
<version>0.0.2-SNAPSHOT</version>
</parent>
Expand All @@ -52,7 +52,7 @@
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>de.muenchen.mobidam</groupId>
<groupId>de.muenchen.oss.mobidam</groupId>
<artifactId>mobidam-eai-commons-lib</artifactId>
<version>${project.version}</version>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/
package de.muenchen.mobidam.eai.integration.configuration;

import de.muenchen.mobidam.eai.common.config.EnvironmentReader;
import de.muenchen.mobidam.eai.common.config.S3BucketCredentialConfig;
import de.muenchen.mobidam.eai.common.s3.S3CredentialProvider;
import org.apache.camel.component.aws2.s3.AWS2S3Component;
Expand All @@ -41,10 +42,16 @@ public S3BucketCredentialConfig s3BucketCredentialConfig() {
return new S3BucketCredentialConfig();
}

@Bean
@ConditionalOnMissingBean
public EnvironmentReader environmentReader() {
return new EnvironmentReader();
}

@Bean
@ConditionalOnClass(AWS2S3Component.class)
@ConditionalOnMissingBean
public S3CredentialProvider s3CredentialProvider(S3BucketCredentialConfig properties) {
return new S3CredentialProvider(properties);
public S3CredentialProvider s3CredentialProvider(S3BucketCredentialConfig properties, EnvironmentReader environmentReader) {
return new S3CredentialProvider(properties, environmentReader);
}
}
14 changes: 12 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<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">
<modelVersion>4.0.0</modelVersion>

<groupId>de.muenchen.mobidam</groupId>
<groupId>de.muenchen.oss.mobidam</groupId>
<artifactId>mobidam-eai-commons</artifactId>
<version>0.0.2-SNAPSHOT</version>

Expand All @@ -51,16 +51,18 @@
<name>sfi2022</name>
<organization>Landeshauptstadt München</organization>
<url>https://github.com/sfi2022</url>
<roles><role>mobidam_devs</role></roles>
</developer>
</developers>

<scm>
<url>${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}</url>
<connection>scm:git:${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}.git</connection>
<developerConnection>scm:git:${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}.git</developerConnection>
<tag>head</tag>
<tag>head</tag>
</scm>


<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
Expand All @@ -70,6 +72,8 @@

<spring.boot.version>3.3.4</spring.boot.version>

<maven-surefire-plugin.version>3.0.0-M5</maven-surefire-plugin.version>

<skipGpg>false</skipGpg>
</properties>

Expand Down Expand Up @@ -277,6 +281,7 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
<configuration>
<excludes>
<exclude>
Expand All @@ -286,6 +291,11 @@
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
</plugin>
</plugins>
</build>

Expand Down

0 comments on commit c2e74be

Please sign in to comment.