From 59a809cb4e8a1a6f9d872c69776f02c771a081e5 Mon Sep 17 00:00:00 2001 From: Andrej Vano Date: Mon, 24 Apr 2023 12:30:28 +0200 Subject: [PATCH] [closes #433] Replace Service interface with an abstract class --- README.md | 4 +- .../software/tnb/common/account/Account.java | 5 +- .../tnb/common/account/NoAccount.java | 10 +++ .../software/tnb/common/client/NoClient.java | 4 + .../common/service/ConfigurableService.java | 25 ++---- .../tnb/common/service/Restartable.java | 5 -- .../software/tnb/common/service/Service.java | 26 +++++- .../tnb/common/service/ServiceFactory.java | 16 ++-- .../tnb/common/service/Validation.java | 4 - .../tnb/common/util/ReflectionUtil.java | 31 +++++++ .../tnb/common/validation/NoValidation.java | 4 + .../tnb/common/validation/Validation.java | 4 + .../aws/cloudwatch/service/Cloudwatch.java | 2 +- .../validation/CloudwatchValidation.java | 2 +- .../tnb/aws/common/service/AWSService.java | 23 +++-- .../tnb/aws/common/service/LocalStack.java | 6 +- .../tnb/aws/dynamodb/service/DynamoDB.java | 2 +- .../validation/DynamoDBValidation.java | 2 +- .../software/tnb/aws/iam/service/IAM.java | 7 +- .../tnb/aws/iam/validation/IAMValidation.java | 2 +- .../aws/firehose/service/KinesisFirehose.java | 2 +- .../validation/KinesisFirehoseValidation.java | 4 +- .../tnb/aws/kinesis/service/Kinesis.java | 2 +- .../kinesis/validation/KinesisValidation.java | 2 +- .../tnb/aws/lambda/service/Lambda.java | 2 +- .../lambda/validation/LambdaValidation.java | 4 +- .../tnb/aws/redshift/service/Redshift.java | 11 +-- .../validation/RedshiftValidation.java | 2 +- .../software/tnb/aws/s3/service/Minio.java | 2 +- .../java/software/tnb/aws/s3/service/S3.java | 2 +- .../tnb/aws/s3/service/local/LocalMinio.java | 4 +- .../s3/service/openshift/OpenshiftMinio.java | 3 +- .../tnb/aws/s3/validation/S3Validation.java | 2 +- .../software/tnb/aws/ses/service/SES.java | 12 +-- .../tnb/aws/ses/validation/SESValidation.java | 2 +- .../software/tnb/aws/sns/service/SNS.java | 2 +- .../tnb/aws/sns/validation/SNSValidation.java | 2 +- .../software/tnb/aws/sqs/service/SQS.java | 2 +- .../tnb/aws/sqs/validation/SQSValidation.java | 2 +- .../event/hubs/client/EventHubClients.java | 49 +++++++++++ .../azure/event/hubs/service/EventHubs.java | 64 +++----------- .../hubs/validation/EventHubsValidation.java | 21 +++-- .../bus/account/ServiceBusAccount.java} | 4 +- .../azure/service/bus/service/ServiceBus.java | 25 ++---- .../bus/validation/ServiceBusValidation.java | 19 +++-- .../storage/blob/service/StorageBlob.java | 17 +--- .../validation/StorageBlobValidation.java | 4 +- .../storage/queue/service/StorageQueue.java | 17 +--- .../validation/StorageQueueValidation.java | 4 +- .../tnb/cryostat/service/Cryostat.java | 6 +- .../validation/CryostatValidation.java | 5 +- .../resource/local/LocalCassandra.java | 2 +- .../openshift/OpenshiftCassandra.java | 2 +- .../tnb/db/cassandra/service/Cassandra.java | 9 +- .../validation/CassandraValidation.java | 4 +- .../software/tnb/db/common/service/SQL.java | 8 +- .../db/common/validation/SQLValidation.java | 3 +- .../DbAllocatorConfiguration.java | 2 +- .../db/dballocator/service/DbAllocator.java | 20 ++--- .../dballocator/validation/SqlValidation.java | 4 +- .../mongodb/resource/local/LocalMongoDB.java | 6 -- .../resource/openshift/OpenshiftMongoDB.java | 6 -- .../tnb/db/mongodb/service/MongoDB.java | 16 +--- .../mongodb/validation/MongoDBValidation.java | 3 +- .../elasticsearch/service/Elasticsearch.java | 6 +- .../validation/ElasticsearchValidation.java | 4 +- .../java/software/tnb/fhir/service/Fhir.java | 5 +- .../tnb/filesystem/service/FileSystem.java | 5 +- .../tnb/ftp/common/FileTransferService.java | 4 +- .../ftp/common/FileTransferValidation.java | 4 +- .../software/tnb/ftp/ftp/service/FTP.java | 18 +--- .../software/tnb/ftp/sftp/service/SFTP.java | 17 +--- .../api/common/service/GoogleAPIService.java | 19 +---- .../tnb/google/mail/service/GoogleMail.java | 9 +- .../mail/validation/GoogleMailValidation.java | 6 +- .../google/sheets/service/GoogleSheets.java | 9 +- .../validation/GoogleSheetsValidation.java | 2 +- .../bigquery/service/GoogleBigQuery.java | 27 +----- .../validation/BigQueryValidation.java | 4 +- .../functions/service/GoogleFunctions.java | 34 +++----- .../validation/GoogleFunctionsValidation.java | 2 +- .../cloud/pubsub/client/PubSubClients.java | 83 +++++++++++++++++++ .../cloud/pubsub/service/GooglePubSub.java | 72 +++------------- .../pubsub/validation/PubSubValidation.java | 34 ++++---- .../google/storage/service/GoogleStorage.java | 18 +--- .../validation/GoogleStorageValidation.java | 3 +- .../software/tnb/horreum/service/Horreum.java | 13 +-- .../horreum/validation/HorreumValidation.java | 3 +- .../java/software/tnb/http/service/HTTP.java | 5 +- .../openshift/OpenshiftHyperfoil.java | 2 +- .../tnb/hyperfoil/service/Hyperfoil.java | 6 +- .../validation/HyperfoilValidation.java | 3 +- .../account/InfinispanAccount.java | 2 +- .../tnb/infinispan/service/Infinispan.java | 6 +- .../jaeger/resource/local/LocalJaeger.java | 6 +- .../resource/openshift/OpenshiftJaeger.java | 6 +- .../software/tnb/jaeger/service/Jaeger.java | 9 +- .../jaeger/validation/JaegerValidation.java | 3 +- .../java/software/tnb/jira/service/Jira.java | 21 +---- .../tnb/jira/validation/JiraValidation.java | 3 +- .../amq/resource/local/LocalAMQBroker.java | 4 +- .../openshift/OpenshiftAMQBroker.java | 4 +- .../tnb/jms/amq/service/AMQBroker.java | 17 +--- .../tnb/jms/amq/validation/AMQValidation.java | 3 +- .../tnb/jms/ibm/mq/service/IBMMQ.java | 30 +++---- .../ibm/mq/validation/IBMMQValidation.java | 6 +- .../software/tnb/kafka/service/Kafka.java | 3 +- .../tnb/kafka/validation/KafkaValidation.java | 4 +- .../software/tnb/knative/service/Knative.java | 6 +- .../knative/validation/KnativeValidation.java | 3 +- .../service/LRACoordinator.java | 9 +- .../software/tnb/mail/service/MailServer.java | 4 +- .../tnb/mail/validation/MailValidation.java | 3 +- .../tnb/microsoft/service/Exchange.java | 18 +--- .../software/tnb/microsoft/service/Graph.java | 17 +--- .../microsoft/validation/GraphValidation.java | 4 +- .../software/tnb/mllp/service/MllpServer.java | 5 +- .../PrometheusMetricsConfiguration.java | 2 +- .../metrics/service/PrometheusMetrics.java | 10 +-- .../PrometheusMetricsValidation.java | 5 +- .../tnb/salesforce/service/Salesforce.java | 14 +--- .../validation/SalesforceValidation.java | 3 +- .../tnb/servicenow/service/ServiceNow.java | 14 +--- .../validation/ServiceNowValidation.java | 17 ++-- .../software/tnb/slack/service/Slack.java | 19 +---- .../tnb/slack/validation/SlackValidation.java | 3 +- .../software/tnb/splunk/service/Splunk.java | 6 +- .../splunk/validation/SplunkValidation.java | 3 +- .../tnb/telegram/service/Telegram.java | 21 +---- .../tnb/telegram/service/TelegramBotApi.java | 14 +--- .../validation/TelegramValidation.java | 3 +- .../software/tnb/webhook/service/Webhook.java | 18 +--- .../webhook/validation/WebhookValidation.java | 2 +- 133 files changed, 576 insertions(+), 759 deletions(-) create mode 100644 system-x/common/src/main/java/software/tnb/common/account/NoAccount.java create mode 100644 system-x/common/src/main/java/software/tnb/common/client/NoClient.java delete mode 100644 system-x/common/src/main/java/software/tnb/common/service/Restartable.java delete mode 100644 system-x/common/src/main/java/software/tnb/common/service/Validation.java create mode 100644 system-x/common/src/main/java/software/tnb/common/util/ReflectionUtil.java create mode 100644 system-x/common/src/main/java/software/tnb/common/validation/NoValidation.java create mode 100644 system-x/common/src/main/java/software/tnb/common/validation/Validation.java create mode 100644 system-x/services/azure/event-hubs/src/main/java/software/tnb/azure/event/hubs/client/EventHubClients.java rename system-x/services/azure/{common/src/main/java/software/tnb/azure/common/account/AzureServiceBusAccount.java => service-bus/src/main/java/software/tnb/azure/service/bus/account/ServiceBusAccount.java} (79%) rename system-x/services/db/dballocator/src/main/java/software/tnb/db/dballocator/{service => configuration}/DbAllocatorConfiguration.java (96%) create mode 100644 system-x/services/google/cloud/pubsub/src/main/java/software/tnb/google/cloud/pubsub/client/PubSubClients.java rename system-x/services/infinispan/src/main/java/software/tnb/infinispan/{resource => }/account/InfinispanAccount.java (91%) rename system-x/services/prometheus-metrics/src/main/java/software/tnb/prometheus/metrics/{service => configuration}/PrometheusMetricsConfiguration.java (85%) diff --git a/README.md b/README.md index e578f40ac..7f6ca3076 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,9 @@ using JUnit's `@AfterAll` method). ## Example service -Each service implements a marker interface [Service](./system-x/common/src/main/java/software/tnb/common/service/Service.java). +Each service extends an abstract [Service](./system-x/common/src/main/java/software/tnb/common/service/Service.java) class that provides +the `account`, +`client` and `validation` fields and methods. ### Remote service diff --git a/system-x/common/src/main/java/software/tnb/common/account/Account.java b/system-x/common/src/main/java/software/tnb/common/account/Account.java index 115f7fc57..15d1384fa 100644 --- a/system-x/common/src/main/java/software/tnb/common/account/Account.java +++ b/system-x/common/src/main/java/software/tnb/common/account/Account.java @@ -19,7 +19,10 @@ default Properties toProperties() { for (Field field : getAllFields(new ArrayList<>(), this.getClass())) { try { field.setAccessible(true); - properties.put(StringUtils.replaceUnderscoreWithCamelCase(field.getName()), field.get(this)); + // Null values can't be stored in properties + if (field.get(this) != null) { + properties.put(StringUtils.replaceUnderscoreWithCamelCase(field.getName()), field.get(this)); + } } catch (IllegalAccessException e) { throw new RuntimeException("Unable to get field " + field.getName() + " value: ", e); } diff --git a/system-x/common/src/main/java/software/tnb/common/account/NoAccount.java b/system-x/common/src/main/java/software/tnb/common/account/NoAccount.java new file mode 100644 index 000000000..277ff6317 --- /dev/null +++ b/system-x/common/src/main/java/software/tnb/common/account/NoAccount.java @@ -0,0 +1,10 @@ +package software.tnb.common.account; + +import java.util.Properties; + +public class NoAccount implements Account { + @Override + public Properties toProperties() { + throw new RuntimeException("This account shouldn't be used, as the service doesn't require an account"); + } +} diff --git a/system-x/common/src/main/java/software/tnb/common/client/NoClient.java b/system-x/common/src/main/java/software/tnb/common/client/NoClient.java new file mode 100644 index 000000000..c8e81a2b7 --- /dev/null +++ b/system-x/common/src/main/java/software/tnb/common/client/NoClient.java @@ -0,0 +1,4 @@ +package software.tnb.common.client; + +public class NoClient { +} diff --git a/system-x/common/src/main/java/software/tnb/common/service/ConfigurableService.java b/system-x/common/src/main/java/software/tnb/common/service/ConfigurableService.java index 632dd5b86..f805ee4f9 100644 --- a/system-x/common/src/main/java/software/tnb/common/service/ConfigurableService.java +++ b/system-x/common/src/main/java/software/tnb/common/service/ConfigurableService.java @@ -1,12 +1,12 @@ package software.tnb.common.service; +import software.tnb.common.account.Account; import software.tnb.common.service.configuration.ServiceConfiguration; +import software.tnb.common.util.ReflectionUtil; +import software.tnb.common.validation.Validation; import org.junit.platform.commons.util.ReflectionUtils; -import java.lang.reflect.ParameterizedType; -import java.lang.reflect.Type; - /** * For services that can have more configurations for deployment *

@@ -14,25 +14,18 @@ * because ConfigurableService service can have more deployment configuration in the same test run, * e.g. TestClass1 -> ServiceA.config1 , ... TestClass4 -> ServiceA.config2 , ... TestClass6 -> ServiceA.config1 * and in that case, the ServiceA.config1 will not be undeployed before ServiceA.config2 which would cause that deploy ServiceA.config2 would fail. + * * @param Service configuration class which extends ServiceConfiguration */ -public abstract class ConfigurableService implements Service { - private final C configuration; +public abstract class ConfigurableService extends Service { + private final S configuration; public ConfigurableService() { - Class current = this.getClass(); - while (true) { - Type superClass = current.getGenericSuperclass(); - if (superClass instanceof ParameterizedType && ((ParameterizedType) superClass).getRawType().equals(ConfigurableService.class)) { - break; - } else { - current = current.getSuperclass(); - } - } - configuration = ReflectionUtils.newInstance((Class) ((ParameterizedType) current.getGenericSuperclass()).getActualTypeArguments()[0]); + Class serviceConfigurationClass = (Class) ReflectionUtil.getGenericTypesOf(ConfigurableService.class, this.getClass())[3]; + configuration = ReflectionUtils.newInstance(serviceConfigurationClass); } - public C getConfiguration() { + public S getConfiguration() { return configuration; } diff --git a/system-x/common/src/main/java/software/tnb/common/service/Restartable.java b/system-x/common/src/main/java/software/tnb/common/service/Restartable.java deleted file mode 100644 index e9dd768b0..000000000 --- a/system-x/common/src/main/java/software/tnb/common/service/Restartable.java +++ /dev/null @@ -1,5 +0,0 @@ -package software.tnb.common.service; - -public interface Restartable { - void restart(); -} diff --git a/system-x/common/src/main/java/software/tnb/common/service/Service.java b/system-x/common/src/main/java/software/tnb/common/service/Service.java index 44e4cc1ff..df735a297 100644 --- a/system-x/common/src/main/java/software/tnb/common/service/Service.java +++ b/system-x/common/src/main/java/software/tnb/common/service/Service.java @@ -1,7 +1,31 @@ package software.tnb.common.service; +import software.tnb.common.account.Account; +import software.tnb.common.account.AccountFactory; +import software.tnb.common.util.ReflectionUtil; +import software.tnb.common.validation.Validation; + import org.junit.jupiter.api.extension.AfterAllCallback; import org.junit.jupiter.api.extension.BeforeAllCallback; -public interface Service extends BeforeAllCallback, AfterAllCallback { +public abstract class Service implements BeforeAllCallback, AfterAllCallback { + protected A account; + protected C client; + protected V validation; + + public A account() { + if (account == null) { + Class accountClass = (Class) ReflectionUtil.getGenericTypesOf(Service.class, this.getClass())[0]; + account = AccountFactory.create(accountClass); + } + return account; + } + + protected C client() { + return client; + } + + public V validation() { + return validation; + } } diff --git a/system-x/common/src/main/java/software/tnb/common/service/ServiceFactory.java b/system-x/common/src/main/java/software/tnb/common/service/ServiceFactory.java index 9bf8fe079..eb909c28d 100644 --- a/system-x/common/src/main/java/software/tnb/common/service/ServiceFactory.java +++ b/system-x/common/src/main/java/software/tnb/common/service/ServiceFactory.java @@ -29,15 +29,15 @@ private ServiceFactory() { * @param clazz class to create * @param type */ - public static S create(Class clazz) { + public static > S create(Class clazz) { S service = loadService(clazz); - if (service instanceof ConfigurableService) { - ((ConfigurableService) service).defaultConfiguration(); + if (service instanceof ConfigurableService) { + ((ConfigurableService) service).defaultConfiguration(); } return service; } - private static S loadService(Class clazz) { + private static > S loadService(Class clazz) { if (ReflectionUtils.isAbstract(clazz) || clazz.isInterface()) { final ServiceLoader loader = ServiceLoader.load(clazz); if (loader.stream().findAny().isEmpty()) { @@ -74,22 +74,22 @@ private static S loadService(Class clazz) { } } - public static > S create(Class clazz, Consumer config) { + public static > S create(Class clazz, Consumer config) { S service = create(clazz); config.accept(service.getConfiguration()); return service; } - public static void withService(Class clazz, Consumer code) { + public static > void withService(Class clazz, Consumer code) { withService(create(clazz), code); } - public static > void withService(Class clazz, Consumer config, + public static > void withService(Class clazz, Consumer config, Consumer code) { withService(create(clazz, config), code); } - private static void withService(S service, Consumer code) { + private static > void withService(S service, Consumer code) { try { service.beforeAll(null); } catch (Exception e) { diff --git a/system-x/common/src/main/java/software/tnb/common/service/Validation.java b/system-x/common/src/main/java/software/tnb/common/service/Validation.java deleted file mode 100644 index bf9b2b61f..000000000 --- a/system-x/common/src/main/java/software/tnb/common/service/Validation.java +++ /dev/null @@ -1,4 +0,0 @@ -package software.tnb.common.service; - -public interface Validation { -} diff --git a/system-x/common/src/main/java/software/tnb/common/util/ReflectionUtil.java b/system-x/common/src/main/java/software/tnb/common/util/ReflectionUtil.java new file mode 100644 index 000000000..5f3db9319 --- /dev/null +++ b/system-x/common/src/main/java/software/tnb/common/util/ReflectionUtil.java @@ -0,0 +1,31 @@ +package software.tnb.common.util; + +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; + +public final class ReflectionUtil { + private ReflectionUtil() { + } + + /** + * This method walks though parent classes until the parent class is equal to the first argument of the method. Then it returns an array of + * generic types of the class. + * + * @param parent parent class that is generic + * @param clazz current class + * @return an array of generic types of the parent class + */ + public static Type[] getGenericTypesOf(Class parent, Class clazz) { + Class current = clazz; + while (true) { + Type superClass = current.getGenericSuperclass(); + if (superClass instanceof ParameterizedType && ((ParameterizedType) superClass).getRawType().equals(parent)) { + break; + } else { + current = current.getSuperclass(); + } + } + + return ((ParameterizedType) (current.getGenericSuperclass())).getActualTypeArguments(); + } +} diff --git a/system-x/common/src/main/java/software/tnb/common/validation/NoValidation.java b/system-x/common/src/main/java/software/tnb/common/validation/NoValidation.java new file mode 100644 index 000000000..a45ceb959 --- /dev/null +++ b/system-x/common/src/main/java/software/tnb/common/validation/NoValidation.java @@ -0,0 +1,4 @@ +package software.tnb.common.validation; + +public class NoValidation implements Validation { +} diff --git a/system-x/common/src/main/java/software/tnb/common/validation/Validation.java b/system-x/common/src/main/java/software/tnb/common/validation/Validation.java new file mode 100644 index 000000000..1749d82ae --- /dev/null +++ b/system-x/common/src/main/java/software/tnb/common/validation/Validation.java @@ -0,0 +1,4 @@ +package software.tnb.common.validation; + +public interface Validation { +} diff --git a/system-x/services/aws/cloudwatch/src/main/java/software/tnb/aws/cloudwatch/service/Cloudwatch.java b/system-x/services/aws/cloudwatch/src/main/java/software/tnb/aws/cloudwatch/service/Cloudwatch.java index ab0b5f3a9..2e39642e5 100644 --- a/system-x/services/aws/cloudwatch/src/main/java/software/tnb/aws/cloudwatch/service/Cloudwatch.java +++ b/system-x/services/aws/cloudwatch/src/main/java/software/tnb/aws/cloudwatch/service/Cloudwatch.java @@ -16,6 +16,6 @@ public class Cloudwatch extends AWSService extends ConfigurableService { +public abstract class AWSService + extends ConfigurableService { protected LocalStack localStack; protected static final Logger LOG = LoggerFactory.getLogger(AWSService.class); - protected A account; - protected C client; - protected V validation; - @Override protected void defaultConfiguration() { getConfiguration().useLocalstack(false); } + @Override public A account() { if (account == null) { - account = (A) AccountFactory.create(AWSAccount.class); + Class accountClass = (Class) ReflectionUtil.getGenericTypesOf(AWSService.class, this.getClass())[0]; + account = AccountFactory.create(accountClass); } return account; } - protected C client(Class clazz) { + protected C client() { if (client == null) { - client = AWSClient.createDefaultClient(account(), clazz, getConfiguration().isLocalstack() ? localStack.clientUrl() : null); + Class clientClass = (Class) ReflectionUtil.getGenericTypesOf(AWSService.class, this.getClass())[1]; + client = AWSClient.createDefaultClient(account(), clientClass, getConfiguration().isLocalstack() ? localStack.clientUrl() : null); } return client; } - public V validation() { - return validation; - } - @Override public void beforeAll(ExtensionContext extensionContext) throws Exception { if (getConfiguration().isLocalstack()) { diff --git a/system-x/services/aws/common/src/main/java/software/tnb/aws/common/service/LocalStack.java b/system-x/services/aws/common/src/main/java/software/tnb/aws/common/service/LocalStack.java index e32562fe7..b05efa9aa 100644 --- a/system-x/services/aws/common/src/main/java/software/tnb/aws/common/service/LocalStack.java +++ b/system-x/services/aws/common/src/main/java/software/tnb/aws/common/service/LocalStack.java @@ -1,9 +1,13 @@ package software.tnb.aws.common.service; +import software.tnb.aws.common.account.AWSAccount; import software.tnb.common.deployment.WithDockerImage; import software.tnb.common.service.Service; +import software.tnb.common.validation.Validation; -public abstract class LocalStack implements Service, WithDockerImage { +import software.amazon.awssdk.core.SdkClient; + +public abstract class LocalStack extends Service implements WithDockerImage { protected static final int PORT = 4566; public abstract String serviceUrl(); diff --git a/system-x/services/aws/dynamo-db/src/main/java/software/tnb/aws/dynamodb/service/DynamoDB.java b/system-x/services/aws/dynamo-db/src/main/java/software/tnb/aws/dynamodb/service/DynamoDB.java index 64775d350..37fdfedc1 100644 --- a/system-x/services/aws/dynamo-db/src/main/java/software/tnb/aws/dynamodb/service/DynamoDB.java +++ b/system-x/services/aws/dynamo-db/src/main/java/software/tnb/aws/dynamodb/service/DynamoDB.java @@ -22,7 +22,7 @@ public void beforeAll(ExtensionContext extensionContext) throws Exception { LOG.debug("Creating new DynamoDB validation"); streamsClient = AWSClient.createDefaultClient(account(), DynamoDbStreamsClient.class, getConfiguration().isLocalstack() ? localStack.clientUrl() : null); - validation = new DynamoDBValidation(client(DynamoDbClient.class), streamsClient); + validation = new DynamoDBValidation(client(), streamsClient); } @Override diff --git a/system-x/services/aws/dynamo-db/src/main/java/software/tnb/aws/dynamodb/validation/DynamoDBValidation.java b/system-x/services/aws/dynamo-db/src/main/java/software/tnb/aws/dynamodb/validation/DynamoDBValidation.java index 65f267de8..b7c4e7faa 100644 --- a/system-x/services/aws/dynamo-db/src/main/java/software/tnb/aws/dynamodb/validation/DynamoDBValidation.java +++ b/system-x/services/aws/dynamo-db/src/main/java/software/tnb/aws/dynamodb/validation/DynamoDBValidation.java @@ -1,7 +1,7 @@ package software.tnb.aws.dynamodb.validation; -import software.tnb.common.service.Validation; import software.tnb.common.utils.WaitUtils; +import software.tnb.common.validation.Validation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/system-x/services/aws/iam/src/main/java/software/tnb/aws/iam/service/IAM.java b/system-x/services/aws/iam/src/main/java/software/tnb/aws/iam/service/IAM.java index d3134f691..b9642fb1a 100644 --- a/system-x/services/aws/iam/src/main/java/software/tnb/aws/iam/service/IAM.java +++ b/system-x/services/aws/iam/src/main/java/software/tnb/aws/iam/service/IAM.java @@ -1,9 +1,8 @@ package software.tnb.aws.iam.service; -import software.tnb.aws.iam.validation.IAMValidation; - import software.tnb.aws.common.account.AWSAccount; import software.tnb.aws.common.service.AWSService; +import software.tnb.aws.iam.validation.IAMValidation; import org.junit.jupiter.api.extension.ExtensionContext; @@ -16,7 +15,7 @@ @AutoService(IAM.class) public class IAM extends AWSService { @Override - protected IamClient client(Class clazz) { + protected IamClient client() { // IAM client doesn't have the "create" method as other clients, probably because it's not tied to any region if (client == null) { client = IamClient.builder() @@ -30,6 +29,6 @@ protected IamClient client(Class clazz) { @Override public void beforeAll(ExtensionContext context) throws Exception { LOG.debug("Creating new IAM validation"); - validation = new IAMValidation(client(IamClient.class)); + validation = new IAMValidation(client()); } } diff --git a/system-x/services/aws/iam/src/main/java/software/tnb/aws/iam/validation/IAMValidation.java b/system-x/services/aws/iam/src/main/java/software/tnb/aws/iam/validation/IAMValidation.java index afdc55139..42fe71bd7 100644 --- a/system-x/services/aws/iam/src/main/java/software/tnb/aws/iam/validation/IAMValidation.java +++ b/system-x/services/aws/iam/src/main/java/software/tnb/aws/iam/validation/IAMValidation.java @@ -1,6 +1,6 @@ package software.tnb.aws.iam.validation; -import software.tnb.common.service.Validation; +import software.tnb.common.validation.Validation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/system-x/services/aws/kinesis-firehose/src/main/java/software/tnb/aws/firehose/service/KinesisFirehose.java b/system-x/services/aws/kinesis-firehose/src/main/java/software/tnb/aws/firehose/service/KinesisFirehose.java index 67fda93a4..0d67a1211 100644 --- a/system-x/services/aws/kinesis-firehose/src/main/java/software/tnb/aws/firehose/service/KinesisFirehose.java +++ b/system-x/services/aws/kinesis-firehose/src/main/java/software/tnb/aws/firehose/service/KinesisFirehose.java @@ -21,7 +21,7 @@ public void beforeAll(ExtensionContext extensionContext) throws Exception { super.beforeAll(extensionContext); iam.beforeAll(extensionContext); LOG.debug("Creating new Kinesis validation"); - validation = new KinesisFirehoseValidation(client(FirehoseClient.class), iam); + validation = new KinesisFirehoseValidation(client(), iam); } @Override diff --git a/system-x/services/aws/kinesis-firehose/src/main/java/software/tnb/aws/firehose/validation/KinesisFirehoseValidation.java b/system-x/services/aws/kinesis-firehose/src/main/java/software/tnb/aws/firehose/validation/KinesisFirehoseValidation.java index caeec4281..de7057426 100644 --- a/system-x/services/aws/kinesis-firehose/src/main/java/software/tnb/aws/firehose/validation/KinesisFirehoseValidation.java +++ b/system-x/services/aws/kinesis-firehose/src/main/java/software/tnb/aws/firehose/validation/KinesisFirehoseValidation.java @@ -2,9 +2,9 @@ import static org.junit.jupiter.api.Assertions.fail; -import software.tnb.common.service.Validation; -import software.tnb.common.utils.WaitUtils; import software.tnb.aws.iam.service.IAM; +import software.tnb.common.utils.WaitUtils; +import software.tnb.common.validation.Validation; import org.apache.commons.io.IOUtils; import org.slf4j.Logger; diff --git a/system-x/services/aws/kinesis/src/main/java/software/tnb/aws/kinesis/service/Kinesis.java b/system-x/services/aws/kinesis/src/main/java/software/tnb/aws/kinesis/service/Kinesis.java index d6c6aae09..081d07dba 100644 --- a/system-x/services/aws/kinesis/src/main/java/software/tnb/aws/kinesis/service/Kinesis.java +++ b/system-x/services/aws/kinesis/src/main/java/software/tnb/aws/kinesis/service/Kinesis.java @@ -16,6 +16,6 @@ public class Kinesis extends AWSService cluster.clusterIdentifier().equals(account.clusterIdentifier())).findFirst().get(); diff --git a/system-x/services/aws/redshift/src/main/java/software/tnb/aws/redshift/validation/RedshiftValidation.java b/system-x/services/aws/redshift/src/main/java/software/tnb/aws/redshift/validation/RedshiftValidation.java index 66d9ee002..edf30233a 100644 --- a/system-x/services/aws/redshift/src/main/java/software/tnb/aws/redshift/validation/RedshiftValidation.java +++ b/system-x/services/aws/redshift/src/main/java/software/tnb/aws/redshift/validation/RedshiftValidation.java @@ -1,8 +1,8 @@ package software.tnb.aws.redshift.validation; import software.tnb.aws.redshift.account.RedshiftAccount; -import software.tnb.common.service.Validation; import software.tnb.common.utils.WaitUtils; +import software.tnb.common.validation.Validation; import org.junit.jupiter.api.Assertions; diff --git a/system-x/services/aws/s3/src/main/java/software/tnb/aws/s3/service/Minio.java b/system-x/services/aws/s3/src/main/java/software/tnb/aws/s3/service/Minio.java index b21d2f5af..8aaa3a065 100644 --- a/system-x/services/aws/s3/src/main/java/software/tnb/aws/s3/service/Minio.java +++ b/system-x/services/aws/s3/src/main/java/software/tnb/aws/s3/service/Minio.java @@ -33,7 +33,7 @@ public AWSAccount account() { } @Override - protected S3Client client(Class clazz) { + protected S3Client client() { if (client == null) { client = S3Client.builder() .endpointOverride(URI.create(clientHostname())) diff --git a/system-x/services/aws/s3/src/main/java/software/tnb/aws/s3/service/S3.java b/system-x/services/aws/s3/src/main/java/software/tnb/aws/s3/service/S3.java index 9a26d2d85..20e8bdd1d 100644 --- a/system-x/services/aws/s3/src/main/java/software/tnb/aws/s3/service/S3.java +++ b/system-x/services/aws/s3/src/main/java/software/tnb/aws/s3/service/S3.java @@ -16,6 +16,6 @@ public class S3 extends AWSService { public void beforeAll(ExtensionContext extensionContext) throws Exception { super.beforeAll(extensionContext); LOG.debug("Creating new S3 validation"); - validation = new S3Validation(client(S3Client.class)); + validation = new S3Validation(client()); } } diff --git a/system-x/services/aws/s3/src/main/java/software/tnb/aws/s3/service/local/LocalMinio.java b/system-x/services/aws/s3/src/main/java/software/tnb/aws/s3/service/local/LocalMinio.java index b29c1f3f2..91f39874b 100644 --- a/system-x/services/aws/s3/src/main/java/software/tnb/aws/s3/service/local/LocalMinio.java +++ b/system-x/services/aws/s3/src/main/java/software/tnb/aws/s3/service/local/LocalMinio.java @@ -13,8 +13,6 @@ import java.util.Map; -import software.amazon.awssdk.services.s3.S3Client; - @AutoService(Minio.class) public class LocalMinio extends Minio implements Deployable { @@ -40,7 +38,7 @@ public void undeploy() { @Override public void openResources() { - validation = new S3Validation(client(S3Client.class)); + validation = new S3Validation(client()); } @Override diff --git a/system-x/services/aws/s3/src/main/java/software/tnb/aws/s3/service/openshift/OpenshiftMinio.java b/system-x/services/aws/s3/src/main/java/software/tnb/aws/s3/service/openshift/OpenshiftMinio.java index 62a1a4f15..3ed1c3992 100644 --- a/system-x/services/aws/s3/src/main/java/software/tnb/aws/s3/service/openshift/OpenshiftMinio.java +++ b/system-x/services/aws/s3/src/main/java/software/tnb/aws/s3/service/openshift/OpenshiftMinio.java @@ -32,7 +32,6 @@ import io.fabric8.kubernetes.api.model.apps.DeploymentBuilder; import io.fabric8.kubernetes.client.PortForward; import io.fabric8.kubernetes.client.dsl.PodResource; -import software.amazon.awssdk.services.s3.S3Client; @AutoService(Minio.class) public class OpenshiftMinio extends Minio implements OpenshiftDeployable, WithName, WithInClusterHostname, WithExternalHostname { @@ -115,7 +114,7 @@ public void undeploy() { public void openResources() { localPort = NetworkUtils.getFreePort(); portForward = OpenshiftClient.get().services().withName(name()).portForward(CONTAINER_API_PORT, localPort); - validation = new S3Validation(client(S3Client.class)); + validation = new S3Validation(client()); } @Override diff --git a/system-x/services/aws/s3/src/main/java/software/tnb/aws/s3/validation/S3Validation.java b/system-x/services/aws/s3/src/main/java/software/tnb/aws/s3/validation/S3Validation.java index 80d29b837..3049b3398 100644 --- a/system-x/services/aws/s3/src/main/java/software/tnb/aws/s3/validation/S3Validation.java +++ b/system-x/services/aws/s3/src/main/java/software/tnb/aws/s3/validation/S3Validation.java @@ -1,6 +1,6 @@ package software.tnb.aws.s3.validation; -import software.tnb.common.service.Validation; +import software.tnb.common.validation.Validation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/system-x/services/aws/ses/src/main/java/software/tnb/aws/ses/service/SES.java b/system-x/services/aws/ses/src/main/java/software/tnb/aws/ses/service/SES.java index ca1f98276..5dcb8c38f 100644 --- a/system-x/services/aws/ses/src/main/java/software/tnb/aws/ses/service/SES.java +++ b/system-x/services/aws/ses/src/main/java/software/tnb/aws/ses/service/SES.java @@ -3,7 +3,6 @@ import software.tnb.aws.common.service.AWSService; import software.tnb.aws.ses.account.SESAccount; import software.tnb.aws.ses.validation.SESValidation; -import software.tnb.common.account.AccountFactory; import org.junit.jupiter.api.extension.ExtensionContext; @@ -13,19 +12,10 @@ @AutoService(SES.class) public class SES extends AWSService { - @Override - public SESAccount account() { - if (account == null) { - LOG.debug("Creating new SES account"); - account = AccountFactory.create(SESAccount.class); - } - return account; - } - @Override public void beforeAll(ExtensionContext extensionContext) throws Exception { super.beforeAll(extensionContext); LOG.debug("Creating new SES validation"); - validation = new SESValidation(client(SesClient.class), account()); + validation = new SESValidation(client(), account()); } } diff --git a/system-x/services/aws/ses/src/main/java/software/tnb/aws/ses/validation/SESValidation.java b/system-x/services/aws/ses/src/main/java/software/tnb/aws/ses/validation/SESValidation.java index f8db752c2..24a4ca231 100644 --- a/system-x/services/aws/ses/src/main/java/software/tnb/aws/ses/validation/SESValidation.java +++ b/system-x/services/aws/ses/src/main/java/software/tnb/aws/ses/validation/SESValidation.java @@ -1,7 +1,7 @@ package software.tnb.aws.ses.validation; import software.tnb.aws.ses.account.SESAccount; -import software.tnb.common.service.Validation; +import software.tnb.common.validation.Validation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/system-x/services/aws/sns/src/main/java/software/tnb/aws/sns/service/SNS.java b/system-x/services/aws/sns/src/main/java/software/tnb/aws/sns/service/SNS.java index f93edb987..4c19bfa85 100644 --- a/system-x/services/aws/sns/src/main/java/software/tnb/aws/sns/service/SNS.java +++ b/system-x/services/aws/sns/src/main/java/software/tnb/aws/sns/service/SNS.java @@ -51,6 +51,6 @@ public void beforeAll(ExtensionContext extensionContext) throws Exception { sqs = ServiceFactory.create(SQS.class, config -> config.useLocalstack(getConfiguration().isLocalstack())); sqs.beforeAll(extensionContext); LOG.debug("Creating new SNS validation"); - validation = new SNSValidation(client(SnsClient.class), account(), sqs); + validation = new SNSValidation(client(), account(), sqs); } } diff --git a/system-x/services/aws/sns/src/main/java/software/tnb/aws/sns/validation/SNSValidation.java b/system-x/services/aws/sns/src/main/java/software/tnb/aws/sns/validation/SNSValidation.java index 7ea5d0ce4..35e2ac2de 100644 --- a/system-x/services/aws/sns/src/main/java/software/tnb/aws/sns/validation/SNSValidation.java +++ b/system-x/services/aws/sns/src/main/java/software/tnb/aws/sns/validation/SNSValidation.java @@ -2,8 +2,8 @@ import software.tnb.aws.sns.account.SNSAccount; import software.tnb.aws.sqs.service.SQS; -import software.tnb.common.service.Validation; import software.tnb.common.utils.WaitUtils; +import software.tnb.common.validation.Validation; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; diff --git a/system-x/services/aws/sqs/src/main/java/software/tnb/aws/sqs/service/SQS.java b/system-x/services/aws/sqs/src/main/java/software/tnb/aws/sqs/service/SQS.java index 132145a06..9bcf8ed12 100644 --- a/system-x/services/aws/sqs/src/main/java/software/tnb/aws/sqs/service/SQS.java +++ b/system-x/services/aws/sqs/src/main/java/software/tnb/aws/sqs/service/SQS.java @@ -39,6 +39,6 @@ public SQSAccount account() { public void beforeAll(ExtensionContext extensionContext) throws Exception { super.beforeAll(extensionContext); LOG.debug("Creating new SQS validation"); - validation = new SQSValidation(client(SqsClient.class), account()); + validation = new SQSValidation(client(), account()); } } diff --git a/system-x/services/aws/sqs/src/main/java/software/tnb/aws/sqs/validation/SQSValidation.java b/system-x/services/aws/sqs/src/main/java/software/tnb/aws/sqs/validation/SQSValidation.java index cd9b74185..f2e6cfb2a 100644 --- a/system-x/services/aws/sqs/src/main/java/software/tnb/aws/sqs/validation/SQSValidation.java +++ b/system-x/services/aws/sqs/src/main/java/software/tnb/aws/sqs/validation/SQSValidation.java @@ -1,8 +1,8 @@ package software.tnb.aws.sqs.validation; import software.tnb.aws.sqs.account.SQSAccount; -import software.tnb.common.service.Validation; import software.tnb.common.utils.WaitUtils; +import software.tnb.common.validation.Validation; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; diff --git a/system-x/services/azure/event-hubs/src/main/java/software/tnb/azure/event/hubs/client/EventHubClients.java b/system-x/services/azure/event-hubs/src/main/java/software/tnb/azure/event/hubs/client/EventHubClients.java new file mode 100644 index 000000000..181c998ca --- /dev/null +++ b/system-x/services/azure/event-hubs/src/main/java/software/tnb/azure/event/hubs/client/EventHubClients.java @@ -0,0 +1,49 @@ +package software.tnb.azure.event.hubs.client; + +import software.tnb.azure.event.hubs.account.EventHubsAccount; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.azure.messaging.eventhubs.EventHubClientBuilder; +import com.azure.messaging.eventhubs.EventHubConsumerClient; +import com.azure.messaging.eventhubs.EventHubProducerClient; + +public class EventHubClients { + private static final Logger LOG = LoggerFactory.getLogger(EventHubClients.class); + + private final EventHubConsumerClient consumerClient; + private final EventHubProducerClient producerClient; + + public EventHubClients(EventHubsAccount account) { + LOG.debug("Creating new Azure Event Hub clients"); + producerClient = new EventHubClientBuilder() + .connectionString(String.format(("Endpoint=sb://%s.servicebus.windows.net/;SharedAccessKeyName=%s;SharedAccessKey=%s;EntityPath=%s"), + account.getEventHubsNamespace(), account.getEventHubSharedAccessName(), account.getEventHubSharedAccessKey(), + account.getEvenHubName())) + .buildProducerClient(); + consumerClient = new EventHubClientBuilder() + .consumerGroup(EventHubClientBuilder.DEFAULT_CONSUMER_GROUP_NAME) + .connectionString(String.format(("Endpoint=sb://%s.servicebus.windows.net/;SharedAccessKeyName=%s;SharedAccessKey=%s;EntityPath=%s"), + account.getEventHubsNamespace(), account.getEventHubSharedAccessName(), account.getEventHubSharedAccessKey(), + account.getEvenHubName())) + .buildConsumerClient(); + } + + public EventHubConsumerClient consumerClient() { + return consumerClient; + } + + public EventHubProducerClient producerClient() { + return producerClient; + } + + public void close() { + if (consumerClient != null) { + consumerClient.close(); + } + if (producerClient != null) { + producerClient.close(); + } + } +} diff --git a/system-x/services/azure/event-hubs/src/main/java/software/tnb/azure/event/hubs/service/EventHubs.java b/system-x/services/azure/event-hubs/src/main/java/software/tnb/azure/event/hubs/service/EventHubs.java index a659a378d..f2572452e 100644 --- a/system-x/services/azure/event-hubs/src/main/java/software/tnb/azure/event/hubs/service/EventHubs.java +++ b/system-x/services/azure/event-hubs/src/main/java/software/tnb/azure/event/hubs/service/EventHubs.java @@ -1,79 +1,36 @@ package software.tnb.azure.event.hubs.service; import software.tnb.azure.event.hubs.account.EventHubsAccount; +import software.tnb.azure.event.hubs.client.EventHubClients; import software.tnb.azure.event.hubs.validation.EventHubsValidation; import software.tnb.azure.storage.blob.service.StorageBlob; -import software.tnb.common.account.AccountFactory; import software.tnb.common.service.Service; import software.tnb.common.service.ServiceFactory; import org.junit.jupiter.api.extension.ExtensionContext; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.azure.messaging.eventhubs.EventHubClientBuilder; -import com.azure.messaging.eventhubs.EventHubConsumerClient; -import com.azure.messaging.eventhubs.EventHubProducerClient; import com.google.auto.service.AutoService; import java.util.Date; @AutoService(EventHubs.class) -public class EventHubs implements Service { +public class EventHubs extends Service { private final StorageBlob storageBlob = ServiceFactory.create(StorageBlob.class); private String blobContainer; - private static final Logger LOG = LoggerFactory.getLogger(EventHubs.class); - - private EventHubsAccount account; - private EventHubsValidation validation; - - private EventHubConsumerClient consumerClient; - private EventHubProducerClient producerClient; - public String getBlobContainer() { return blobContainer; } - public EventHubsAccount account() { - if (account == null) { - account = AccountFactory.create(EventHubsAccount.class); - } - return account; - } - - protected EventHubProducerClient producerClient() { - LOG.debug("Creating new Azure Event Hub producer client"); - return new EventHubClientBuilder() - .connectionString(String.format(("Endpoint=sb://%s.servicebus.windows.net/;SharedAccessKeyName=%s;SharedAccessKey=%s;EntityPath=%s"), - account().getEventHubsNamespace(), account().getEventHubSharedAccessName(), account().getEventHubSharedAccessKey(), - account().getEvenHubName())) - .buildProducerClient(); - } - - protected EventHubConsumerClient consumerClient() { - LOG.debug("Creating new Azure Event Hub consumer client"); - return new EventHubClientBuilder() - .consumerGroup(EventHubClientBuilder.DEFAULT_CONSUMER_GROUP_NAME) - .connectionString(String.format(("Endpoint=sb://%s.servicebus.windows.net/;SharedAccessKeyName=%s;SharedAccessKey=%s;EntityPath=%s"), - account().getEventHubsNamespace(), account().getEventHubSharedAccessName(), account().getEventHubSharedAccessKey(), - account().getEvenHubName())) - .buildConsumerClient(); - } - - public EventHubsValidation validation() { - return validation; - } - @Override public void afterAll(ExtensionContext extensionContext) throws Exception { storageBlob.validation().deleteBlobContainer(blobContainer); storageBlob.afterAll(extensionContext); - producerClient.close(); - consumerClient.close(); + if (client != null) { + client.close(); + } } @Override @@ -82,9 +39,14 @@ public void beforeAll(ExtensionContext extensionContext) throws Exception { blobContainer = "event-hub-blob-" + new Date().getTime(); storageBlob.validation().createBlobContainer(blobContainer); - producerClient = producerClient(); - consumerClient = consumerClient(); + validation = new EventHubsValidation(client()); + } - validation = new EventHubsValidation(consumerClient, producerClient); + @Override + protected EventHubClients client() { + if (client == null) { + client = new EventHubClients(account()); + } + return client; } } diff --git a/system-x/services/azure/event-hubs/src/main/java/software/tnb/azure/event/hubs/validation/EventHubsValidation.java b/system-x/services/azure/event-hubs/src/main/java/software/tnb/azure/event/hubs/validation/EventHubsValidation.java index d8b0415e8..d44ff0252 100644 --- a/system-x/services/azure/event-hubs/src/main/java/software/tnb/azure/event/hubs/validation/EventHubsValidation.java +++ b/system-x/services/azure/event-hubs/src/main/java/software/tnb/azure/event/hubs/validation/EventHubsValidation.java @@ -1,8 +1,9 @@ package software.tnb.azure.event.hubs.validation; +import software.tnb.azure.event.hubs.client.EventHubClients; +import software.tnb.common.validation.Validation; + import com.azure.messaging.eventhubs.EventData; -import com.azure.messaging.eventhubs.EventHubConsumerClient; -import com.azure.messaging.eventhubs.EventHubProducerClient; import com.azure.messaging.eventhubs.models.EventPosition; import com.azure.messaging.eventhubs.models.PartitionEvent; import com.azure.messaging.eventhubs.models.SendOptions; @@ -13,13 +14,11 @@ import java.util.List; import java.util.stream.Collectors; -public class EventHubsValidation { - private final EventHubProducerClient producerClient; - private final EventHubConsumerClient consumerClient; +public class EventHubsValidation implements Validation { + private final EventHubClients clients; - public EventHubsValidation(EventHubConsumerClient consumerClient, EventHubProducerClient producerClient) { - this.consumerClient = consumerClient; - this.producerClient = producerClient; + public EventHubsValidation(EventHubClients clients) { + this.clients = clients; } public void produceEvent(String message) { @@ -28,7 +27,7 @@ public void produceEvent(String message) { public void produceEvent(String message, String partitionId) { final SendOptions sendOptions = new SendOptions().setPartitionId(partitionId); - producerClient.send(Collections.singletonList(new EventData(message)), sendOptions); + clients.producerClient().send(Collections.singletonList(new EventData(message)), sendOptions); } /** @@ -40,14 +39,14 @@ public void produceEvent(String message, String partitionId) { */ public List consumeEvents(Duration maxWaitTime, int maxEvents) { List messages = new ArrayList<>(); - for (String partitionId : consumerClient.getPartitionIds()) { + for (String partitionId : clients.consumerClient().getPartitionIds()) { messages.addAll(consumeEvents(partitionId, maxWaitTime, maxEvents)); } return messages; } public List consumeEvents(String partitionId, Duration duration, int maxEvents) { - return consumerClient.receiveFromPartition(partitionId, maxEvents, EventPosition.earliest(), duration).stream() + return clients.consumerClient().receiveFromPartition(partitionId, maxEvents, EventPosition.earliest(), duration).stream() .collect(Collectors.toList()); } } diff --git a/system-x/services/azure/common/src/main/java/software/tnb/azure/common/account/AzureServiceBusAccount.java b/system-x/services/azure/service-bus/src/main/java/software/tnb/azure/service/bus/account/ServiceBusAccount.java similarity index 79% rename from system-x/services/azure/common/src/main/java/software/tnb/azure/common/account/AzureServiceBusAccount.java rename to system-x/services/azure/service-bus/src/main/java/software/tnb/azure/service/bus/account/ServiceBusAccount.java index 766d74f90..1b62e8405 100644 --- a/system-x/services/azure/common/src/main/java/software/tnb/azure/common/account/AzureServiceBusAccount.java +++ b/system-x/services/azure/service-bus/src/main/java/software/tnb/azure/service/bus/account/ServiceBusAccount.java @@ -1,9 +1,9 @@ -package software.tnb.azure.common.account; +package software.tnb.azure.service.bus.account; import software.tnb.common.account.Account; import software.tnb.common.account.WithId; -public class AzureServiceBusAccount implements Account, WithId { +public class ServiceBusAccount implements Account, WithId { private String connection_string; @Override diff --git a/system-x/services/azure/service-bus/src/main/java/software/tnb/azure/service/bus/service/ServiceBus.java b/system-x/services/azure/service-bus/src/main/java/software/tnb/azure/service/bus/service/ServiceBus.java index 99e853343..8c14677ab 100644 --- a/system-x/services/azure/service-bus/src/main/java/software/tnb/azure/service/bus/service/ServiceBus.java +++ b/system-x/services/azure/service-bus/src/main/java/software/tnb/azure/service/bus/service/ServiceBus.java @@ -1,8 +1,7 @@ package software.tnb.azure.service.bus.service; -import software.tnb.azure.common.account.AzureServiceBusAccount; +import software.tnb.azure.service.bus.account.ServiceBusAccount; import software.tnb.azure.service.bus.validation.ServiceBusValidation; -import software.tnb.common.account.AccountFactory; import software.tnb.common.service.Service; import org.junit.jupiter.api.extension.ExtensionContext; @@ -12,19 +11,13 @@ import com.google.auto.service.AutoService; @AutoService(ServiceBus.class) -public class ServiceBus implements Service { - private ServiceBusValidation validation; - - private static AzureServiceBusAccount azureServiceBusAccount() { - return AccountFactory.create(AzureServiceBusAccount.class); - } - - private static ServiceBusAdministrationClient getAdminClient() { - return new ServiceBusAdministrationClientBuilder().connectionString(azureServiceBusAccount().connectionString()).buildClient(); - } - - public ServiceBusValidation validation() { - return validation; +public class ServiceBus extends Service { + @Override + protected ServiceBusAdministrationClient client() { + if (client == null) { + client = new ServiceBusAdministrationClientBuilder().connectionString(account().connectionString()).buildClient(); + } + return client; } @Override @@ -34,6 +27,6 @@ public void afterAll(ExtensionContext extensionContext) { @Override public void beforeAll(ExtensionContext extensionContext) { - validation = new ServiceBusValidation(azureServiceBusAccount(), getAdminClient()); + validation = new ServiceBusValidation(account(), client()); } } diff --git a/system-x/services/azure/service-bus/src/main/java/software/tnb/azure/service/bus/validation/ServiceBusValidation.java b/system-x/services/azure/service-bus/src/main/java/software/tnb/azure/service/bus/validation/ServiceBusValidation.java index 66e3a0cc0..367eb203e 100644 --- a/system-x/services/azure/service-bus/src/main/java/software/tnb/azure/service/bus/validation/ServiceBusValidation.java +++ b/system-x/services/azure/service-bus/src/main/java/software/tnb/azure/service/bus/validation/ServiceBusValidation.java @@ -1,9 +1,10 @@ package software.tnb.azure.service.bus.validation; -import software.tnb.azure.common.account.AzureServiceBusAccount; +import software.tnb.azure.service.bus.account.ServiceBusAccount; import software.tnb.azure.service.bus.service.ErrorProcessor; import software.tnb.azure.service.bus.service.MessageProcessor; import software.tnb.common.utils.WaitUtils; +import software.tnb.common.validation.Validation; import com.azure.messaging.servicebus.ServiceBusClientBuilder; import com.azure.messaging.servicebus.ServiceBusMessage; @@ -15,17 +16,17 @@ import java.util.stream.Collectors; import java.util.stream.Stream; -public class ServiceBusValidation { - private final AzureServiceBusAccount azureServiceBusAccount; +public class ServiceBusValidation implements Validation { + private final ServiceBusAccount serviceBusAccount; private final ServiceBusAdministrationClient adminClient; - public ServiceBusValidation(AzureServiceBusAccount azureServiceBusAccount, ServiceBusAdministrationClient adminClient) { - this.azureServiceBusAccount = azureServiceBusAccount; + public ServiceBusValidation(ServiceBusAccount serviceBusAccount, ServiceBusAdministrationClient adminClient) { + this.serviceBusAccount = serviceBusAccount; this.adminClient = adminClient; } - public AzureServiceBusAccount getAzureServiceBusAccount() { - return azureServiceBusAccount; + public ServiceBusAccount getAzureServiceBusAccount() { + return serviceBusAccount; } public void createQueue(String queue) { @@ -42,7 +43,7 @@ public void deleteQueue(String queue) { public void sendMessage(String queue, String message) { ServiceBusSenderClient client = new ServiceBusClientBuilder() - .connectionString(azureServiceBusAccount.connectionString()) + .connectionString(serviceBusAccount.connectionString()) .sender() .queueName(queue) .buildClient(); @@ -55,7 +56,7 @@ public List receiveMessages(String queue) { final ErrorProcessor errorProcessor = new ErrorProcessor(); try (ServiceBusProcessorClient client = new ServiceBusClientBuilder() - .connectionString(azureServiceBusAccount.connectionString()) + .connectionString(serviceBusAccount.connectionString()) .processor() .queueName(queue) .processMessage(messageProcessor) diff --git a/system-x/services/azure/storage-blob/src/main/java/software/tnb/azure/storage/blob/service/StorageBlob.java b/system-x/services/azure/storage-blob/src/main/java/software/tnb/azure/storage/blob/service/StorageBlob.java index c5f85260c..709de2ad7 100644 --- a/system-x/services/azure/storage-blob/src/main/java/software/tnb/azure/storage/blob/service/StorageBlob.java +++ b/system-x/services/azure/storage-blob/src/main/java/software/tnb/azure/storage/blob/service/StorageBlob.java @@ -2,7 +2,6 @@ import software.tnb.azure.common.account.AzureAccount; import software.tnb.azure.storage.blob.validation.StorageBlobValidation; -import software.tnb.common.account.AccountFactory; import software.tnb.common.service.Service; import org.junit.jupiter.api.extension.ExtensionContext; @@ -16,20 +15,10 @@ import com.google.auto.service.AutoService; @AutoService(StorageBlob.class) -public class StorageBlob implements Service { +public class StorageBlob extends Service { private static final Logger LOG = LoggerFactory.getLogger(StorageBlob.class); - private AzureAccount account; - private StorageBlobValidation validation; - - public AzureAccount account() { - if (account == null) { - account = AccountFactory.create(AzureAccount.class); - } - return account; - } - protected BlobServiceClient client() { LOG.debug("Creating new Azure Storage Blob client"); return new BlobServiceClientBuilder() @@ -38,10 +27,6 @@ protected BlobServiceClient client() { .buildClient(); } - public StorageBlobValidation validation() { - return validation; - } - @Override public void afterAll(ExtensionContext extensionContext) throws Exception { // no-op diff --git a/system-x/services/azure/storage-blob/src/main/java/software/tnb/azure/storage/blob/validation/StorageBlobValidation.java b/system-x/services/azure/storage-blob/src/main/java/software/tnb/azure/storage/blob/validation/StorageBlobValidation.java index e45a645ee..cc007477e 100644 --- a/system-x/services/azure/storage-blob/src/main/java/software/tnb/azure/storage/blob/validation/StorageBlobValidation.java +++ b/system-x/services/azure/storage-blob/src/main/java/software/tnb/azure/storage/blob/validation/StorageBlobValidation.java @@ -1,5 +1,7 @@ package software.tnb.azure.storage.blob.validation; +import software.tnb.common.validation.Validation; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -11,7 +13,7 @@ import java.util.List; import java.util.stream.Collectors; -public class StorageBlobValidation { +public class StorageBlobValidation implements Validation { private static final Logger LOG = LoggerFactory.getLogger(StorageBlobValidation.class); diff --git a/system-x/services/azure/storage-queue/src/main/java/software/tnb/azure/storage/queue/service/StorageQueue.java b/system-x/services/azure/storage-queue/src/main/java/software/tnb/azure/storage/queue/service/StorageQueue.java index 80545f2f8..e7d8d7bc4 100644 --- a/system-x/services/azure/storage-queue/src/main/java/software/tnb/azure/storage/queue/service/StorageQueue.java +++ b/system-x/services/azure/storage-queue/src/main/java/software/tnb/azure/storage/queue/service/StorageQueue.java @@ -2,7 +2,6 @@ import software.tnb.azure.common.account.AzureAccount; import software.tnb.azure.storage.queue.validation.StorageQueueValidation; -import software.tnb.common.account.AccountFactory; import software.tnb.common.service.Service; import org.junit.jupiter.api.extension.ExtensionContext; @@ -16,19 +15,9 @@ import com.google.auto.service.AutoService; @AutoService(StorageQueue.class) -public class StorageQueue implements Service { +public class StorageQueue extends Service { private static final Logger LOG = LoggerFactory.getLogger(StorageQueue.class); - private AzureAccount account; - private StorageQueueValidation validation; - - public AzureAccount account() { - if (account == null) { - account = AccountFactory.create(AzureAccount.class); - } - return account; - } - protected QueueServiceClient client() { LOG.debug("Creating new Storage Queue client"); return new QueueServiceClientBuilder() @@ -37,10 +26,6 @@ protected QueueServiceClient client() { .buildClient(); } - public StorageQueueValidation validation() { - return validation; - } - @Override public void afterAll(ExtensionContext extensionContext) throws Exception { // no-op diff --git a/system-x/services/azure/storage-queue/src/main/java/software/tnb/azure/storage/queue/validation/StorageQueueValidation.java b/system-x/services/azure/storage-queue/src/main/java/software/tnb/azure/storage/queue/validation/StorageQueueValidation.java index f8fa02a55..dcb4d819d 100644 --- a/system-x/services/azure/storage-queue/src/main/java/software/tnb/azure/storage/queue/validation/StorageQueueValidation.java +++ b/system-x/services/azure/storage-queue/src/main/java/software/tnb/azure/storage/queue/validation/StorageQueueValidation.java @@ -1,5 +1,7 @@ package software.tnb.azure.storage.queue.validation; +import software.tnb.common.validation.Validation; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -9,7 +11,7 @@ import java.util.ArrayList; import java.util.List; -public class StorageQueueValidation { +public class StorageQueueValidation implements Validation { private static final Logger LOG = LoggerFactory.getLogger(StorageQueueValidation.class); private final QueueServiceClient client; diff --git a/system-x/services/cryostat/src/main/java/software/tnb/cryostat/service/Cryostat.java b/system-x/services/cryostat/src/main/java/software/tnb/cryostat/service/Cryostat.java index 599e6f511..8595ba297 100644 --- a/system-x/services/cryostat/src/main/java/software/tnb/cryostat/service/Cryostat.java +++ b/system-x/services/cryostat/src/main/java/software/tnb/cryostat/service/Cryostat.java @@ -1,15 +1,13 @@ package software.tnb.cryostat.service; +import software.tnb.common.account.Account; import software.tnb.common.service.Service; import software.tnb.cryostat.client.CryostatClient; import software.tnb.cryostat.validation.CryostatValidation; import java.util.Optional; -public abstract class Cryostat implements Service { - - protected CryostatValidation validation; - +public abstract class Cryostat extends Service { public abstract String connectionUrl(); public abstract CryostatClient client(); diff --git a/system-x/services/cryostat/src/main/java/software/tnb/cryostat/validation/CryostatValidation.java b/system-x/services/cryostat/src/main/java/software/tnb/cryostat/validation/CryostatValidation.java index a0ee2af67..bf5bec1d8 100644 --- a/system-x/services/cryostat/src/main/java/software/tnb/cryostat/validation/CryostatValidation.java +++ b/system-x/services/cryostat/src/main/java/software/tnb/cryostat/validation/CryostatValidation.java @@ -1,9 +1,10 @@ package software.tnb.cryostat.validation; +import software.tnb.common.validation.Validation; +import software.tnb.cryostat.client.CryostatClient; import software.tnb.cryostat.generated.recording.Recording; import software.tnb.cryostat.generated.targets.Cryostat; import software.tnb.cryostat.generated.targets.Target; -import software.tnb.cryostat.client.CryostatClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -14,7 +15,7 @@ import java.util.UUID; import java.util.concurrent.atomic.AtomicReference; -public class CryostatValidation { +public class CryostatValidation implements Validation { private static final Logger LOG = LoggerFactory.getLogger(CryostatValidation.class); diff --git a/system-x/services/db/cassandra/src/main/java/software/tnb/db/cassandra/resource/local/LocalCassandra.java b/system-x/services/db/cassandra/src/main/java/software/tnb/db/cassandra/resource/local/LocalCassandra.java index 167ad4ef7..66a39c9d7 100644 --- a/system-x/services/db/cassandra/src/main/java/software/tnb/db/cassandra/resource/local/LocalCassandra.java +++ b/system-x/services/db/cassandra/src/main/java/software/tnb/db/cassandra/resource/local/LocalCassandra.java @@ -72,7 +72,7 @@ public int port() { } @Override - protected CqlSession session() { + protected CqlSession client() { return session; } diff --git a/system-x/services/db/cassandra/src/main/java/software/tnb/db/cassandra/resource/openshift/OpenshiftCassandra.java b/system-x/services/db/cassandra/src/main/java/software/tnb/db/cassandra/resource/openshift/OpenshiftCassandra.java index 83a2c4ca4..f214c70d7 100644 --- a/system-x/services/db/cassandra/src/main/java/software/tnb/db/cassandra/resource/openshift/OpenshiftCassandra.java +++ b/system-x/services/db/cassandra/src/main/java/software/tnb/db/cassandra/resource/openshift/OpenshiftCassandra.java @@ -104,7 +104,7 @@ public void create() { } @Override - protected CqlSession session() { + protected CqlSession client() { return session; } diff --git a/system-x/services/db/cassandra/src/main/java/software/tnb/db/cassandra/service/Cassandra.java b/system-x/services/db/cassandra/src/main/java/software/tnb/db/cassandra/service/Cassandra.java index a2fbf5efb..f803caefd 100644 --- a/system-x/services/db/cassandra/src/main/java/software/tnb/db/cassandra/service/Cassandra.java +++ b/system-x/services/db/cassandra/src/main/java/software/tnb/db/cassandra/service/Cassandra.java @@ -10,13 +10,10 @@ import java.util.Map; -public abstract class Cassandra implements Service, WithDockerImage { +public abstract class Cassandra extends Service implements WithDockerImage { public static final int CASSANDRA_PORT = 9042; - private CassandraAccount account; - protected CassandraValidation validation; - public String defaultImage() { // official library image required hacks in openshift, bitnami works out of the box return "quay.io/fuse_qe/cassandra:4.0.7"; @@ -24,13 +21,11 @@ public String defaultImage() { public CassandraValidation validation() { if (validation == null) { - validation = new CassandraValidation(session()); + validation = new CassandraValidation(client()); } return validation; } - protected abstract CqlSession session(); - public abstract int port(); public abstract String host(); diff --git a/system-x/services/db/cassandra/src/main/java/software/tnb/db/cassandra/validation/CassandraValidation.java b/system-x/services/db/cassandra/src/main/java/software/tnb/db/cassandra/validation/CassandraValidation.java index 1b9b7349a..4e5440569 100644 --- a/system-x/services/db/cassandra/src/main/java/software/tnb/db/cassandra/validation/CassandraValidation.java +++ b/system-x/services/db/cassandra/src/main/java/software/tnb/db/cassandra/validation/CassandraValidation.java @@ -1,5 +1,7 @@ package software.tnb.db.cassandra.validation; +import software.tnb.common.validation.Validation; + import com.datastax.oss.driver.api.core.CqlIdentifier; import com.datastax.oss.driver.api.core.CqlSession; import com.datastax.oss.driver.api.core.cql.ResultSet; @@ -10,7 +12,7 @@ import java.util.Collection; import java.util.List; -public class CassandraValidation { +public class CassandraValidation implements Validation { private final CqlSession session; diff --git a/system-x/services/db/common/src/main/java/software/tnb/db/common/service/SQL.java b/system-x/services/db/common/src/main/java/software/tnb/db/common/service/SQL.java index 533abdad4..17ac8ac9a 100644 --- a/system-x/services/db/common/src/main/java/software/tnb/db/common/service/SQL.java +++ b/system-x/services/db/common/src/main/java/software/tnb/db/common/service/SQL.java @@ -1,6 +1,7 @@ package software.tnb.db.common.service; import software.tnb.common.account.AccountFactory; +import software.tnb.common.client.NoClient; import software.tnb.common.deployment.WithDockerImage; import software.tnb.common.deployment.WithExternalHostname; import software.tnb.common.deployment.WithName; @@ -13,12 +14,9 @@ import java.util.Map; -public abstract class SQL implements Service, WithName, WithExternalHostname, WithDockerImage { +public abstract class SQL extends Service implements WithName, WithExternalHostname, WithDockerImage { private static final Logger LOG = LoggerFactory.getLogger(SQL.class); - private SQLAccount account; - protected SQLValidation validation; - protected abstract Class accountClass(); public SQLAccount account() { @@ -38,6 +36,7 @@ public int localPort() { return port(); } + @Override public SQLValidation validation() { if (validation == null) { LOG.debug("Creating new SQL validation"); @@ -48,6 +47,7 @@ public SQLValidation validation() { /** * Override this method in case the default replace doesn't work for the given type of database + * * @return local connection url (through port-forward in case of openshift) */ protected String localConnectionUrl() { diff --git a/system-x/services/db/common/src/main/java/software/tnb/db/common/validation/SQLValidation.java b/system-x/services/db/common/src/main/java/software/tnb/db/common/validation/SQLValidation.java index bf6f130b0..d11f197a3 100644 --- a/system-x/services/db/common/src/main/java/software/tnb/db/common/validation/SQLValidation.java +++ b/system-x/services/db/common/src/main/java/software/tnb/db/common/validation/SQLValidation.java @@ -1,5 +1,6 @@ package software.tnb.db.common.validation; +import software.tnb.common.validation.Validation; import software.tnb.db.common.account.SQLAccount; import java.sql.Connection; @@ -8,7 +9,7 @@ import java.sql.SQLException; import java.util.function.Consumer; -public class SQLValidation { +public class SQLValidation implements Validation { private final String jdbcConnectionUrl; private final SQLAccount account; diff --git a/system-x/services/db/dballocator/src/main/java/software/tnb/db/dballocator/service/DbAllocatorConfiguration.java b/system-x/services/db/dballocator/src/main/java/software/tnb/db/dballocator/configuration/DbAllocatorConfiguration.java similarity index 96% rename from system-x/services/db/dballocator/src/main/java/software/tnb/db/dballocator/service/DbAllocatorConfiguration.java rename to system-x/services/db/dballocator/src/main/java/software/tnb/db/dballocator/configuration/DbAllocatorConfiguration.java index e03bbeb8d..c64a4fc2a 100644 --- a/system-x/services/db/dballocator/src/main/java/software/tnb/db/dballocator/service/DbAllocatorConfiguration.java +++ b/system-x/services/db/dballocator/src/main/java/software/tnb/db/dballocator/configuration/DbAllocatorConfiguration.java @@ -1,4 +1,4 @@ -package software.tnb.db.dballocator.service; +package software.tnb.db.dballocator.configuration; import software.tnb.common.config.Configuration; diff --git a/system-x/services/db/dballocator/src/main/java/software/tnb/db/dballocator/service/DbAllocator.java b/system-x/services/db/dballocator/src/main/java/software/tnb/db/dballocator/service/DbAllocator.java index fba332f59..ba9b9affa 100644 --- a/system-x/services/db/dballocator/src/main/java/software/tnb/db/dballocator/service/DbAllocator.java +++ b/system-x/services/db/dballocator/src/main/java/software/tnb/db/dballocator/service/DbAllocator.java @@ -1,7 +1,10 @@ package software.tnb.db.dballocator.service; -import software.tnb.db.dballocator.validation.SqlValidation; +import software.tnb.common.account.NoAccount; +import software.tnb.common.client.NoClient; import software.tnb.common.service.Service; +import software.tnb.db.dballocator.configuration.DbAllocatorConfiguration; +import software.tnb.db.dballocator.validation.SqlValidation; import org.junit.jupiter.api.extension.ExtensionContext; @@ -16,13 +19,12 @@ import java.util.Optional; @AutoService(DbAllocator.class) -public class DbAllocator implements Service { +public class DbAllocator extends Service { private static final String API = DbAllocatorConfiguration.getUrl(); private static final Logger LOG = LoggerFactory.getLogger(DbAllocator.class); - private DbAllocatorResource resource; - private Optional validation; + private final DbAllocatorResource resource; private Optional allocation = Optional.empty(); @@ -47,14 +49,8 @@ public void beforeAll(ExtensionContext extensionContext) throws Exception { DbAllocatorConfiguration.getExpire(), DbAllocatorConfiguration.getErase()) ); - allocation.ifPresent(a -> { - LOG.info("Database '{}' has been allocated {}", label, a.getUuid()); - }); - validation = allocation.map(i -> new SqlValidation(i, RestClientFactory.getSqlExecutorRestClient(API + "/sql-executor/api/"))); - } - - public SqlValidation validation() { - return validation.orElse(null); + allocation.ifPresent(a -> LOG.info("Database '{}' has been allocated {}", label, a.getUuid())); + validation = allocation.map(i -> new SqlValidation(i, RestClientFactory.getSqlExecutorRestClient(API + "/sql-executor/api/"))).get(); } public Optional getAllocation() { diff --git a/system-x/services/db/dballocator/src/main/java/software/tnb/db/dballocator/validation/SqlValidation.java b/system-x/services/db/dballocator/src/main/java/software/tnb/db/dballocator/validation/SqlValidation.java index 242986dff..55c709579 100644 --- a/system-x/services/db/dballocator/src/main/java/software/tnb/db/dballocator/validation/SqlValidation.java +++ b/system-x/services/db/dballocator/src/main/java/software/tnb/db/dballocator/validation/SqlValidation.java @@ -1,5 +1,7 @@ package software.tnb.db.dballocator.validation; +import software.tnb.common.validation.Validation; + import org.jboss.qa.dballoc.api.allocator.entity.JaxbAllocation; import org.jboss.qa.dballoc.api.executor.SqlExecutor; import org.jboss.qa.dballoc.api.executor.SqlRequest; @@ -9,7 +11,7 @@ import java.util.Arrays; import java.util.List; -public class SqlValidation { +public class SqlValidation implements Validation { private JaxbAllocation allocation; private SqlExecutor executor; diff --git a/system-x/services/db/mongodb/src/main/java/software/tnb/db/mongodb/resource/local/LocalMongoDB.java b/system-x/services/db/mongodb/src/main/java/software/tnb/db/mongodb/resource/local/LocalMongoDB.java index 66c52237c..8570172c0 100644 --- a/system-x/services/db/mongodb/src/main/java/software/tnb/db/mongodb/resource/local/LocalMongoDB.java +++ b/system-x/services/db/mongodb/src/main/java/software/tnb/db/mongodb/resource/local/LocalMongoDB.java @@ -7,7 +7,6 @@ import org.slf4j.LoggerFactory; import com.google.auto.service.AutoService; -import com.mongodb.client.MongoClient; import com.mongodb.client.MongoClients; @AutoService(MongoDB.class) @@ -46,11 +45,6 @@ public void closeResources() { validation = null; } - @Override - protected MongoClient client() { - return client; - } - @Override public String replicaSetUrl() { return String.format("mongodb://%s:%s@%s:%d/%s", account().username(), account().password(), hostname(), diff --git a/system-x/services/db/mongodb/src/main/java/software/tnb/db/mongodb/resource/openshift/OpenshiftMongoDB.java b/system-x/services/db/mongodb/src/main/java/software/tnb/db/mongodb/resource/openshift/OpenshiftMongoDB.java index 2b9ffc545..8f49b4e0d 100644 --- a/system-x/services/db/mongodb/src/main/java/software/tnb/db/mongodb/resource/openshift/OpenshiftMongoDB.java +++ b/system-x/services/db/mongodb/src/main/java/software/tnb/db/mongodb/resource/openshift/OpenshiftMongoDB.java @@ -15,7 +15,6 @@ import org.slf4j.LoggerFactory; import com.google.auto.service.AutoService; -import com.mongodb.client.MongoClient; import com.mongodb.client.MongoClients; import com.mongodb.client.MongoDatabase; @@ -148,11 +147,6 @@ public String name() { return "mongodb"; } - @Override - protected MongoClient client() { - return client; - } - @Override public String replicaSetUrl() { return String.format("mongodb://%s:%s@%s:%d/%s", account().username(), account().password(), hostname(), DEFAULT_PORT, account().database()); diff --git a/system-x/services/db/mongodb/src/main/java/software/tnb/db/mongodb/service/MongoDB.java b/system-x/services/db/mongodb/src/main/java/software/tnb/db/mongodb/service/MongoDB.java index 20f1dd44d..71e3d7650 100644 --- a/system-x/services/db/mongodb/src/main/java/software/tnb/db/mongodb/service/MongoDB.java +++ b/system-x/services/db/mongodb/src/main/java/software/tnb/db/mongodb/service/MongoDB.java @@ -1,6 +1,5 @@ package software.tnb.db.mongodb.service; -import software.tnb.common.account.AccountFactory; import software.tnb.common.deployment.WithDockerImage; import software.tnb.common.service.Service; import software.tnb.db.mongodb.account.MongoDBAccount; @@ -13,23 +12,10 @@ import java.util.Map; -public abstract class MongoDB implements Service, WithDockerImage { +public abstract class MongoDB extends Service implements WithDockerImage { private static final Logger LOG = LoggerFactory.getLogger(MongoDB.class); public static final int DEFAULT_PORT = 27017; - private MongoDBAccount account; - protected MongoDBValidation validation; - protected MongoClient client; - - public MongoDBAccount account() { - if (account == null) { - account = AccountFactory.create(MongoDBAccount.class); - } - return account; - } - - protected abstract MongoClient client(); - public MongoDBValidation validation() { if (validation == null) { LOG.debug("Creating new MongoDB validation"); diff --git a/system-x/services/db/mongodb/src/main/java/software/tnb/db/mongodb/validation/MongoDBValidation.java b/system-x/services/db/mongodb/src/main/java/software/tnb/db/mongodb/validation/MongoDBValidation.java index 4e1f7afd1..22157ce87 100644 --- a/system-x/services/db/mongodb/src/main/java/software/tnb/db/mongodb/validation/MongoDBValidation.java +++ b/system-x/services/db/mongodb/src/main/java/software/tnb/db/mongodb/validation/MongoDBValidation.java @@ -1,5 +1,6 @@ package software.tnb.db.mongodb.validation; +import software.tnb.common.validation.Validation; import software.tnb.db.mongodb.account.MongoDBAccount; import org.bson.Document; @@ -18,7 +19,7 @@ import java.util.stream.Collectors; import java.util.stream.StreamSupport; -public class MongoDBValidation { +public class MongoDBValidation implements Validation { private static final Logger LOG = LoggerFactory.getLogger(MongoDBValidation.class); private final MongoClient client; diff --git a/system-x/services/elasticsearch/src/main/java/software/tnb/elasticsearch/service/Elasticsearch.java b/system-x/services/elasticsearch/src/main/java/software/tnb/elasticsearch/service/Elasticsearch.java index a689a9a0a..66b9d554f 100644 --- a/system-x/services/elasticsearch/src/main/java/software/tnb/elasticsearch/service/Elasticsearch.java +++ b/system-x/services/elasticsearch/src/main/java/software/tnb/elasticsearch/service/Elasticsearch.java @@ -22,15 +22,11 @@ import co.elastic.clients.transport.ElasticsearchTransport; import co.elastic.clients.transport.rest_client.RestClientTransport; -public abstract class Elasticsearch implements Service { +public abstract class Elasticsearch extends Service { private static final Logger LOG = LoggerFactory.getLogger(Elasticsearch.class); private static final String CLUSTER_NAME = "tnb-es"; private static final String ELASTICSEARCH_VERSION = "elasticsearch.version"; - protected ElasticsearchAccount account; - protected ElasticsearchClient client; - private ElasticsearchValidation validation; - protected ElasticsearchClient client() { final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); diff --git a/system-x/services/elasticsearch/src/main/java/software/tnb/elasticsearch/validation/ElasticsearchValidation.java b/system-x/services/elasticsearch/src/main/java/software/tnb/elasticsearch/validation/ElasticsearchValidation.java index 3a47dec2f..bf8bd2501 100644 --- a/system-x/services/elasticsearch/src/main/java/software/tnb/elasticsearch/validation/ElasticsearchValidation.java +++ b/system-x/services/elasticsearch/src/main/java/software/tnb/elasticsearch/validation/ElasticsearchValidation.java @@ -2,6 +2,8 @@ import static org.junit.jupiter.api.Assertions.fail; +import software.tnb.common.validation.Validation; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -22,7 +24,7 @@ import co.elastic.clients.elasticsearch.indices.DeleteIndexRequest; import co.elastic.clients.elasticsearch.indices.ExistsRequest; -public class ElasticsearchValidation { +public class ElasticsearchValidation implements Validation { private static final Logger LOG = LoggerFactory.getLogger(ElasticsearchValidation.class); private final ElasticsearchClient client; diff --git a/system-x/services/fhir/src/main/java/software/tnb/fhir/service/Fhir.java b/system-x/services/fhir/src/main/java/software/tnb/fhir/service/Fhir.java index 1ec2ede52..9083908bf 100644 --- a/system-x/services/fhir/src/main/java/software/tnb/fhir/service/Fhir.java +++ b/system-x/services/fhir/src/main/java/software/tnb/fhir/service/Fhir.java @@ -1,11 +1,14 @@ package software.tnb.fhir.service; +import software.tnb.common.account.NoAccount; +import software.tnb.common.client.NoClient; import software.tnb.common.deployment.WithDockerImage; import software.tnb.common.service.Service; +import software.tnb.common.validation.NoValidation; import java.util.Map; -public abstract class Fhir implements Service, WithDockerImage { +public abstract class Fhir extends Service implements WithDockerImage { public static final String FHIR_IMAGE = "quay.io/fuse_qe/hapi:v4.2.0"; public static final int PORT = 8080; diff --git a/system-x/services/filesystem/src/main/java/software/tnb/filesystem/service/FileSystem.java b/system-x/services/filesystem/src/main/java/software/tnb/filesystem/service/FileSystem.java index faedb5b5f..c11492ddc 100644 --- a/system-x/services/filesystem/src/main/java/software/tnb/filesystem/service/FileSystem.java +++ b/system-x/services/filesystem/src/main/java/software/tnb/filesystem/service/FileSystem.java @@ -1,10 +1,13 @@ package software.tnb.filesystem.service; +import software.tnb.common.account.NoAccount; +import software.tnb.common.client.NoClient; import software.tnb.common.service.Service; +import software.tnb.common.validation.NoValidation; import java.nio.file.Path; -public abstract class FileSystem implements Service { +public abstract class FileSystem extends Service { public abstract void setAppName(String app); public abstract String getFileContent(Path path); diff --git a/system-x/services/ftp/src/main/java/software/tnb/ftp/common/FileTransferService.java b/system-x/services/ftp/src/main/java/software/tnb/ftp/common/FileTransferService.java index fae7af104..52b0247a7 100644 --- a/system-x/services/ftp/src/main/java/software/tnb/ftp/common/FileTransferService.java +++ b/system-x/services/ftp/src/main/java/software/tnb/ftp/common/FileTransferService.java @@ -1,8 +1,6 @@ package software.tnb.ftp.common; -import software.tnb.common.service.Service; - -public interface FileTransferService extends Service { +public interface FileTransferService { String host(); int port(); diff --git a/system-x/services/ftp/src/main/java/software/tnb/ftp/common/FileTransferValidation.java b/system-x/services/ftp/src/main/java/software/tnb/ftp/common/FileTransferValidation.java index 898c4dc44..9649cd5ed 100644 --- a/system-x/services/ftp/src/main/java/software/tnb/ftp/common/FileTransferValidation.java +++ b/system-x/services/ftp/src/main/java/software/tnb/ftp/common/FileTransferValidation.java @@ -1,9 +1,11 @@ package software.tnb.ftp.common; +import software.tnb.common.validation.Validation; + import java.util.List; import java.util.Map; -public interface FileTransferValidation { +public interface FileTransferValidation extends Validation { void createFile(String fileName, String fileContent); String downloadFile(String fileName); diff --git a/system-x/services/ftp/src/main/java/software/tnb/ftp/ftp/service/FTP.java b/system-x/services/ftp/src/main/java/software/tnb/ftp/ftp/service/FTP.java index 34d96a308..33affafcc 100644 --- a/system-x/services/ftp/src/main/java/software/tnb/ftp/ftp/service/FTP.java +++ b/system-x/services/ftp/src/main/java/software/tnb/ftp/ftp/service/FTP.java @@ -1,7 +1,7 @@ package software.tnb.ftp.ftp.service; -import software.tnb.common.account.AccountFactory; import software.tnb.common.deployment.WithDockerImage; +import software.tnb.common.service.Service; import software.tnb.ftp.common.FileTransferService; import software.tnb.ftp.ftp.account.FTPAccount; import software.tnb.ftp.ftp.validation.FTPValidation; @@ -11,21 +11,9 @@ import java.util.Map; -public abstract class FTP implements FileTransferService, WithDockerImage { - private static final Logger LOG = LoggerFactory.getLogger(FTP.class); - - private FTPAccount account; - private FTPValidation validation; +public abstract class FTP extends Service implements FileTransferService, WithDockerImage { - protected abstract CustomFTPClient client(); - - @Override - public FTPAccount account() { - if (account == null) { - account = AccountFactory.create(FTPAccount.class); - } - return account; - } + private static final Logger LOG = LoggerFactory.getLogger(FTP.class); @Override public FTPValidation validation() { diff --git a/system-x/services/ftp/src/main/java/software/tnb/ftp/sftp/service/SFTP.java b/system-x/services/ftp/src/main/java/software/tnb/ftp/sftp/service/SFTP.java index 4fc228506..52451677c 100644 --- a/system-x/services/ftp/src/main/java/software/tnb/ftp/sftp/service/SFTP.java +++ b/system-x/services/ftp/src/main/java/software/tnb/ftp/sftp/service/SFTP.java @@ -1,7 +1,7 @@ package software.tnb.ftp.sftp.service; -import software.tnb.common.account.AccountFactory; import software.tnb.common.deployment.WithDockerImage; +import software.tnb.common.service.Service; import software.tnb.ftp.common.FileTransferService; import software.tnb.ftp.sftp.account.SFTPAccount; import software.tnb.ftp.sftp.validation.SFTPValidation; @@ -13,27 +13,14 @@ import net.schmizz.sshj.sftp.SFTPClient; -public abstract class SFTP implements FileTransferService, WithDockerImage { +public abstract class SFTP extends Service implements FileTransferService, WithDockerImage { private static final Logger LOG = LoggerFactory.getLogger(SFTP.class); - private SFTPAccount account; - private SFTPValidation validation; - - public abstract SFTPClient client(); - @Override public int port() { return 22; } - @Override - public SFTPAccount account() { - if (account == null) { - account = AccountFactory.create(SFTPAccount.class); - } - return account; - } - @Override public SFTPValidation validation() { if (validation == null) { diff --git a/system-x/services/google/api/common/src/main/java/software/tnb/google/api/common/service/GoogleAPIService.java b/system-x/services/google/api/common/src/main/java/software/tnb/google/api/common/service/GoogleAPIService.java index 447e4a3a6..a1ae92f20 100644 --- a/system-x/services/google/api/common/src/main/java/software/tnb/google/api/common/service/GoogleAPIService.java +++ b/system-x/services/google/api/common/src/main/java/software/tnb/google/api/common/service/GoogleAPIService.java @@ -1,10 +1,9 @@ package software.tnb.google.api.common.service; -import software.tnb.common.account.AccountFactory; import software.tnb.common.service.Service; -import software.tnb.common.service.Validation; import software.tnb.common.utils.FIPSUtils; import software.tnb.common.utils.HTTPUtils; +import software.tnb.common.validation.Validation; import software.tnb.google.api.common.account.GoogleAPIAccount; import org.junit.jupiter.api.extension.ExtensionContext; @@ -23,15 +22,12 @@ import java.io.IOException; -public abstract class GoogleAPIService implements Service { +public abstract class GoogleAPIService extends Service { protected static final Logger LOG = LoggerFactory.getLogger(GoogleAPIService.class); protected static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance(); protected final NetHttpTransport httpTransport; - protected GoogleAPIAccount account; - protected V validation; - protected GoogleAPIService() { try { if (FIPSUtils.isFipsEnabled()) { @@ -45,17 +41,6 @@ protected GoogleAPIService() { } } - public GoogleAPIAccount account() { - if (account == null) { - account = AccountFactory.create(GoogleAPIAccount.class); - } - return account; - } - - public V validation() { - return validation; - } - protected HttpCredentialsAdapter createCredentials() { GenericJson json = new GenericJson(); json.put("type", "authorized_user"); diff --git a/system-x/services/google/api/mail/src/main/java/software/tnb/google/mail/service/GoogleMail.java b/system-x/services/google/api/mail/src/main/java/software/tnb/google/mail/service/GoogleMail.java index 8d3de0b95..ae2a06ed2 100644 --- a/system-x/services/google/api/mail/src/main/java/software/tnb/google/mail/service/GoogleMail.java +++ b/system-x/services/google/api/mail/src/main/java/software/tnb/google/mail/service/GoogleMail.java @@ -9,7 +9,7 @@ import com.google.auto.service.AutoService; @AutoService(GoogleMail.class) -public class GoogleMail extends GoogleAPIService { +public class GoogleMail extends GoogleAPIService { private static final String APPLICATION_NAME = "tnb-system-x-google-mail"; @Override @@ -19,7 +19,10 @@ public void beforeAll(ExtensionContext extensionContext) throws Exception { } protected Gmail client() { - LOG.debug("Creating new Google Mail client"); - return new Gmail.Builder(httpTransport, JSON_FACTORY, createCredentials()).setApplicationName(APPLICATION_NAME).build(); + if (client == null) { + LOG.debug("Creating new Google Mail client"); + client = new Gmail.Builder(httpTransport, JSON_FACTORY, createCredentials()).setApplicationName(APPLICATION_NAME).build(); + } + return client; } } diff --git a/system-x/services/google/api/mail/src/main/java/software/tnb/google/mail/validation/GoogleMailValidation.java b/system-x/services/google/api/mail/src/main/java/software/tnb/google/mail/validation/GoogleMailValidation.java index b224313e3..f7e1ea436 100644 --- a/system-x/services/google/api/mail/src/main/java/software/tnb/google/mail/validation/GoogleMailValidation.java +++ b/system-x/services/google/api/mail/src/main/java/software/tnb/google/mail/validation/GoogleMailValidation.java @@ -1,6 +1,6 @@ package software.tnb.google.mail.validation; -import software.tnb.common.service.Validation; +import software.tnb.common.validation.Validation; import software.tnb.google.mail.validation.model.GoogleEmail; import org.slf4j.Logger; @@ -23,10 +23,6 @@ public GoogleMailValidation(Gmail client) { this.client = client; } - public Gmail x() { - return client; - } - public List getEmails(String query) { List emails = new ArrayList<>(); try { diff --git a/system-x/services/google/api/sheets/src/main/java/software/tnb/google/sheets/service/GoogleSheets.java b/system-x/services/google/api/sheets/src/main/java/software/tnb/google/sheets/service/GoogleSheets.java index edf98b826..d0342c8a3 100644 --- a/system-x/services/google/api/sheets/src/main/java/software/tnb/google/sheets/service/GoogleSheets.java +++ b/system-x/services/google/api/sheets/src/main/java/software/tnb/google/sheets/service/GoogleSheets.java @@ -9,12 +9,15 @@ import com.google.auto.service.AutoService; @AutoService(GoogleSheets.class) -public class GoogleSheets extends GoogleAPIService { +public class GoogleSheets extends GoogleAPIService { private static final String APPLICATION_NAME = "tnb-system-x-google-sheets"; protected Sheets client() { - LOG.debug("Creating new Google Sheets client"); - return new Sheets.Builder(httpTransport, JSON_FACTORY, createCredentials()).setApplicationName(APPLICATION_NAME).build(); + if (client == null) { + LOG.debug("Creating new Google Sheets client"); + client = new Sheets.Builder(httpTransport, JSON_FACTORY, createCredentials()).setApplicationName(APPLICATION_NAME).build(); + } + return client; } @Override diff --git a/system-x/services/google/api/sheets/src/main/java/software/tnb/google/sheets/validation/GoogleSheetsValidation.java b/system-x/services/google/api/sheets/src/main/java/software/tnb/google/sheets/validation/GoogleSheetsValidation.java index 208c07197..6793a7492 100644 --- a/system-x/services/google/api/sheets/src/main/java/software/tnb/google/sheets/validation/GoogleSheetsValidation.java +++ b/system-x/services/google/api/sheets/src/main/java/software/tnb/google/sheets/validation/GoogleSheetsValidation.java @@ -1,6 +1,6 @@ package software.tnb.google.sheets.validation; -import software.tnb.common.service.Validation; +import software.tnb.common.validation.Validation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/system-x/services/google/cloud/bigquery/src/main/java/software/tnb/google/cloud/bigquery/service/GoogleBigQuery.java b/system-x/services/google/cloud/bigquery/src/main/java/software/tnb/google/cloud/bigquery/service/GoogleBigQuery.java index 7b530798b..855b6c7f1 100644 --- a/system-x/services/google/cloud/bigquery/src/main/java/software/tnb/google/cloud/bigquery/service/GoogleBigQuery.java +++ b/system-x/services/google/cloud/bigquery/src/main/java/software/tnb/google/cloud/bigquery/service/GoogleBigQuery.java @@ -1,6 +1,5 @@ package software.tnb.google.cloud.bigquery.service; -import software.tnb.common.account.AccountFactory; import software.tnb.common.service.Service; import software.tnb.google.cloud.bigquery.validation.BigQueryValidation; import software.tnb.google.cloud.common.account.GoogleCloudAccount; @@ -23,30 +22,16 @@ import java.util.Base64; @AutoService(GoogleBigQuery.class) -public class GoogleBigQuery implements Service { - +public class GoogleBigQuery extends Service { private static final Logger LOG = LoggerFactory.getLogger(GoogleBigQuery.class); - private GoogleCloudAccount account; - private BigQueryValidation validation; - - private BigQuery client; - - public GoogleCloudAccount account() { - if (account == null) { - account = AccountFactory.create(GoogleCloudAccount.class); - } - return account; - } - - protected BigQuery client() throws IOException { + protected BigQuery client() { if (client == null) { LOG.debug("Creating new Google BigQuery client"); try { - client = - BigQueryOptions.newBuilder().setCredentials(credentialsProvider().getCredentials()).build().getService(); + client = BigQueryOptions.newBuilder().setCredentials(credentialsProvider().getCredentials()).build().getService(); } catch (Exception e) { - throw new RuntimeException("Unable to create new Google Storage client", e); + throw new RuntimeException("Unable to create new Google BigQuery client", e); } } return client; @@ -57,10 +42,6 @@ private CredentialsProvider credentialsProvider() throws IOException { return FixedCredentialsProvider.create(GoogleCredentials.fromStream(serviceAccountKey)); } - public BigQueryValidation validation() { - return validation; - } - @Override public void afterAll(ExtensionContext extensionContext) { } diff --git a/system-x/services/google/cloud/bigquery/src/main/java/software/tnb/google/cloud/bigquery/validation/BigQueryValidation.java b/system-x/services/google/cloud/bigquery/src/main/java/software/tnb/google/cloud/bigquery/validation/BigQueryValidation.java index ac5b7852b..861c40d3f 100644 --- a/system-x/services/google/cloud/bigquery/src/main/java/software/tnb/google/cloud/bigquery/validation/BigQueryValidation.java +++ b/system-x/services/google/cloud/bigquery/src/main/java/software/tnb/google/cloud/bigquery/validation/BigQueryValidation.java @@ -1,5 +1,7 @@ package software.tnb.google.cloud.bigquery.validation; +import software.tnb.common.validation.Validation; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -23,7 +25,7 @@ import java.util.stream.Collectors; import java.util.stream.StreamSupport; -public class BigQueryValidation { +public class BigQueryValidation implements Validation { private static final Logger LOG = LoggerFactory.getLogger(BigQueryValidation.class); private final BigQuery bigQuery; diff --git a/system-x/services/google/cloud/functions/src/main/java/software/tnb/google/cloud/functions/service/GoogleFunctions.java b/system-x/services/google/cloud/functions/src/main/java/software/tnb/google/cloud/functions/service/GoogleFunctions.java index dccb5cf67..1be4d71c3 100644 --- a/system-x/services/google/cloud/functions/src/main/java/software/tnb/google/cloud/functions/service/GoogleFunctions.java +++ b/system-x/services/google/cloud/functions/src/main/java/software/tnb/google/cloud/functions/service/GoogleFunctions.java @@ -1,6 +1,5 @@ package software.tnb.google.cloud.functions.service; -import software.tnb.common.account.AccountFactory; import software.tnb.common.service.Service; import software.tnb.common.service.ServiceFactory; import software.tnb.google.cloud.functions.account.GoogleFunctionsAccount; @@ -20,32 +19,21 @@ import io.fabric8.kubernetes.client.utils.Base64; @AutoService(GoogleFunctions.class) -public class GoogleFunctions implements Service { +public class GoogleFunctions extends Service { private final GoogleStorage storage = ServiceFactory.create(GoogleStorage.class); - private GoogleFunctionsAccount account; - private GoogleFunctionsValidation validation; - - public GoogleFunctionsAccount account() { - if (account == null) { - account = AccountFactory.create(GoogleFunctionsAccount.class); - } - return account; - } - protected CloudFunctionsServiceClient client() { - try { - String decodedJson = new String(Base64.decode(account().serviceAccountKey())); - return CloudFunctionsServiceClient.create(CloudFunctionsServiceSettings.newBuilder() - .setCredentialsProvider(FixedCredentialsProvider.create(ServiceAccountCredentials - .fromStream(IOUtils.toInputStream(decodedJson, "UTF-8")))).build()); - } catch (Exception e) { - throw new RuntimeException("Unable to create new client", e); + if (client == null) { + try { + String decodedJson = new String(Base64.decode(account().serviceAccountKey())); + return CloudFunctionsServiceClient.create(CloudFunctionsServiceSettings.newBuilder() + .setCredentialsProvider(FixedCredentialsProvider.create(ServiceAccountCredentials + .fromStream(IOUtils.toInputStream(decodedJson, "UTF-8")))).build()); + } catch (Exception e) { + throw new RuntimeException("Unable to create new client", e); + } } - } - - public GoogleFunctionsValidation validation() { - return validation; + return client; } @Override diff --git a/system-x/services/google/cloud/functions/src/main/java/software/tnb/google/cloud/functions/validation/GoogleFunctionsValidation.java b/system-x/services/google/cloud/functions/src/main/java/software/tnb/google/cloud/functions/validation/GoogleFunctionsValidation.java index 9208db344..37903b038 100644 --- a/system-x/services/google/cloud/functions/src/main/java/software/tnb/google/cloud/functions/validation/GoogleFunctionsValidation.java +++ b/system-x/services/google/cloud/functions/src/main/java/software/tnb/google/cloud/functions/validation/GoogleFunctionsValidation.java @@ -1,7 +1,7 @@ package software.tnb.google.cloud.functions.validation; -import software.tnb.common.service.Validation; import software.tnb.common.utils.WaitUtils; +import software.tnb.common.validation.Validation; import software.tnb.google.cloud.functions.account.GoogleFunctionsAccount; import software.tnb.google.storage.service.GoogleStorage; diff --git a/system-x/services/google/cloud/pubsub/src/main/java/software/tnb/google/cloud/pubsub/client/PubSubClients.java b/system-x/services/google/cloud/pubsub/src/main/java/software/tnb/google/cloud/pubsub/client/PubSubClients.java new file mode 100644 index 000000000..e140c5125 --- /dev/null +++ b/system-x/services/google/cloud/pubsub/src/main/java/software/tnb/google/cloud/pubsub/client/PubSubClients.java @@ -0,0 +1,83 @@ +package software.tnb.google.cloud.pubsub.client; + +import software.tnb.google.cloud.common.account.GoogleCloudAccount; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.api.gax.core.CredentialsProvider; +import com.google.api.gax.core.FixedCredentialsProvider; +import com.google.auth.oauth2.GoogleCredentials; +import com.google.cloud.pubsub.v1.SubscriptionAdminClient; +import com.google.cloud.pubsub.v1.SubscriptionAdminSettings; +import com.google.cloud.pubsub.v1.TopicAdminClient; +import com.google.cloud.pubsub.v1.TopicAdminSettings; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Base64; + +public class PubSubClients { + private static final Logger LOG = LoggerFactory.getLogger(PubSubClients.class); + + private final GoogleCloudAccount account; + private final TopicAdminClient topicAdminClient; + private final SubscriptionAdminClient subscriptionAdminClient; + + public PubSubClients(GoogleCloudAccount account) { + this.account = account; + topicAdminClient = newTopicAdminClient(); + subscriptionAdminClient = newSubscriptionAdminClient(); + } + + public TopicAdminClient topicAdminClient() { + return topicAdminClient; + } + + public SubscriptionAdminClient subscriptionAdminClient() { + return subscriptionAdminClient; + } + + private TopicAdminClient newTopicAdminClient() { + LOG.debug("Creating new Google cloud topic admin client"); + + try { + TopicAdminSettings topicAdminSettings = + TopicAdminSettings.newBuilder().setCredentialsProvider(credentialsProvider()).build(); + return TopicAdminClient.create(topicAdminSettings); + } catch (IOException e) { + throw new RuntimeException("Unable to create topic admin client: ", e); + } + } + + private SubscriptionAdminClient newSubscriptionAdminClient() { + LOG.debug("Creating new Google subscription admin client"); + + try { + SubscriptionAdminSettings subscriptionAdminSettings = + SubscriptionAdminSettings.newBuilder().setCredentialsProvider(credentialsProvider()).build(); + return SubscriptionAdminClient.create(subscriptionAdminSettings); + } catch (IOException e) { + throw new RuntimeException("Unable to create subscription admin client: ", e); + } + } + + public CredentialsProvider credentialsProvider() { + InputStream serviceAccountKey = new ByteArrayInputStream(Base64.getDecoder().decode(account.serviceAccountKey())); + try { + return FixedCredentialsProvider.create(GoogleCredentials.fromStream(serviceAccountKey)); + } catch (IOException e) { + throw new RuntimeException("Unable to extract service account key: ", e); + } + } + + public void close() { + if (topicAdminClient != null) { + topicAdminClient.close(); + } + if (subscriptionAdminClient != null) { + subscriptionAdminClient.close(); + } + } +} diff --git a/system-x/services/google/cloud/pubsub/src/main/java/software/tnb/google/cloud/pubsub/service/GooglePubSub.java b/system-x/services/google/cloud/pubsub/src/main/java/software/tnb/google/cloud/pubsub/service/GooglePubSub.java index 5ff66f3c5..68f22e1a2 100644 --- a/system-x/services/google/cloud/pubsub/src/main/java/software/tnb/google/cloud/pubsub/service/GooglePubSub.java +++ b/system-x/services/google/cloud/pubsub/src/main/java/software/tnb/google/cloud/pubsub/service/GooglePubSub.java @@ -1,8 +1,8 @@ package software.tnb.google.cloud.pubsub.service; -import software.tnb.common.account.AccountFactory; import software.tnb.common.service.Service; import software.tnb.google.cloud.common.account.GoogleCloudAccount; +import software.tnb.google.cloud.pubsub.client.PubSubClients; import software.tnb.google.cloud.pubsub.validation.PubSubValidation; import org.junit.jupiter.api.extension.ExtensionContext; @@ -10,76 +10,30 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.api.gax.core.CredentialsProvider; -import com.google.api.gax.core.FixedCredentialsProvider; -import com.google.auth.oauth2.GoogleCredentials; import com.google.auto.service.AutoService; -import com.google.cloud.pubsub.v1.SubscriptionAdminClient; -import com.google.cloud.pubsub.v1.SubscriptionAdminSettings; -import com.google.cloud.pubsub.v1.TopicAdminClient; -import com.google.cloud.pubsub.v1.TopicAdminSettings; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.Base64; @AutoService(GooglePubSub.class) -public class GooglePubSub implements Service { - +public class GooglePubSub extends Service { private static final Logger LOG = LoggerFactory.getLogger(GooglePubSub.class); - private GoogleCloudAccount account; - private PubSubValidation validation; - - private TopicAdminClient topicAdminClient; - private SubscriptionAdminClient subscriptionAdminClient; - - public GoogleCloudAccount account() { - if (account == null) { - account = AccountFactory.create(GoogleCloudAccount.class); - } - return account; - } - - protected TopicAdminClient topicAdminClient() throws IOException { - LOG.debug("Creating new Google cloud topic admin client"); - - TopicAdminSettings topicAdminSettings = - TopicAdminSettings.newBuilder().setCredentialsProvider(credentialsProvider()).build(); - return TopicAdminClient.create(topicAdminSettings); - } - - protected SubscriptionAdminClient subscriptionAdminClient() throws IOException { - LOG.debug("Creating new Google subscription admin client"); - - SubscriptionAdminSettings subscriptionAdminSettings = - SubscriptionAdminSettings.newBuilder().setCredentialsProvider(credentialsProvider()).build(); - return SubscriptionAdminClient.create(subscriptionAdminSettings); - } - - private CredentialsProvider credentialsProvider() throws IOException { - InputStream serviceAccountKey = new ByteArrayInputStream(Base64.getDecoder().decode(account().serviceAccountKey())); - return FixedCredentialsProvider.create(GoogleCredentials.fromStream(serviceAccountKey)); - } - - public PubSubValidation validation() { - return validation; - } - @Override public void afterAll(ExtensionContext extensionContext) { - topicAdminClient.close(); - subscriptionAdminClient.close(); + if (client != null) { + client.close(); + } } @Override public void beforeAll(ExtensionContext extensionContext) throws Exception { LOG.debug("Creating new Google PubSub validation"); + validation = new PubSubValidation(client(), account().projectId()); + } - topicAdminClient = topicAdminClient(); - subscriptionAdminClient = subscriptionAdminClient(); - - validation = new PubSubValidation(topicAdminClient, subscriptionAdminClient, credentialsProvider(), account().projectId()); + @Override + protected PubSubClients client() { + if (client == null) { + client = new PubSubClients(account()); + } + return client; } } diff --git a/system-x/services/google/cloud/pubsub/src/main/java/software/tnb/google/cloud/pubsub/validation/PubSubValidation.java b/system-x/services/google/cloud/pubsub/src/main/java/software/tnb/google/cloud/pubsub/validation/PubSubValidation.java index ca54ece22..30f1cc9b4 100644 --- a/system-x/services/google/cloud/pubsub/src/main/java/software/tnb/google/cloud/pubsub/validation/PubSubValidation.java +++ b/system-x/services/google/cloud/pubsub/src/main/java/software/tnb/google/cloud/pubsub/validation/PubSubValidation.java @@ -1,16 +1,16 @@ package software.tnb.google.cloud.pubsub.validation; +import software.tnb.common.validation.Validation; +import software.tnb.google.cloud.pubsub.client.PubSubClients; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.api.core.ApiFuture; -import com.google.api.gax.core.CredentialsProvider; import com.google.cloud.pubsub.v1.AckReplyConsumer; import com.google.cloud.pubsub.v1.MessageReceiver; import com.google.cloud.pubsub.v1.Publisher; import com.google.cloud.pubsub.v1.Subscriber; -import com.google.cloud.pubsub.v1.SubscriptionAdminClient; -import com.google.cloud.pubsub.v1.TopicAdminClient; import com.google.protobuf.ByteString; import com.google.pubsub.v1.ProjectSubscriptionName; import com.google.pubsub.v1.PubsubMessage; @@ -25,31 +25,26 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; -public class PubSubValidation { +public class PubSubValidation implements Validation { private static final Logger LOG = LoggerFactory.getLogger(PubSubValidation.class); - private TopicAdminClient topicAdminClient; - private SubscriptionAdminClient subscriptionAdminClient; - private String projectId; - private CredentialsProvider credsProvider; + private final PubSubClients clients; + private final String projectId; - public PubSubValidation(TopicAdminClient topicAdminClient, SubscriptionAdminClient subscriptionAdminClient, CredentialsProvider credsProvider, - String projectId) { - this.topicAdminClient = topicAdminClient; - this.subscriptionAdminClient = subscriptionAdminClient; + public PubSubValidation(PubSubClients clients, String projectId) { + this.clients = clients; this.projectId = projectId; - this.credsProvider = credsProvider; } public Topic createNewTopic(String topicId) { - Topic topic = topicAdminClient.createTopic(TopicName.of(projectId, topicId)); + Topic topic = clients.topicAdminClient().createTopic(TopicName.of(projectId, topicId)); LOG.info("Created google pubsub topic: " + topic.getName()); return topic; } public void deleteTopic(String topicId) { - topicAdminClient.deleteTopic(TopicName.of(projectId, topicId)); + clients.topicAdminClient().deleteTopic(TopicName.of(projectId, topicId)); LOG.info("Deleted google pubsub topic: " + topicId); } @@ -57,13 +52,14 @@ public Subscription createNewSubscription(String subscriptionId, String topicId) SubscriptionName subscriptionName = SubscriptionName.of(projectId, subscriptionId); TopicName topicName = TopicName.of(projectId, topicId); - Subscription subscription = subscriptionAdminClient.createSubscription(subscriptionName, topicName, PushConfig.getDefaultInstance(), 10); + Subscription subscription = + clients.subscriptionAdminClient().createSubscription(subscriptionName, topicName, PushConfig.getDefaultInstance(), 10); LOG.info("Created google pubsub subscription: " + subscription.getName()); return subscription; } public void deleteSubscription(String subscriptionId) { - subscriptionAdminClient.deleteSubscription(SubscriptionName.of(projectId, subscriptionId)); + clients.subscriptionAdminClient().deleteSubscription(SubscriptionName.of(projectId, subscriptionId)); LOG.info("Deleted google pubsub subscription: " + subscriptionId); } @@ -71,7 +67,7 @@ public void publishMessage(String topicId, String message) { Publisher publisher = null; try { // Create a publisher instance with default settings bound to the topic - publisher = Publisher.newBuilder(TopicName.of(projectId, topicId)).setCredentialsProvider(credsProvider).build(); + publisher = Publisher.newBuilder(TopicName.of(projectId, topicId)).setCredentialsProvider(clients.credentialsProvider()).build(); ByteString data = ByteString.copyFromUtf8(message); PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build(); @@ -118,7 +114,7 @@ public List receiveMessageFor(int seconds, String subscriptionId) Subscriber subscriber = null; try { - subscriber = Subscriber.newBuilder(subscriptionName, receiver).setCredentialsProvider(credsProvider).build(); + subscriber = Subscriber.newBuilder(subscriptionName, receiver).setCredentialsProvider(clients.credentialsProvider()).build(); // Start the subscriber. subscriber.startAsync().awaitRunning(); LOG.info("Listening for messages on {}", subscriptionName.toString()); diff --git a/system-x/services/google/cloud/storage/src/main/java/software/tnb/google/storage/service/GoogleStorage.java b/system-x/services/google/cloud/storage/src/main/java/software/tnb/google/storage/service/GoogleStorage.java index 8cbcc7d35..0293f9dbf 100644 --- a/system-x/services/google/cloud/storage/src/main/java/software/tnb/google/storage/service/GoogleStorage.java +++ b/system-x/services/google/cloud/storage/src/main/java/software/tnb/google/storage/service/GoogleStorage.java @@ -1,6 +1,5 @@ package software.tnb.google.storage.service; -import software.tnb.common.account.AccountFactory; import software.tnb.common.service.Service; import software.tnb.google.cloud.common.account.GoogleCloudAccount; import software.tnb.google.storage.validation.GoogleStorageValidation; @@ -19,20 +18,9 @@ import io.fabric8.kubernetes.client.utils.Base64; @AutoService(GoogleStorage.class) -public class GoogleStorage implements Service { +public class GoogleStorage extends Service { private static final Logger LOG = LoggerFactory.getLogger(GoogleStorage.class); - private GoogleCloudAccount account; - private Storage client; - private GoogleStorageValidation validation; - - public GoogleCloudAccount account() { - if (account == null) { - account = AccountFactory.create(GoogleCloudAccount.class); - } - return account; - } - protected Storage client() { if (client == null) { LOG.debug("Creating new Google Storage client"); @@ -47,10 +35,6 @@ protected Storage client() { return client; } - public GoogleStorageValidation validation() { - return validation; - } - @Override public void afterAll(ExtensionContext extensionContext) throws Exception { diff --git a/system-x/services/google/cloud/storage/src/main/java/software/tnb/google/storage/validation/GoogleStorageValidation.java b/system-x/services/google/cloud/storage/src/main/java/software/tnb/google/storage/validation/GoogleStorageValidation.java index 0564ed1ee..55af16bbb 100644 --- a/system-x/services/google/cloud/storage/src/main/java/software/tnb/google/storage/validation/GoogleStorageValidation.java +++ b/system-x/services/google/cloud/storage/src/main/java/software/tnb/google/storage/validation/GoogleStorageValidation.java @@ -1,6 +1,7 @@ package software.tnb.google.storage.validation; import software.tnb.common.utils.WaitUtils; +import software.tnb.common.validation.Validation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -18,7 +19,7 @@ import java.util.List; import java.util.stream.StreamSupport; -public class GoogleStorageValidation { +public class GoogleStorageValidation implements Validation { private static final Logger LOG = LoggerFactory.getLogger(GoogleStorageValidation.class); private final Storage client; diff --git a/system-x/services/horreum/src/main/java/software/tnb/horreum/service/Horreum.java b/system-x/services/horreum/src/main/java/software/tnb/horreum/service/Horreum.java index 703b55621..3716b09d8 100644 --- a/system-x/services/horreum/src/main/java/software/tnb/horreum/service/Horreum.java +++ b/system-x/services/horreum/src/main/java/software/tnb/horreum/service/Horreum.java @@ -1,6 +1,6 @@ package software.tnb.horreum.service; -import software.tnb.common.account.AccountFactory; +import software.tnb.common.client.NoClient; import software.tnb.common.service.Service; import software.tnb.horreum.account.HorreumAccount; import software.tnb.horreum.validation.HorreumValidation; @@ -10,17 +10,10 @@ import com.google.auto.service.AutoService; @AutoService(Horreum.class) -public class Horreum implements Service { - - private HorreumValidation validation; - - public HorreumValidation validation() { - return validation; - } - +public class Horreum extends Service { @Override public void beforeAll(ExtensionContext context) throws Exception { - validation = new HorreumValidation(AccountFactory.create(HorreumAccount.class)); + validation = new HorreumValidation(account()); } @Override diff --git a/system-x/services/horreum/src/main/java/software/tnb/horreum/validation/HorreumValidation.java b/system-x/services/horreum/src/main/java/software/tnb/horreum/validation/HorreumValidation.java index 119367b5e..2e405202b 100644 --- a/system-x/services/horreum/src/main/java/software/tnb/horreum/validation/HorreumValidation.java +++ b/system-x/services/horreum/src/main/java/software/tnb/horreum/validation/HorreumValidation.java @@ -2,6 +2,7 @@ import software.tnb.common.utils.HTTPUtils; import software.tnb.common.utils.HTTPUtils.Response; +import software.tnb.common.validation.Validation; import software.tnb.horreum.account.HorreumAccount; import software.tnb.horreum.configuration.HorreumConfiguration; import software.tnb.horreum.validation.generated.ApiClient; @@ -25,7 +26,7 @@ import okhttp3.MediaType; import okhttp3.RequestBody; -public class HorreumValidation { +public class HorreumValidation implements Validation { public static final MediaType FORM_URLENC = MediaType.get("application/x-www-form-urlencoded; charset=utf-8"); diff --git a/system-x/services/http/src/main/java/software/tnb/http/service/HTTP.java b/system-x/services/http/src/main/java/software/tnb/http/service/HTTP.java index 833cb0d34..87d148995 100644 --- a/system-x/services/http/src/main/java/software/tnb/http/service/HTTP.java +++ b/system-x/services/http/src/main/java/software/tnb/http/service/HTTP.java @@ -1,13 +1,16 @@ package software.tnb.http.service; +import software.tnb.common.account.NoAccount; +import software.tnb.common.client.NoClient; import software.tnb.common.deployment.WithDockerImage; import software.tnb.common.service.Service; +import software.tnb.common.validation.NoValidation; import java.io.InputStream; import java.security.cert.CertificateFactory; import java.security.cert.X509Certificate; -public abstract class HTTP implements Service, WithDockerImage { +public abstract class HTTP extends Service implements WithDockerImage { public static final int HTTP_PORT = 8080; public static final int HTTPS_PORT = 8443; diff --git a/system-x/services/hyperfoil/src/main/java/software/tnb/hyperfoil/resource/openshift/OpenshiftHyperfoil.java b/system-x/services/hyperfoil/src/main/java/software/tnb/hyperfoil/resource/openshift/OpenshiftHyperfoil.java index 846ef47cb..94196f98b 100644 --- a/system-x/services/hyperfoil/src/main/java/software/tnb/hyperfoil/resource/openshift/OpenshiftHyperfoil.java +++ b/system-x/services/hyperfoil/src/main/java/software/tnb/hyperfoil/resource/openshift/OpenshiftHyperfoil.java @@ -61,7 +61,7 @@ public void undeploy() { public void openResources() { WaitUtils.waitFor(() -> { try { - return getValidation().getDefaultApi().openApiWithHttpInfo().getStatusCode() == 200; + return validation().getDefaultApi().openApiWithHttpInfo().getStatusCode() == 200; } catch (ApiException e) { return false; } diff --git a/system-x/services/hyperfoil/src/main/java/software/tnb/hyperfoil/service/Hyperfoil.java b/system-x/services/hyperfoil/src/main/java/software/tnb/hyperfoil/service/Hyperfoil.java index 93ffbf409..aeb5c137b 100644 --- a/system-x/services/hyperfoil/src/main/java/software/tnb/hyperfoil/service/Hyperfoil.java +++ b/system-x/services/hyperfoil/src/main/java/software/tnb/hyperfoil/service/Hyperfoil.java @@ -1,14 +1,16 @@ package software.tnb.hyperfoil.service; +import software.tnb.common.account.NoAccount; +import software.tnb.common.client.NoClient; import software.tnb.common.service.Service; import software.tnb.hyperfoil.validation.HyperfoilValidation; -public abstract class Hyperfoil implements Service { +public abstract class Hyperfoil extends Service { public abstract String connection(); public abstract String hyperfoilUrl(); - public HyperfoilValidation getValidation() { + public HyperfoilValidation validation() { return new HyperfoilValidation(connection()); } diff --git a/system-x/services/hyperfoil/src/main/java/software/tnb/hyperfoil/validation/HyperfoilValidation.java b/system-x/services/hyperfoil/src/main/java/software/tnb/hyperfoil/validation/HyperfoilValidation.java index 1eeb9b79e..832212c8c 100644 --- a/system-x/services/hyperfoil/src/main/java/software/tnb/hyperfoil/validation/HyperfoilValidation.java +++ b/system-x/services/hyperfoil/src/main/java/software/tnb/hyperfoil/validation/HyperfoilValidation.java @@ -1,6 +1,7 @@ package software.tnb.hyperfoil.validation; import software.tnb.common.utils.HTTPUtils; +import software.tnb.common.validation.Validation; import software.tnb.hyperfoil.service.HyperfoilConfiguration; import software.tnb.hyperfoil.validation.generated.ApiClient; import software.tnb.hyperfoil.validation.generated.ApiException; @@ -35,7 +36,7 @@ import java.util.regex.Pattern; import java.util.stream.Collectors; -public class HyperfoilValidation { +public class HyperfoilValidation implements Validation { private static final Logger LOG = LoggerFactory.getLogger(HyperfoilValidation.class); private static final Long WAIT_BENCHMARK_SLEEP_TIME = 10000L; private static final ObjectMapper yamlMapper = new YAMLMapper(); diff --git a/system-x/services/infinispan/src/main/java/software/tnb/infinispan/resource/account/InfinispanAccount.java b/system-x/services/infinispan/src/main/java/software/tnb/infinispan/account/InfinispanAccount.java similarity index 91% rename from system-x/services/infinispan/src/main/java/software/tnb/infinispan/resource/account/InfinispanAccount.java rename to system-x/services/infinispan/src/main/java/software/tnb/infinispan/account/InfinispanAccount.java index 1f4f83d87..a1bf930c1 100644 --- a/system-x/services/infinispan/src/main/java/software/tnb/infinispan/resource/account/InfinispanAccount.java +++ b/system-x/services/infinispan/src/main/java/software/tnb/infinispan/account/InfinispanAccount.java @@ -1,4 +1,4 @@ -package software.tnb.infinispan.resource.account; +package software.tnb.infinispan.account; import software.tnb.common.account.Account; diff --git a/system-x/services/infinispan/src/main/java/software/tnb/infinispan/service/Infinispan.java b/system-x/services/infinispan/src/main/java/software/tnb/infinispan/service/Infinispan.java index 39e626ea9..7e4dd4860 100644 --- a/system-x/services/infinispan/src/main/java/software/tnb/infinispan/service/Infinispan.java +++ b/system-x/services/infinispan/src/main/java/software/tnb/infinispan/service/Infinispan.java @@ -1,13 +1,15 @@ package software.tnb.infinispan.service; import software.tnb.common.account.AccountFactory; +import software.tnb.common.client.NoClient; import software.tnb.common.deployment.WithDockerImage; import software.tnb.common.service.Service; -import software.tnb.infinispan.resource.account.InfinispanAccount; +import software.tnb.common.validation.NoValidation; +import software.tnb.infinispan.account.InfinispanAccount; import java.util.Map; -public abstract class Infinispan implements Service, WithDockerImage { +public abstract class Infinispan extends Service implements WithDockerImage { public static final int PORT = 11222; public abstract int getPortMapping(); diff --git a/system-x/services/jaeger/src/main/java/software/tnb/jaeger/resource/local/LocalJaeger.java b/system-x/services/jaeger/src/main/java/software/tnb/jaeger/resource/local/LocalJaeger.java index 9164ba349..5baa79d95 100644 --- a/system-x/services/jaeger/src/main/java/software/tnb/jaeger/resource/local/LocalJaeger.java +++ b/system-x/services/jaeger/src/main/java/software/tnb/jaeger/resource/local/LocalJaeger.java @@ -2,10 +2,10 @@ import software.tnb.common.deployment.Deployable; import software.tnb.common.deployment.WithDockerImage; +import software.tnb.jaeger.client.JaegerClient; import software.tnb.jaeger.client.UnauthenticatedJaegerClient; import software.tnb.jaeger.service.Jaeger; import software.tnb.jaeger.service.configuration.JaegerConfiguration; -import software.tnb.jaeger.validation.JaegerValidation; import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; @@ -65,8 +65,8 @@ public String getExternalUrl() { } @Override - protected JaegerValidation getClientBasedValidation() { - return new JaegerValidation(new UnauthenticatedJaegerClient(getQueryUrl(JaegerConfiguration.QueryPort.HTTP))); + protected JaegerClient client() { + return new UnauthenticatedJaegerClient(getQueryUrl(JaegerConfiguration.QueryPort.HTTP)); } @NotNull diff --git a/system-x/services/jaeger/src/main/java/software/tnb/jaeger/resource/openshift/OpenshiftJaeger.java b/system-x/services/jaeger/src/main/java/software/tnb/jaeger/resource/openshift/OpenshiftJaeger.java index 96f1276bb..3aaa6ed51 100644 --- a/system-x/services/jaeger/src/main/java/software/tnb/jaeger/resource/openshift/OpenshiftJaeger.java +++ b/system-x/services/jaeger/src/main/java/software/tnb/jaeger/resource/openshift/OpenshiftJaeger.java @@ -6,10 +6,10 @@ import software.tnb.common.deployment.WithExternalHostname; import software.tnb.common.deployment.WithOperatorHub; import software.tnb.common.openshift.OpenshiftClient; +import software.tnb.jaeger.client.JaegerClient; import software.tnb.jaeger.client.UnauthenticatedJaegerClient; import software.tnb.jaeger.service.Jaeger; import software.tnb.jaeger.service.configuration.JaegerConfiguration; -import software.tnb.jaeger.validation.JaegerValidation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -110,8 +110,8 @@ public String getExternalUrl() { } @Override - protected JaegerValidation getClientBasedValidation() { - return new JaegerValidation(new UnauthenticatedJaegerClient(getExternalUrl())); + protected JaegerClient client() { + return new UnauthenticatedJaegerClient(getExternalUrl()); } @Override diff --git a/system-x/services/jaeger/src/main/java/software/tnb/jaeger/service/Jaeger.java b/system-x/services/jaeger/src/main/java/software/tnb/jaeger/service/Jaeger.java index ebd2c19c0..853c2f1ec 100644 --- a/system-x/services/jaeger/src/main/java/software/tnb/jaeger/service/Jaeger.java +++ b/system-x/services/jaeger/src/main/java/software/tnb/jaeger/service/Jaeger.java @@ -1,7 +1,8 @@ package software.tnb.jaeger.service; +import software.tnb.common.account.NoAccount; import software.tnb.common.service.ConfigurableService; -import software.tnb.common.service.Service; +import software.tnb.jaeger.client.JaegerClient; import software.tnb.jaeger.service.configuration.JaegerConfiguration; import software.tnb.jaeger.validation.JaegerValidation; @@ -11,7 +12,7 @@ import java.util.Map; import java.util.Optional; -public abstract class Jaeger extends ConfigurableService implements Service { +public abstract class Jaeger extends ConfigurableService { public abstract String getLog(); @@ -21,8 +22,6 @@ public abstract class Jaeger extends ConfigurableService im public abstract String getExternalUrl(); - protected abstract JaegerValidation getClientBasedValidation(); - protected JaegerValidation validation; @Override @@ -42,6 +41,6 @@ protected Map env() { } public JaegerValidation validation() { - return Optional.ofNullable(validation).orElseGet(() -> validation = getClientBasedValidation()); + return Optional.ofNullable(validation).orElseGet(() -> validation = new JaegerValidation(client())); } } diff --git a/system-x/services/jaeger/src/main/java/software/tnb/jaeger/validation/JaegerValidation.java b/system-x/services/jaeger/src/main/java/software/tnb/jaeger/validation/JaegerValidation.java index d7d7d87b5..a04101710 100644 --- a/system-x/services/jaeger/src/main/java/software/tnb/jaeger/validation/JaegerValidation.java +++ b/system-x/services/jaeger/src/main/java/software/tnb/jaeger/validation/JaegerValidation.java @@ -1,12 +1,13 @@ package software.tnb.jaeger.validation; +import software.tnb.common.validation.Validation; import software.tnb.jaeger.client.JaegerClient; import software.tnb.jaeger.validation.model.Span; import java.util.List; import java.util.Map; -public class JaegerValidation { +public class JaegerValidation implements Validation { private final JaegerClient client; diff --git a/system-x/services/jira/src/main/java/software/tnb/jira/service/Jira.java b/system-x/services/jira/src/main/java/software/tnb/jira/service/Jira.java index c320c62b2..dac2815bd 100644 --- a/system-x/services/jira/src/main/java/software/tnb/jira/service/Jira.java +++ b/system-x/services/jira/src/main/java/software/tnb/jira/service/Jira.java @@ -1,6 +1,5 @@ package software.tnb.jira.service; -import software.tnb.common.account.AccountFactory; import software.tnb.common.service.Service; import software.tnb.jira.account.JiraAccount; import software.tnb.jira.validation.JiraValidation; @@ -15,23 +14,11 @@ import com.google.auto.service.AutoService; @AutoService(Jira.class) -public class Jira implements Service { +public class Jira extends Service { private static final Logger LOG = LoggerFactory.getLogger(Jira.class); - private JiraAccount account; - private ApiClient client; - private JiraValidation validation; - - public JiraAccount account() { - if (account == null) { - LOG.debug("Creating new Jira account"); - account = AccountFactory.create(JiraAccount.class); - } - return account; - } - + @Override protected ApiClient client() { - if (client == null) { LOG.debug("Creating new JiraRest client"); @@ -43,10 +30,6 @@ protected ApiClient client() { return client; } - public JiraValidation validation() { - return validation; - } - @Override public void afterAll(ExtensionContext context) throws Exception { } diff --git a/system-x/services/jira/src/main/java/software/tnb/jira/validation/JiraValidation.java b/system-x/services/jira/src/main/java/software/tnb/jira/validation/JiraValidation.java index ca58cdb58..4e2f68d0b 100644 --- a/system-x/services/jira/src/main/java/software/tnb/jira/validation/JiraValidation.java +++ b/system-x/services/jira/src/main/java/software/tnb/jira/validation/JiraValidation.java @@ -1,5 +1,6 @@ package software.tnb.jira.validation; +import software.tnb.common.validation.Validation; import software.tnb.jira.validation.generated.ApiClient; import software.tnb.jira.validation.generated.ApiException; import software.tnb.jira.validation.generated.api.IssueCommentsApi; @@ -26,7 +27,7 @@ import java.util.Objects; import java.util.stream.Collectors; -public class JiraValidation { +public class JiraValidation implements Validation { private static final Logger LOG = LoggerFactory.getLogger(JiraValidation.class); private final ApiClient client; diff --git a/system-x/services/jms/amq/src/main/java/software/tnb/jms/amq/resource/local/LocalAMQBroker.java b/system-x/services/jms/amq/src/main/java/software/tnb/jms/amq/resource/local/LocalAMQBroker.java index 25058906c..5b446bfe3 100644 --- a/system-x/services/jms/amq/src/main/java/software/tnb/jms/amq/resource/local/LocalAMQBroker.java +++ b/system-x/services/jms/amq/src/main/java/software/tnb/jms/amq/resource/local/LocalAMQBroker.java @@ -39,14 +39,14 @@ public void undeploy() { @Override public void openResources() { - connection = createConnection(); + client = createConnection(); } @Override public void closeResources() { validation = null; try { - connection.close(); + client.close(); } catch (JMSException e) { throw new RuntimeException("Can't close JMS connection", e); } diff --git a/system-x/services/jms/amq/src/main/java/software/tnb/jms/amq/resource/openshift/OpenshiftAMQBroker.java b/system-x/services/jms/amq/src/main/java/software/tnb/jms/amq/resource/openshift/OpenshiftAMQBroker.java index 22083952c..b4d7c1e23 100644 --- a/system-x/services/jms/amq/src/main/java/software/tnb/jms/amq/resource/openshift/OpenshiftAMQBroker.java +++ b/system-x/services/jms/amq/src/main/java/software/tnb/jms/amq/resource/openshift/OpenshiftAMQBroker.java @@ -108,14 +108,14 @@ public void undeploy() { @Override public void openResources() { - connection = createConnection(); + client = createConnection(); } @Override public void closeResources() { validation = null; try { - connection.close(); + client.close(); } catch (JMSException e) { throw new RuntimeException("Can't close JMS connection"); } diff --git a/system-x/services/jms/amq/src/main/java/software/tnb/jms/amq/service/AMQBroker.java b/system-x/services/jms/amq/src/main/java/software/tnb/jms/amq/service/AMQBroker.java index 8e44ee1ad..b1996d220 100644 --- a/system-x/services/jms/amq/src/main/java/software/tnb/jms/amq/service/AMQBroker.java +++ b/system-x/services/jms/amq/src/main/java/software/tnb/jms/amq/service/AMQBroker.java @@ -1,32 +1,19 @@ package software.tnb.jms.amq.service; -import software.tnb.common.account.AccountFactory; import software.tnb.common.service.Service; import software.tnb.jms.amq.account.AMQBrokerAccount; import software.tnb.jms.amq.validation.AMQValidation; import javax.jms.Connection; -public abstract class AMQBroker implements Service { - protected Connection connection; - - protected AMQBrokerAccount account; - protected AMQValidation validation; - - public AMQBrokerAccount account() { - if (account == null) { - account = AccountFactory.create(AMQBrokerAccount.class); - } - return account; - } - +public abstract class AMQBroker extends Service { public abstract String brokerUrl(); protected abstract String mqttUrl(); public AMQValidation validation() { if (validation == null) { - validation = new AMQValidation(connection, account(), mqttUrl()); + validation = new AMQValidation(client(), account(), mqttUrl()); } return validation; } diff --git a/system-x/services/jms/amq/src/main/java/software/tnb/jms/amq/validation/AMQValidation.java b/system-x/services/jms/amq/src/main/java/software/tnb/jms/amq/validation/AMQValidation.java index 8692e0bfa..60eca6be4 100644 --- a/system-x/services/jms/amq/src/main/java/software/tnb/jms/amq/validation/AMQValidation.java +++ b/system-x/services/jms/amq/src/main/java/software/tnb/jms/amq/validation/AMQValidation.java @@ -1,5 +1,6 @@ package software.tnb.jms.amq.validation; +import software.tnb.common.validation.Validation; import software.tnb.jms.amq.account.AMQBrokerAccount; import software.tnb.jms.client.JMSClientManager; import software.tnb.jms.client.JMSQueueClient; @@ -11,7 +12,7 @@ import java.util.UUID; -public class AMQValidation { +public class AMQValidation implements Validation { private final Connection connection; private JMSClientManager client; private final AMQBrokerAccount account; diff --git a/system-x/services/jms/ibm-mq/src/main/java/software/tnb/jms/ibm/mq/service/IBMMQ.java b/system-x/services/jms/ibm-mq/src/main/java/software/tnb/jms/ibm/mq/service/IBMMQ.java index a9e2ff07b..27bac4464 100644 --- a/system-x/services/jms/ibm-mq/src/main/java/software/tnb/jms/ibm/mq/service/IBMMQ.java +++ b/system-x/services/jms/ibm-mq/src/main/java/software/tnb/jms/ibm/mq/service/IBMMQ.java @@ -1,6 +1,5 @@ package software.tnb.jms.ibm.mq.service; -import software.tnb.common.account.AccountFactory; import software.tnb.common.deployment.WithDockerImage; import software.tnb.common.service.Service; import software.tnb.jms.ibm.mq.account.IBMMQAccount; @@ -18,29 +17,17 @@ import java.io.File; import java.util.Map; -public abstract class IBMMQ implements Service, WithDockerImage { +public abstract class IBMMQ extends Service implements WithDockerImage { private static final Logger LOG = LoggerFactory.getLogger(IBMMQ.class); public static final int DEFAULT_PORT = 1414; public static final String MQSC_COMMAND_FILES_LOCATION = "/etc/mqm"; public static final String MQSC_COMMAND_FILE_NAME = "99-tnb.mqsc"; - private IBMMQAccount account; - private IBMMQValidation validation; - - private Connection connection; - - public IBMMQAccount account() { - if (account == null) { - account = AccountFactory.create(IBMMQAccount.class); - } - return account; - } - public IBMMQValidation validation() { if (validation == null) { LOG.debug("Creating new IBM MQ validation"); - validation = new IBMMQValidation(account(), port(), connection); + validation = new IBMMQValidation(account(), port(), client()); } return validation; } @@ -82,8 +69,8 @@ public void openResources() { connectionFactory.setChannel(account().channel()); connectionFactory.setQueueManager(account().queueManager()); connectionFactory.setTransportType(WMQConstants.WMQ_CM_CLIENT); - connection = connectionFactory.createConnection(account().username(), account().password()); - connection.start(); + client = connectionFactory.createConnection(account().username(), account().password()); + client.start(); } catch (Exception e) { throw new RuntimeException("Unable to create MQConnectionFactory:", e); } @@ -91,12 +78,17 @@ public void openResources() { public void closeResources() { validation = null; - if (connection != null) { + if (client != null) { try { - connection.stop(); + client.stop(); } catch (JMSException e) { LOG.warn("Unable to close connection:", e); } } } + + @Override + protected Connection client() { + return client; + } } diff --git a/system-x/services/jms/ibm-mq/src/main/java/software/tnb/jms/ibm/mq/validation/IBMMQValidation.java b/system-x/services/jms/ibm-mq/src/main/java/software/tnb/jms/ibm/mq/validation/IBMMQValidation.java index f523594fd..3ceefc58f 100644 --- a/system-x/services/jms/ibm-mq/src/main/java/software/tnb/jms/ibm/mq/validation/IBMMQValidation.java +++ b/system-x/services/jms/ibm-mq/src/main/java/software/tnb/jms/ibm/mq/validation/IBMMQValidation.java @@ -1,10 +1,10 @@ package software.tnb.jms.ibm.mq.validation; -import software.tnb.jms.ibm.mq.account.IBMMQAccount; - +import software.tnb.common.validation.Validation; import software.tnb.jms.client.JMSClientManager; import software.tnb.jms.client.JMSQueueClient; import software.tnb.jms.client.JMSTopicClient; +import software.tnb.jms.ibm.mq.account.IBMMQAccount; import com.ibm.mq.MQException; import com.ibm.mq.MQQueueManager; @@ -19,7 +19,7 @@ import java.util.Hashtable; import java.util.Set; -public class IBMMQValidation { +public class IBMMQValidation implements Validation { private final IBMMQAccount account; private JMSClientManager client; diff --git a/system-x/services/kafka/src/main/java/software/tnb/kafka/service/Kafka.java b/system-x/services/kafka/src/main/java/software/tnb/kafka/service/Kafka.java index c30cae7f0..622b9458d 100644 --- a/system-x/services/kafka/src/main/java/software/tnb/kafka/service/Kafka.java +++ b/system-x/services/kafka/src/main/java/software/tnb/kafka/service/Kafka.java @@ -1,6 +1,7 @@ package software.tnb.kafka.service; import software.tnb.common.account.AccountFactory; +import software.tnb.common.client.NoClient; import software.tnb.common.service.Service; import software.tnb.kafka.account.KafkaAccount; import software.tnb.kafka.validation.KafkaValidation; @@ -19,7 +20,7 @@ import java.util.Properties; import java.util.UUID; -public abstract class Kafka implements Service { +public abstract class Kafka extends Service> { protected Map, KafkaValidation> validations; protected Properties props = defaultClientProperties(); private KafkaAccount account; diff --git a/system-x/services/kafka/src/main/java/software/tnb/kafka/validation/KafkaValidation.java b/system-x/services/kafka/src/main/java/software/tnb/kafka/validation/KafkaValidation.java index beb8ec755..922e22a86 100644 --- a/system-x/services/kafka/src/main/java/software/tnb/kafka/validation/KafkaValidation.java +++ b/system-x/services/kafka/src/main/java/software/tnb/kafka/validation/KafkaValidation.java @@ -1,5 +1,7 @@ package software.tnb.kafka.validation; +import software.tnb.common.validation.Validation; + import org.apache.kafka.clients.consumer.ConsumerRecord; import org.apache.kafka.clients.consumer.KafkaConsumer; import org.apache.kafka.clients.producer.KafkaProducer; @@ -16,7 +18,7 @@ import java.util.stream.Collectors; import java.util.stream.StreamSupport; -public class KafkaValidation { +public class KafkaValidation implements Validation { private static final Logger LOG = LoggerFactory.getLogger(KafkaValidation.class); private final KafkaProducer producer; private final KafkaConsumer consumer; diff --git a/system-x/services/knative/src/main/java/software/tnb/knative/service/Knative.java b/system-x/services/knative/src/main/java/software/tnb/knative/service/Knative.java index e540accd8..78002575e 100644 --- a/system-x/services/knative/src/main/java/software/tnb/knative/service/Knative.java +++ b/system-x/services/knative/src/main/java/software/tnb/knative/service/Knative.java @@ -1,14 +1,13 @@ package software.tnb.knative.service; +import software.tnb.common.account.NoAccount; import software.tnb.common.openshift.OpenshiftClient; import software.tnb.common.service.Service; import software.tnb.knative.validation.KnativeValidation; import io.fabric8.knative.client.KnativeClient; -public abstract class Knative implements Service { - protected KnativeValidation validation; - +public abstract class Knative extends Service { protected KnativeClient client() { if (OpenshiftClient.get().isAdaptable(KnativeClient.class)) { return OpenshiftClient.get().adapt(KnativeClient.class); @@ -16,6 +15,7 @@ protected KnativeClient client() { throw new IllegalArgumentException("Unable to adapt OpenshiftClient to KnativeClient"); } + @Override public KnativeValidation validation() { if (validation == null) { validation = new KnativeValidation(client()); diff --git a/system-x/services/knative/src/main/java/software/tnb/knative/validation/KnativeValidation.java b/system-x/services/knative/src/main/java/software/tnb/knative/validation/KnativeValidation.java index e78bcd3a9..c3a7ba725 100644 --- a/system-x/services/knative/src/main/java/software/tnb/knative/validation/KnativeValidation.java +++ b/system-x/services/knative/src/main/java/software/tnb/knative/validation/KnativeValidation.java @@ -2,6 +2,7 @@ import software.tnb.common.openshift.OpenshiftClient; import software.tnb.common.utils.WaitUtils; +import software.tnb.common.validation.Validation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -18,7 +19,7 @@ import io.fabric8.knative.messaging.v1.InMemoryChannel; import io.fabric8.kubernetes.api.model.HasMetadata; -public class KnativeValidation { +public class KnativeValidation implements Validation { private static final Logger LOG = LoggerFactory.getLogger(KnativeValidation.class); // This string is from the error message when you try to create a resource with invalid name private static final String NAME_VALIDATION_REGEX = "[a-z]([-a-z0-9]*[a-z0-9])?"; diff --git a/system-x/services/lra-coordinator/src/main/java/software/tnb/lracoordinator/service/LRACoordinator.java b/system-x/services/lra-coordinator/src/main/java/software/tnb/lracoordinator/service/LRACoordinator.java index 18981e152..837c30b1b 100644 --- a/system-x/services/lra-coordinator/src/main/java/software/tnb/lracoordinator/service/LRACoordinator.java +++ b/system-x/services/lra-coordinator/src/main/java/software/tnb/lracoordinator/service/LRACoordinator.java @@ -1,15 +1,14 @@ package software.tnb.lracoordinator.service; +import software.tnb.common.account.NoAccount; +import software.tnb.common.client.NoClient; import software.tnb.common.deployment.WithDockerImage; import software.tnb.common.service.Service; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import software.tnb.common.validation.NoValidation; import java.util.Map; -public abstract class LRACoordinator implements Service, WithDockerImage { - private static final Logger LOG = LoggerFactory.getLogger(LRACoordinator.class); +public abstract class LRACoordinator extends Service implements WithDockerImage { public static final int DEFAULT_PORT = 8888; diff --git a/system-x/services/mail/src/main/java/software/tnb/mail/service/MailServer.java b/system-x/services/mail/src/main/java/software/tnb/mail/service/MailServer.java index a502051a4..443a51532 100644 --- a/system-x/services/mail/src/main/java/software/tnb/mail/service/MailServer.java +++ b/system-x/services/mail/src/main/java/software/tnb/mail/service/MailServer.java @@ -1,5 +1,7 @@ package software.tnb.mail.service; +import software.tnb.common.account.NoAccount; +import software.tnb.common.client.NoClient; import software.tnb.common.deployment.WithDockerImage; import software.tnb.common.service.Service; import software.tnb.mail.validation.MailValidation; @@ -7,7 +9,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public abstract class MailServer implements Service, WithDockerImage { +public abstract class MailServer extends Service implements WithDockerImage { private static final Logger LOG = LoggerFactory.getLogger(MailServer.class); protected static final int SMTP_PORT = 25; protected static final int HTTP_PORT = 8000; diff --git a/system-x/services/mail/src/main/java/software/tnb/mail/validation/MailValidation.java b/system-x/services/mail/src/main/java/software/tnb/mail/validation/MailValidation.java index 908e6b72f..73c11de9f 100644 --- a/system-x/services/mail/src/main/java/software/tnb/mail/validation/MailValidation.java +++ b/system-x/services/mail/src/main/java/software/tnb/mail/validation/MailValidation.java @@ -1,6 +1,7 @@ package software.tnb.mail.validation; import software.tnb.common.utils.HTTPUtils; +import software.tnb.common.validation.Validation; import org.apache.commons.mail.DefaultAuthenticator; import org.apache.commons.mail.Email; @@ -15,7 +16,7 @@ import okhttp3.MediaType; import okhttp3.RequestBody; -public class MailValidation { +public class MailValidation implements Validation { private static final Logger LOG = LoggerFactory.getLogger(MailValidation.class); private final Map accounts = new HashMap<>(); diff --git a/system-x/services/microsoft/src/main/java/software/tnb/microsoft/service/Exchange.java b/system-x/services/microsoft/src/main/java/software/tnb/microsoft/service/Exchange.java index 844300702..b920f82e5 100644 --- a/system-x/services/microsoft/src/main/java/software/tnb/microsoft/service/Exchange.java +++ b/system-x/services/microsoft/src/main/java/software/tnb/microsoft/service/Exchange.java @@ -1,30 +1,18 @@ package software.tnb.microsoft.service; -import software.tnb.common.account.AccountFactory; +import software.tnb.common.client.NoClient; import software.tnb.common.service.Service; +import software.tnb.common.validation.NoValidation; import software.tnb.microsoft.account.MicrosoftAccount; import org.junit.jupiter.api.extension.ExtensionContext; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.google.auto.service.AutoService; import java.util.Map; @AutoService(Exchange.class) -public class Exchange implements Service { - private static final Logger LOG = LoggerFactory.getLogger(Exchange.class); - MicrosoftAccount account; - - public MicrosoftAccount account() { - if (account == null) { - LOG.debug("Creating new Exchange account"); - account = AccountFactory.create(MicrosoftAccount.class); - } - return account; - } +public class Exchange extends Service { public Map getCredentials() { return Map.of( diff --git a/system-x/services/microsoft/src/main/java/software/tnb/microsoft/service/Graph.java b/system-x/services/microsoft/src/main/java/software/tnb/microsoft/service/Graph.java index 3363a7762..b2e08b9dc 100644 --- a/system-x/services/microsoft/src/main/java/software/tnb/microsoft/service/Graph.java +++ b/system-x/services/microsoft/src/main/java/software/tnb/microsoft/service/Graph.java @@ -1,6 +1,5 @@ package software.tnb.microsoft.service; -import software.tnb.common.account.AccountFactory; import software.tnb.common.service.Service; import software.tnb.microsoft.account.MicrosoftAccount; import software.tnb.microsoft.validation.GraphValidation; @@ -19,22 +18,8 @@ import okhttp3.Request; @AutoService(Graph.class) -public class Graph implements Service { +public class Graph extends Service, GraphValidation> { private static final Logger LOG = LoggerFactory.getLogger(Graph.class); - private GraphValidation validation; - private MicrosoftAccount account; - - public MicrosoftAccount account() { - if (account == null) { - LOG.debug("Creating new Graph account"); - account = AccountFactory.create(MicrosoftAccount.class); - } - return account; - } - - public GraphValidation validation() { - return validation; - } protected GraphServiceClient client() { final ClientSecretCredential clientSecretCredential = new ClientSecretCredentialBuilder() diff --git a/system-x/services/microsoft/src/main/java/software/tnb/microsoft/validation/GraphValidation.java b/system-x/services/microsoft/src/main/java/software/tnb/microsoft/validation/GraphValidation.java index c4d705fd6..d33edd987 100644 --- a/system-x/services/microsoft/src/main/java/software/tnb/microsoft/validation/GraphValidation.java +++ b/system-x/services/microsoft/src/main/java/software/tnb/microsoft/validation/GraphValidation.java @@ -1,5 +1,7 @@ package software.tnb.microsoft.validation; +import software.tnb.common.validation.Validation; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -16,7 +18,7 @@ import okhttp3.Request; -public class GraphValidation { +public class GraphValidation implements Validation { private static final Logger LOG = LoggerFactory.getLogger(GraphValidation.class); private final GraphServiceClient client; diff --git a/system-x/services/mllp/src/main/java/software/tnb/mllp/service/MllpServer.java b/system-x/services/mllp/src/main/java/software/tnb/mllp/service/MllpServer.java index 4e97b2c87..7937f8986 100644 --- a/system-x/services/mllp/src/main/java/software/tnb/mllp/service/MllpServer.java +++ b/system-x/services/mllp/src/main/java/software/tnb/mllp/service/MllpServer.java @@ -1,11 +1,14 @@ package software.tnb.mllp.service; +import software.tnb.common.account.NoAccount; +import software.tnb.common.client.NoClient; import software.tnb.common.deployment.WithDockerImage; import software.tnb.common.service.Service; +import software.tnb.common.validation.NoValidation; import java.util.Map; -public abstract class MllpServer implements Service, WithDockerImage { +public abstract class MllpServer extends Service implements WithDockerImage { public static final int LISTENING_PORT = 8080; diff --git a/system-x/services/prometheus-metrics/src/main/java/software/tnb/prometheus/metrics/service/PrometheusMetricsConfiguration.java b/system-x/services/prometheus-metrics/src/main/java/software/tnb/prometheus/metrics/configuration/PrometheusMetricsConfiguration.java similarity index 85% rename from system-x/services/prometheus-metrics/src/main/java/software/tnb/prometheus/metrics/service/PrometheusMetricsConfiguration.java rename to system-x/services/prometheus-metrics/src/main/java/software/tnb/prometheus/metrics/configuration/PrometheusMetricsConfiguration.java index be6bec5cf..9bdf5585b 100644 --- a/system-x/services/prometheus-metrics/src/main/java/software/tnb/prometheus/metrics/service/PrometheusMetricsConfiguration.java +++ b/system-x/services/prometheus-metrics/src/main/java/software/tnb/prometheus/metrics/configuration/PrometheusMetricsConfiguration.java @@ -1,4 +1,4 @@ -package software.tnb.prometheus.metrics.service; +package software.tnb.prometheus.metrics.configuration; import software.tnb.common.config.Configuration; diff --git a/system-x/services/prometheus-metrics/src/main/java/software/tnb/prometheus/metrics/service/PrometheusMetrics.java b/system-x/services/prometheus-metrics/src/main/java/software/tnb/prometheus/metrics/service/PrometheusMetrics.java index 68846e7b6..3978b7471 100644 --- a/system-x/services/prometheus-metrics/src/main/java/software/tnb/prometheus/metrics/service/PrometheusMetrics.java +++ b/system-x/services/prometheus-metrics/src/main/java/software/tnb/prometheus/metrics/service/PrometheusMetrics.java @@ -1,15 +1,11 @@ package software.tnb.prometheus.metrics.service; +import software.tnb.common.account.NoAccount; +import software.tnb.common.client.NoClient; import software.tnb.common.service.Service; import software.tnb.prometheus.metrics.validation.PrometheusMetricsValidation; -public abstract class PrometheusMetrics implements Service { - - protected PrometheusMetricsValidation validation; - - public PrometheusMetricsValidation validation() { - return validation; - } +public abstract class PrometheusMetrics extends Service { public abstract String getUrl(); } diff --git a/system-x/services/prometheus-metrics/src/main/java/software/tnb/prometheus/metrics/validation/PrometheusMetricsValidation.java b/system-x/services/prometheus-metrics/src/main/java/software/tnb/prometheus/metrics/validation/PrometheusMetricsValidation.java index 143743657..63c07dc3c 100644 --- a/system-x/services/prometheus-metrics/src/main/java/software/tnb/prometheus/metrics/validation/PrometheusMetricsValidation.java +++ b/system-x/services/prometheus-metrics/src/main/java/software/tnb/prometheus/metrics/validation/PrometheusMetricsValidation.java @@ -2,7 +2,8 @@ import software.tnb.common.utils.HTTPUtils; import software.tnb.common.utils.HTTPUtils.Response; -import software.tnb.prometheus.metrics.service.PrometheusMetricsConfiguration; +import software.tnb.common.validation.Validation; +import software.tnb.prometheus.metrics.configuration.PrometheusMetricsConfiguration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -19,7 +20,7 @@ import java.util.List; import java.util.Map; -public class PrometheusMetricsValidation { +public class PrometheusMetricsValidation implements Validation { private static final Logger LOG = LoggerFactory.getLogger(PrometheusMetricsValidation.class); diff --git a/system-x/services/salesforce/src/main/java/software/tnb/salesforce/service/Salesforce.java b/system-x/services/salesforce/src/main/java/software/tnb/salesforce/service/Salesforce.java index ee3645839..9aa72e7b8 100644 --- a/system-x/services/salesforce/src/main/java/software/tnb/salesforce/service/Salesforce.java +++ b/system-x/services/salesforce/src/main/java/software/tnb/salesforce/service/Salesforce.java @@ -1,6 +1,5 @@ package software.tnb.salesforce.service; -import software.tnb.common.account.AccountFactory; import software.tnb.common.service.Service; import software.tnb.common.utils.WaitUtils; import software.tnb.salesforce.account.SalesforceAccount; @@ -16,20 +15,9 @@ import com.google.auto.service.AutoService; @AutoService(Salesforce.class) -public class Salesforce implements Service { +public class Salesforce extends Service { private static final Logger LOG = LoggerFactory.getLogger(Salesforce.class); - private SalesforceAccount account; - private ForceApi client; - private SalesforceValidation validation; - - public SalesforceAccount account() { - if (account == null) { - account = AccountFactory.create(SalesforceAccount.class); - } - return account; - } - protected ForceApi client() { if (client == null) { WaitUtils.waitFor(() -> { diff --git a/system-x/services/salesforce/src/main/java/software/tnb/salesforce/validation/SalesforceValidation.java b/system-x/services/salesforce/src/main/java/software/tnb/salesforce/validation/SalesforceValidation.java index 719a3f1bf..c6b627501 100644 --- a/system-x/services/salesforce/src/main/java/software/tnb/salesforce/validation/SalesforceValidation.java +++ b/system-x/services/salesforce/src/main/java/software/tnb/salesforce/validation/SalesforceValidation.java @@ -1,5 +1,6 @@ package software.tnb.salesforce.validation; +import software.tnb.common.validation.Validation; import software.tnb.salesforce.dto.Account; import software.tnb.salesforce.dto.Case; import software.tnb.salesforce.dto.Lead; @@ -23,7 +24,7 @@ import java.util.Map; import java.util.Optional; -public class SalesforceValidation { +public class SalesforceValidation implements Validation { private static final Logger LOG = LoggerFactory.getLogger(SalesforceValidation.class); private final ForceApi client; diff --git a/system-x/services/servicenow/src/main/java/software/tnb/servicenow/service/ServiceNow.java b/system-x/services/servicenow/src/main/java/software/tnb/servicenow/service/ServiceNow.java index 045f3175e..3bc454692 100644 --- a/system-x/services/servicenow/src/main/java/software/tnb/servicenow/service/ServiceNow.java +++ b/system-x/services/servicenow/src/main/java/software/tnb/servicenow/service/ServiceNow.java @@ -1,6 +1,6 @@ package software.tnb.servicenow.service; -import software.tnb.common.account.AccountFactory; +import software.tnb.common.client.NoClient; import software.tnb.common.service.Service; import software.tnb.servicenow.account.ServiceNowAccount; import software.tnb.servicenow.validation.ServiceNowValidation; @@ -13,20 +13,10 @@ import com.google.auto.service.AutoService; @AutoService(ServiceNow.class) -public class ServiceNow implements Service { +public class ServiceNow extends Service { private static final Logger LOG = LoggerFactory.getLogger(ServiceNow.class); - private ServiceNowAccount account; - private ServiceNowValidation validation; - - public ServiceNowAccount account() { - if (account == null) { - account = AccountFactory.create(ServiceNowAccount.class); - } - return account; - } - public ServiceNowValidation validation() { if (validation == null) { LOG.debug("Creating new ServiceNow validation"); diff --git a/system-x/services/servicenow/src/main/java/software/tnb/servicenow/validation/ServiceNowValidation.java b/system-x/services/servicenow/src/main/java/software/tnb/servicenow/validation/ServiceNowValidation.java index fdbc70e31..292cb576f 100644 --- a/system-x/services/servicenow/src/main/java/software/tnb/servicenow/validation/ServiceNowValidation.java +++ b/system-x/services/servicenow/src/main/java/software/tnb/servicenow/validation/ServiceNowValidation.java @@ -1,10 +1,11 @@ package software.tnb.servicenow.validation; +import software.tnb.common.utils.HTTPUtils; +import software.tnb.common.validation.Validation; import software.tnb.servicenow.account.ServiceNowAccount; import software.tnb.servicenow.dto.Incident; import software.tnb.servicenow.dto.IncidentRecordList; import software.tnb.servicenow.dto.IncidentSingleResponse; -import software.tnb.common.utils.HTTPUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -13,20 +14,20 @@ import com.fasterxml.jackson.databind.ObjectMapper; import java.io.IOException; -import java.io.UnsupportedEncodingException; import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import java.util.Map; import okhttp3.Credentials; -public class ServiceNowValidation { +public class ServiceNowValidation implements Validation { private static final Logger LOG = LoggerFactory.getLogger(ServiceNowValidation.class); - private ServiceNowAccount account; - private ObjectMapper om; + private final ServiceNowAccount account; + private final ObjectMapper om; public ServiceNowValidation(ServiceNowAccount account) { this.account = account; @@ -42,11 +43,7 @@ public List getFilteredIncidents(String filter, int limit) { String getUrl = account.url() + "?sysparm_limit=" + limit; if (filter != null) { - try { - getUrl += "&sysparm_query=" + URLEncoder.encode(filter, "UTF-8"); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); - } + getUrl += "&sysparm_query=" + URLEncoder.encode(filter, StandardCharsets.UTF_8); } HTTPUtils.Response r = HTTPUtils.getInstance().get(getUrl, Map.of("Authorization", Credentials.basic(account.userName(), account.password()))); diff --git a/system-x/services/slack/src/main/java/software/tnb/slack/service/Slack.java b/system-x/services/slack/src/main/java/software/tnb/slack/service/Slack.java index 6a61a3dd3..6f6514c44 100644 --- a/system-x/services/slack/src/main/java/software/tnb/slack/service/Slack.java +++ b/system-x/services/slack/src/main/java/software/tnb/slack/service/Slack.java @@ -1,6 +1,5 @@ package software.tnb.slack.service; -import software.tnb.common.account.AccountFactory; import software.tnb.common.service.Service; import software.tnb.slack.account.SlackAccount; import software.tnb.slack.validation.SlackValidation; @@ -13,31 +12,15 @@ import com.google.auto.service.AutoService; @AutoService(Slack.class) -public class Slack implements Service { +public class Slack extends Service { private static final Logger LOG = LoggerFactory.getLogger(Slack.class); - private SlackAccount account; - private com.slack.api.Slack client; - private SlackValidation validation; - - public SlackAccount account() { - if (account == null) { - LOG.debug("Creating new Slack account"); - account = AccountFactory.create(SlackAccount.class); - } - return account; - } - protected com.slack.api.Slack client() { LOG.debug("Creating new Slack client"); client = com.slack.api.Slack.getInstance(); return client; } - public SlackValidation validation() { - return validation; - } - @Override public void afterAll(ExtensionContext extensionContext) throws Exception { if (client != null) { diff --git a/system-x/services/slack/src/main/java/software/tnb/slack/validation/SlackValidation.java b/system-x/services/slack/src/main/java/software/tnb/slack/validation/SlackValidation.java index 3f56be855..f5255afde 100644 --- a/system-x/services/slack/src/main/java/software/tnb/slack/validation/SlackValidation.java +++ b/system-x/services/slack/src/main/java/software/tnb/slack/validation/SlackValidation.java @@ -3,6 +3,7 @@ import static org.junit.jupiter.api.Assertions.fail; import software.tnb.common.utils.WaitUtils; +import software.tnb.common.validation.Validation; import software.tnb.slack.account.SlackAccount; import software.tnb.slack.validation.util.ThrowingFunction; @@ -24,7 +25,7 @@ import java.util.List; import java.util.stream.Collectors; -public class SlackValidation { +public class SlackValidation implements Validation { private static final Logger LOG = LoggerFactory.getLogger(SlackValidation.class); private final Slack client; diff --git a/system-x/services/splunk/src/main/java/software/tnb/splunk/service/Splunk.java b/system-x/services/splunk/src/main/java/software/tnb/splunk/service/Splunk.java index a4497dc3c..70fdf54b7 100644 --- a/system-x/services/splunk/src/main/java/software/tnb/splunk/service/Splunk.java +++ b/system-x/services/splunk/src/main/java/software/tnb/splunk/service/Splunk.java @@ -7,9 +7,11 @@ import software.tnb.splunk.service.configuration.SplunkConfiguration; import software.tnb.splunk.validation.SplunkValidation; +import com.splunk.Service; import com.splunk.ServiceArgs; -public abstract class Splunk extends ConfigurableService implements WithExternalHostname, WithDockerImage { +public abstract class Splunk extends ConfigurableService + implements WithExternalHostname, WithDockerImage { protected SplunkAccount account; protected com.splunk.Service client; @@ -17,8 +19,6 @@ public abstract class Splunk extends ConfigurableService im public abstract int apiPort(); - public abstract SplunkAccount account(); - /** * Due to self sign certificate, the client is not able to communicate via localhost and port-forward. * OCP external route with `reencrypt` is used. (Cluster needs to have valid certificate!) diff --git a/system-x/services/splunk/src/main/java/software/tnb/splunk/validation/SplunkValidation.java b/system-x/services/splunk/src/main/java/software/tnb/splunk/validation/SplunkValidation.java index 4b36565d4..e2f830cce 100644 --- a/system-x/services/splunk/src/main/java/software/tnb/splunk/validation/SplunkValidation.java +++ b/system-x/services/splunk/src/main/java/software/tnb/splunk/validation/SplunkValidation.java @@ -1,6 +1,7 @@ package software.tnb.splunk.validation; import software.tnb.common.utils.WaitUtils; +import software.tnb.common.validation.Validation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -18,7 +19,7 @@ import java.util.List; import java.util.stream.Collectors; -public class SplunkValidation { +public class SplunkValidation implements Validation { private static final Logger LOG = LoggerFactory.getLogger(SplunkValidation.class); diff --git a/system-x/services/telegram/src/main/java/software/tnb/telegram/service/Telegram.java b/system-x/services/telegram/src/main/java/software/tnb/telegram/service/Telegram.java index 5b84a913a..682000dde 100644 --- a/system-x/services/telegram/src/main/java/software/tnb/telegram/service/Telegram.java +++ b/system-x/services/telegram/src/main/java/software/tnb/telegram/service/Telegram.java @@ -1,37 +1,20 @@ package software.tnb.telegram.service; -import software.tnb.common.account.AccountFactory; +import software.tnb.common.client.NoClient; import software.tnb.common.deployment.WithDockerImage; import software.tnb.common.service.Service; import software.tnb.telegram.account.TelegramAccount; -import software.tnb.telegram.resource.local.LocalTelegram; import software.tnb.telegram.validation.TelegramValidation; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import java.util.HashMap; import java.util.Map; -public abstract class Telegram implements Service, WithDockerImage { - - private static final Logger LOG = LoggerFactory.getLogger(LocalTelegram.class); - private TelegramAccount account; - private TelegramValidation validation; - +public abstract class Telegram extends Service implements WithDockerImage { @Override public String defaultImage() { return "quay.io/fuse_qe/telegram-client:latest"; } - public TelegramAccount account() { - if (account == null) { - LOG.debug("Creating new Telegram account"); - account = AccountFactory.create(TelegramAccount.class); - } - return account; - } - public abstract String execInContainer(String... commands); public TelegramValidation validation() { diff --git a/system-x/services/telegram/src/main/java/software/tnb/telegram/service/TelegramBotApi.java b/system-x/services/telegram/src/main/java/software/tnb/telegram/service/TelegramBotApi.java index e3322ccc2..52b3a6993 100644 --- a/system-x/services/telegram/src/main/java/software/tnb/telegram/service/TelegramBotApi.java +++ b/system-x/services/telegram/src/main/java/software/tnb/telegram/service/TelegramBotApi.java @@ -1,25 +1,18 @@ package software.tnb.telegram.service; -import software.tnb.common.account.AccountFactory; +import software.tnb.common.client.NoClient; import software.tnb.common.deployment.WithDockerImage; import software.tnb.common.deployment.WithExternalHostname; import software.tnb.common.service.Service; +import software.tnb.common.validation.Validation; import software.tnb.telegram.account.TelegramAccount; import java.util.Map; -public abstract class TelegramBotApi implements Service, WithExternalHostname, WithDockerImage { +public abstract class TelegramBotApi extends Service implements WithExternalHostname, WithDockerImage { private static final String PORT = System.getProperty("telegram-bot-api.port", "8081"); private static final String VERBOSITY = System.getProperty("telegram-bot-api.verbosity", "9"); - private TelegramAccount account; - - protected TelegramAccount account() { - if (account == null) { - account = AccountFactory.create(TelegramAccount.class); - } - return account; - } protected Map getEnv() { return Map.of("TELEGRAM_API_ID", account().getAppId(), @@ -48,5 +41,4 @@ public String defaultImage() { protected abstract String getWorkingDir(); protected abstract String getUploadDir(); - } diff --git a/system-x/services/telegram/src/main/java/software/tnb/telegram/validation/TelegramValidation.java b/system-x/services/telegram/src/main/java/software/tnb/telegram/validation/TelegramValidation.java index a2a914a7c..f14333e27 100644 --- a/system-x/services/telegram/src/main/java/software/tnb/telegram/validation/TelegramValidation.java +++ b/system-x/services/telegram/src/main/java/software/tnb/telegram/validation/TelegramValidation.java @@ -1,5 +1,6 @@ package software.tnb.telegram.validation; +import software.tnb.common.validation.Validation; import software.tnb.telegram.service.Telegram; import software.tnb.telegram.validation.model.Message; @@ -11,7 +12,7 @@ import java.util.List; -public class TelegramValidation { +public class TelegramValidation implements Validation { private final Telegram client; private final ObjectMapper objectMapper = new ObjectMapper(); diff --git a/system-x/services/webhook/src/main/java/software/tnb/webhook/service/Webhook.java b/system-x/services/webhook/src/main/java/software/tnb/webhook/service/Webhook.java index 22cff5c24..bcc3c0da8 100644 --- a/system-x/services/webhook/src/main/java/software/tnb/webhook/service/Webhook.java +++ b/system-x/services/webhook/src/main/java/software/tnb/webhook/service/Webhook.java @@ -1,6 +1,6 @@ package software.tnb.webhook.service; -import software.tnb.common.account.AccountFactory; +import software.tnb.common.client.NoClient; import software.tnb.common.service.Service; import software.tnb.common.utils.JUnitUtils; import software.tnb.webhook.account.WebhookAccount; @@ -8,27 +8,13 @@ import org.junit.jupiter.api.extension.ExtensionContext; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.google.auto.service.AutoService; @AutoService(Webhook.class) -public class Webhook implements Service { - private static final Logger LOG = LoggerFactory.getLogger(Webhook.class); - - private WebhookAccount account; +public class Webhook extends Service { // make validation static on purpose to share the created webhook endpoint in multiple test classes (in addition to afterAll method) private static WebhookValidation validation; - public WebhookAccount account() { - if (account == null) { - LOG.debug("Creating new Webhook account"); - account = AccountFactory.create(WebhookAccount.class); - } - return account; - } - public WebhookValidation validation() { if (validation == null) { validation = new WebhookValidation(); diff --git a/system-x/services/webhook/src/main/java/software/tnb/webhook/validation/WebhookValidation.java b/system-x/services/webhook/src/main/java/software/tnb/webhook/validation/WebhookValidation.java index 234127a70..94f5d2b89 100644 --- a/system-x/services/webhook/src/main/java/software/tnb/webhook/validation/WebhookValidation.java +++ b/system-x/services/webhook/src/main/java/software/tnb/webhook/validation/WebhookValidation.java @@ -1,7 +1,7 @@ package software.tnb.webhook.validation; -import software.tnb.common.service.Validation; import software.tnb.common.utils.HTTPUtils; +import software.tnb.common.validation.Validation; import software.tnb.webhook.validation.model.WebhookSiteRequest; import software.tnb.webhook.validation.model.WebhookSiteRequests;