/*
 * 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.couchbase.services;

import org.apache.camel.test.infra.couchbase.common.CouchbaseProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.couchbase.BucketDefinition;
import org.testcontainers.couchbase.CouchbaseContainer;

public class CouchbaseLocalContainerService implements CouchbaseService {

    /*
     * Couchbase container uses a dynamic port for the KV service. The configuration
     * used in the Camel component tries to use that port by default and it seems
     * we cannot configure it. Therefore, we override the default container and
     * force the default KV port to be used.
     */
    private class CustomCouchbaseContainer extends CouchbaseContainer {
        public CustomCouchbaseContainer() {
            final int kvPort = 11210;
            addFixedExposedPort(kvPort, kvPort);
        }
    }

    private static final Logger LOG = LoggerFactory.getLogger(CouchbaseLocalContainerService.class);
    private CouchbaseContainer container;

    public CouchbaseLocalContainerService() {
        container = new CustomCouchbaseContainer();
    }

    public CouchbaseLocalContainerService(String bucketName) {
        BucketDefinition bucketDefinition = new BucketDefinition(bucketName);

        container = new CustomCouchbaseContainer()
                .withBucket(bucketDefinition);
    }

    @Override
    public String getConnectionString() {
        return container.getConnectionString();
    }

    @Override
    public String getUsername() {
        return container.getUsername();
    }

    @Override
    public String getPassword() {
        return container.getPassword();
    }

    @Override
    public String getHostname() {
        return container.getHost();
    }

    @Override
    public int getPort() {
        return container.getBootstrapHttpDirectPort();
    }

    @Override
    public void registerProperties() {
        System.setProperty(CouchbaseProperties.COUCHBASE_HOSTNAME, getHostname());
        System.setProperty(CouchbaseProperties.COUCHBASE_PORT, String.valueOf(getPort()));
        System.setProperty(CouchbaseProperties.COUCHBASE_USERNAME, getUsername());
        System.setProperty(CouchbaseProperties.COUCHBASE_PASSWORD, getPassword());
    }

    @Override
    public void initialize() {
        container.start();
        registerProperties();

        LOG.debug("Couchbase container running at {}", getConnectionString());
    }

    @Override
    public void shutdown() {
        container.stop();
    }
}

@@ -854,16 +854,85 @@
 vices;%0A%0A
+import org.apache.camel.test.infra.common.services.ContainerService;%0A
 import o
@@ -1235,16 +1235,54 @@
 eService
+, ContainerService%3CCouchbaseContainer%3E
  %7B%0A%0A    
@@ -1696,24 +1696,70 @@
 ntainer() %7B%0A
+            super(%22couchbase/server:6.5.1%22);%0A%0A
             
@@ -1832,16 +1832,415 @@
 kvPort);
+%0A%0A            final int managementPort = 8091;%0A            addFixedExposedPort(managementPort, managementPort);%0A%0A            final int viewPort = 8092;%0A            addFixedExposedPort(viewPort, viewPort);%0A%0A            final int queryPort = 8093;%0A            addFixedExposedPort(queryPort, queryPort);%0A%0A            final int searchPort = 8094;%0A            addFixedExposedPort(searchPort, searchPort);
 %0A       
@@ -3903,11 +3903,105 @@
 );%0A    %7D
+%0A%0A    @Override%0A    public CouchbaseContainer getContainer() %7B%0A        return container;%0A    %7D
 %0A%7D%0A

Fix upsert timeouts issues when using the couchbase test-infra (#4764)