/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.camel.test.infra.mongodb.services;

import org.apache.camel.test.infra.common.services.SimpleTestServiceBuilder;
import org.apache.camel.test.infra.common.services.SingletonService;
import org.junit.jupiter.api.extension.ExtensionContext;

public final class MongoDBServiceFactory {
    static class SingletonMongoDBService extends SingletonService<MongoDBService> implements MongoDBService {
        public SingletonMongoDBService(MongoDBService service, String name) {
            super(service, name);
        }

        @Override
        public void beforeAll(ExtensionContext extensionContext) {
            addToStore(extensionContext);
        }

        @Override
        public void afterAll(ExtensionContext extensionContext) {
            // NO-OP
        }

        @Override
        public String getReplicaSetUrl() {
            return getService().getReplicaSetUrl();
        }

        @Override
        public String getConnectionAddress() {
            return getService().getConnectionAddress();
        }
    }

    private MongoDBServiceFactory() {

    }

    public static SimpleTestServiceBuilder<MongoDBService> builder() {
        return new SimpleTestServiceBuilder<>("mongodb");
    }

    public static MongoDBService createService() {
        return builder()
                .addLocalMapping(MongoDBLocalContainerService::new)
                .addRemoteMapping(MongoDBRemoteService::new)
                .build();
    }

    public static MongoDBService createSingletonService() {
        return builder()
                .addLocalMapping(() -> new SingletonMongoDBService(new MongoDBLocalContainerService(), "mongo-db"))
                .addRemoteMapping(MongoDBRemoteService::new)
                .build();
    }
}

@@ -1844,24 +1844,138 @@
    %7D%0A    %7D%0A%0A
+    private static SimpleTestServiceBuilder%3CMongoDBService%3E instance;%0A    private static MongoDBService service;%0A%0A
     private 
@@ -2000,16 +2000,16 @@
 ory() %7B%0A
-
 %0A    %7D%0A%0A
@@ -2442,38 +2442,117 @@
 ice() %7B%0A        
-return
+if (service == null) %7B%0A            if (instance == null) %7B%0A                instance =
  builder()%0A     
@@ -2537,32 +2537,33 @@
 ance = builder()
+;
 %0A               
@@ -2555,32 +2555,40 @@
                 
+instance
 .addLocalMapping
@@ -2679,32 +2679,40 @@
                 
+        
 .addRemoteMappin
@@ -2735,32 +2735,33 @@
 ervice::new)
+;
 %0A       
          .bu
@@ -2740,32 +2740,40 @@
 e::new);%0A       
+     %7D%0A%0A
          .build(
@@ -2761,30 +2761,86 @@
 %7D%0A%0A         
-.build()
+   service = instance.build();%0A        %7D%0A%0A        return service
 ;%0A    %7D%0A%7D%0A

fix MongoDB services in test infra not being singleton