-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Merge pull request #224 from pagopa/release-dev
chore: Release dev
- Loading branch information
Showing
18 changed files
with
315 additions
and
34 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
# see https://help.github.com/en/articles/about-code-owners#example-of-a-codeowners-file | ||
|
||
* @pagopa/idpay-app-maintainer-team @dariopelliccioli @antonioT90 @GiovanaSolorzano @pelliccm | ||
* @pagopa/idpay-app-maintainer-team @dariopelliccioli @antonioT90 @frtrinca @Benedetta-fabbri @FrancescoGraziano @stedelia |
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
7 changes: 2 additions & 5 deletions
7
...sibility/connector/repository/CustomSequenceGeneratorOpsRepositoryImplTestIntegrated.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 |
---|---|---|
@@ -1,14 +1,11 @@ | ||
package it.gov.pagopa.admissibility.connector.repository; | ||
|
||
import org.springframework.test.context.TestPropertySource; | ||
import it.gov.pagopa.common.mongo.MongoTestIntegrated; | ||
|
||
/** | ||
* See confluence page: <a href="https://pagopa.atlassian.net/wiki/spaces/IDPAY/pages/615974424/Secrets+UnitTests">Secrets for UnitTests</a> | ||
*/ | ||
@SuppressWarnings({"squid:S3577", "NewClassNamingConvention"}) // suppressing class name not match alert: we are not using the Test suffix in order to let not execute this test by default maven configuration because it depends on properties not pushable. See | ||
@TestPropertySource(locations = { | ||
"classpath:/mongodbEmbeddedDisabled.properties", | ||
"classpath:/secrets/mongodbConnectionString.properties" | ||
}) | ||
@MongoTestIntegrated | ||
public class CustomSequenceGeneratorOpsRepositoryImplTestIntegrated extends CustomSequenceGeneratorOpsRepositoryImplTest{ | ||
} |
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
5 changes: 5 additions & 0 deletions
5
src/test/java/it/gov/pagopa/common/mongo/EmbeddedMongodbTestClient.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,5 @@ | ||
package it.gov.pagopa.common.mongo; | ||
|
||
public interface EmbeddedMongodbTestClient { | ||
void dropDatabase(); | ||
} |
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
16 changes: 16 additions & 0 deletions
16
...st/java/it/gov/pagopa/common/mongo/singleinstance/AutoConfigureSingleInstanceMongodb.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,16 @@ | ||
package it.gov.pagopa.common.mongo.singleinstance; | ||
|
||
import de.flapdoodle.embed.mongo.spring.autoconfigure.EmbeddedMongoAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration; | ||
import org.springframework.boot.test.autoconfigure.data.mongo.AutoConfigureDataMongo; | ||
|
||
import java.lang.annotation.*; | ||
|
||
/** It will enable the usage of a single instance of {@link EmbeddedMongoAutoConfiguration}, dropping the database at each new Spring Context */ | ||
@Target({ElementType.TYPE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
@AutoConfigureDataMongo | ||
@ImportAutoConfiguration(exclude = EmbeddedMongoAutoConfiguration.class) | ||
public @interface AutoConfigureSingleInstanceMongodb { | ||
} |
149 changes: 149 additions & 0 deletions
149
...ava/it/gov/pagopa/common/mongo/singleinstance/SingleEmbeddedMongodbAutoConfiguration.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,149 @@ | ||
package it.gov.pagopa.common.mongo.singleinstance; | ||
|
||
import de.flapdoodle.embed.mongo.commands.MongodArguments; | ||
import de.flapdoodle.embed.mongo.config.Net; | ||
import de.flapdoodle.embed.mongo.distribution.IFeatureAwareVersion; | ||
import de.flapdoodle.embed.mongo.spring.autoconfigure.*; | ||
import de.flapdoodle.embed.mongo.transitions.Mongod; | ||
import it.gov.pagopa.common.mongo.EmbeddedMongodbTestClient; | ||
import org.springframework.boot.autoconfigure.AutoConfiguration; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.mongo.MongoProperties; | ||
import org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration; | ||
import org.springframework.context.ConfigurableApplicationContext; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.core.env.ConfigurableEnvironment; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.core.env.MapPropertySource; | ||
import org.springframework.test.context.event.annotation.AfterTestClass; | ||
|
||
import java.io.IOException; | ||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Used to start just once Mongodb instance for the entire duration of the test, dropping the database at each new Spring Context | ||
*/ | ||
@AutoConfiguration( | ||
before = {MongoAutoConfiguration.class, MongoReactiveAutoConfiguration.class, EmbeddedMongoAutoConfiguration.class} | ||
) | ||
public class SingleEmbeddedMongodbAutoConfiguration extends EmbeddedMongoAutoConfiguration { | ||
|
||
private static MongodWrapper singleMongodWrapperInstance; | ||
|
||
private final EmbeddedMongodbTestClient embeddedMongodbTestClient; | ||
|
||
public SingleEmbeddedMongodbAutoConfiguration(EmbeddedMongodbTestClient embeddedMongodbTestClient) { | ||
this.embeddedMongodbTestClient = embeddedMongodbTestClient; | ||
} | ||
|
||
@Bean | ||
@Override | ||
public Net net(ConfigurableApplicationContext context) throws IOException { | ||
if(SingleInstanceMongodWrapper.singleMongodNet!=null){ | ||
ConfigurableEnvironment env = context.getEnvironment(); | ||
env.getPropertySources().addFirst(new MapPropertySource("embeddedMongoReusedProperties", | ||
Map.of("spring.data.mongodb.port", SingleInstanceMongodWrapper.singleMongodNet.getPort()))); | ||
super.net(context); | ||
|
||
return SingleInstanceMongodWrapper.singleMongodNet; | ||
}else { | ||
return SingleInstanceMongodWrapper.singleMongodNet = super.net(context); | ||
} | ||
} | ||
|
||
@AfterTestClass | ||
void clearData(){ | ||
embeddedMongodbTestClient.dropDatabase(); | ||
} | ||
|
||
@ConditionalOnClass(name = { | ||
"com.mongodb.reactivestreams.client.MongoClient", | ||
"org.springframework.data.mongodb.core.ReactiveMongoClientFactoryBean" | ||
}) | ||
static class ReactiveClientServerWrapperConfig { | ||
|
||
private final Constructor<ReactiveClientServerFactory> unprotectedReactiveClientServerFactoryConstructor; | ||
|
||
ReactiveClientServerWrapperConfig() { | ||
try { | ||
unprotectedReactiveClientServerFactoryConstructor = ReactiveClientServerFactory.class.getDeclaredConstructor(MongoProperties.class); | ||
unprotectedReactiveClientServerFactoryConstructor.setAccessible(true); | ||
} catch (NoSuchMethodException e) { | ||
throw new IllegalStateException("Cannot unprotect AbstractServerFactory constructor", e); | ||
} | ||
} | ||
|
||
@Bean( | ||
initMethod = "start", | ||
destroyMethod = "stop" | ||
) | ||
@ConditionalOnMissingBean | ||
public MongodWrapper reactiveClientServerWrapper(IFeatureAwareVersion version, MongoProperties properties, Mongod mongod, MongodArguments mongodArguments) { | ||
return Objects.requireNonNullElseGet( | ||
singleMongodWrapperInstance, | ||
() -> createMongodWrapper(unprotectedReactiveClientServerFactoryConstructor, version, properties, mongod, mongodArguments)); | ||
} | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
public EmbeddedMongodbTestClient embeddedMongodbTestClient(Environment env) { | ||
return createEmbeddedMongodbTestClient("it.gov.pagopa.common.reactive.mongo.EmbeddedMongodbTestReactiveClient", env); | ||
} | ||
} | ||
|
||
@ConditionalOnClass(name = { | ||
"com.mongodb.client.MongoClient", | ||
"org.springframework.data.mongodb.core.MongoClientFactoryBean" | ||
}) | ||
static class SyncClientServerWrapperConfig { | ||
|
||
private final Constructor<SyncClientServerFactory> unprotectedSyncClientServerFactoryConstructor; | ||
|
||
SyncClientServerWrapperConfig() { | ||
try { | ||
unprotectedSyncClientServerFactoryConstructor = SyncClientServerFactory.class.getDeclaredConstructor(MongoProperties.class); | ||
unprotectedSyncClientServerFactoryConstructor.setAccessible(true); | ||
} catch (NoSuchMethodException e) { | ||
throw new IllegalStateException("Cannot unprotect AbstractServerFactory constructor", e); | ||
} | ||
} | ||
|
||
@Bean( | ||
initMethod = "start", | ||
destroyMethod = "stop" | ||
) | ||
@ConditionalOnMissingBean | ||
public MongodWrapper syncClientServerWrapper(IFeatureAwareVersion version, MongoProperties properties, Mongod mongod, MongodArguments mongodArguments) { | ||
return Objects.requireNonNullElseGet( | ||
singleMongodWrapperInstance, | ||
() -> createMongodWrapper(unprotectedSyncClientServerFactoryConstructor, version, properties, mongod, mongodArguments)); | ||
} | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
public EmbeddedMongodbTestClient embeddedMongodbTestClient(Environment env) { | ||
return createEmbeddedMongodbTestClient("it.gov.pagopa.common.mongo.EmbeddedMongodbTestSyncClient", env); | ||
} | ||
} | ||
|
||
private static MongodWrapper createMongodWrapper(Constructor<? extends AbstractServerFactory<?>> unprotectedSyncClientServerFactoryConstructor, IFeatureAwareVersion version, MongoProperties properties, Mongod mongod, MongodArguments mongodArguments) { | ||
try { | ||
return singleMongodWrapperInstance = new SingleInstanceMongodWrapper(unprotectedSyncClientServerFactoryConstructor.newInstance(properties).createWrapper(version, mongod, mongodArguments)); | ||
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) { | ||
throw new IllegalStateException("Cannot call protected constructor", e); | ||
} | ||
} | ||
|
||
private static EmbeddedMongodbTestClient createEmbeddedMongodbTestClient(String embeddedMongodbTestClientClassName, Environment env) { | ||
try { | ||
return (EmbeddedMongodbTestClient) Class.forName(embeddedMongodbTestClientClassName).getConstructor(Environment.class).newInstance(env); | ||
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException | InvocationTargetException | NoSuchMethodException e) { | ||
throw new IllegalStateException("Cannot create EmbeddedMongodbTestClient", e); | ||
} | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/test/java/it/gov/pagopa/common/mongo/singleinstance/SingleInstanceMongodWrapper.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,56 @@ | ||
package it.gov.pagopa.common.mongo.singleinstance; | ||
|
||
import de.flapdoodle.embed.mongo.config.Net; | ||
import de.flapdoodle.embed.mongo.spring.autoconfigure.MongodWrapper; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
public class SingleInstanceMongodWrapper extends MongodWrapper { | ||
|
||
private final Runnable unprotectedStart; | ||
private final Runnable unprotectedStop; | ||
|
||
private final AtomicInteger counter = new AtomicInteger(0); | ||
public static Net singleMongodNet; | ||
|
||
public SingleInstanceMongodWrapper(MongodWrapper mongodWrapper) { | ||
super(null); | ||
|
||
unprotectedStart = unprotectMongodWrapperMethod("start", mongodWrapper); | ||
unprotectedStop = unprotectMongodWrapperMethod("stop", mongodWrapper); | ||
} | ||
|
||
private Runnable unprotectMongodWrapperMethod(String methodName, MongodWrapper mongodWrapper) { | ||
try { | ||
Method method = MongodWrapper.class.getDeclaredMethod(methodName); | ||
method.setAccessible(true); | ||
return () -> { | ||
try { | ||
method.invoke(mongodWrapper); | ||
} catch (IllegalAccessException | InvocationTargetException e) { | ||
throw new IllegalStateException("Cannot invoke protected %s mongodWrapper method".formatted(methodName), e); | ||
} | ||
}; | ||
} catch (NoSuchMethodException e) { | ||
throw new IllegalStateException("Cannot unprotect mongodWrapper methods", e); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unused") // called by Spring | ||
private void start() { | ||
synchronized (counter) { | ||
if (counter.getAndIncrement() == 0) { | ||
unprotectedStart.run(); | ||
} | ||
} | ||
} | ||
|
||
@SuppressWarnings("unused") // called by Spring | ||
private void stop() { | ||
if (counter.decrementAndGet() == 0) { | ||
unprotectedStop.run(); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.