/**
 * Copyright 2009-2020 the original author or authors.
 *
 * Licensed 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 net.javacrumbs.shedlock.provider.couchbase.javaclient;


import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.CouchbaseCluster;
import com.couchbase.client.java.cluster.BucketSettings;
import com.couchbase.client.java.cluster.DefaultBucketSettings;
import com.couchbase.client.java.document.JsonDocument;
import net.javacrumbs.shedlock.support.StorageBasedLockProvider;
import net.javacrumbs.shedlock.test.support.AbstractStorageBasedLockProviderIntegrationTest;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.testcontainers.couchbase.CouchbaseContainer;

import static java.time.Instant.parse;
import static net.javacrumbs.shedlock.core.ClockProvider.now;
import static net.javacrumbs.shedlock.provider.couchbase.javaclient.CouchbaseLockProvider.LOCKED_AT;
import static net.javacrumbs.shedlock.provider.couchbase.javaclient.CouchbaseLockProvider.LOCKED_BY;
import static net.javacrumbs.shedlock.provider.couchbase.javaclient.CouchbaseLockProvider.LOCK_UNTIL;
import static org.assertj.core.api.Assertions.assertThat;

public class CouchbaseLockProviderIntegrationTest extends AbstractStorageBasedLockProviderIntegrationTest {

    private static final String BUCKET_NAME = "test";
    private static final String HOST = "127.0.0.1";
    public static final String BUCKET_PASSWORD = "bucketPass";

    private CouchbaseLockProvider lockProvider;
    private static CouchbaseCluster cluster;
    private static Bucket bucket;
    private static CouchbaseContainer container;

    @BeforeAll
    public static void startCouchbase () {
        BucketSettings bucketSettings = DefaultBucketSettings
            .builder()
            .name(BUCKET_NAME)
            .password(BUCKET_PASSWORD)
            .build();
        container = new CouchbaseContainer().withNewBucket(bucketSettings);
        container.start();

        cluster = container.getCouchbaseCluster();
        bucket = cluster.openBucket(BUCKET_NAME, BUCKET_PASSWORD);
    }

    @AfterAll
    public static void stopCouchbase () {
        cluster.disconnect();
        container.stop();
    }

    @BeforeEach
    public void createLockProvider()  {
        bucket = getBucket();
        lockProvider = new CouchbaseLockProvider(bucket);
    }

    @AfterEach
    public void clear() {
        try {
            bucket.remove(LOCK_NAME1);
        } catch (Exception e) {
            // ignore
        }
    }

    @Override
    protected StorageBasedLockProvider getLockProvider() {
        return lockProvider;
    }

    @Override
    public void assertUnlocked(String lockName) {
        JsonDocument lockDocument = bucket.get(lockName);
        assertThat(parse((String) lockDocument.content().get(LOCK_UNTIL))).isBeforeOrEqualTo(now());
        assertThat(parse((String) lockDocument.content().get(LOCKED_AT))).isBefore(now());
        assertThat(lockDocument.content().get(LOCKED_BY)).asString().isNotEmpty();
    }

    @Override
    public void assertLocked(String lockName) {
        JsonDocument lockDocument = bucket.get(lockName);
        assertThat(parse((String) lockDocument.content().get(LOCK_UNTIL))).isAfter(now());
        assertThat(parse((String) lockDocument.content().get(LOCKED_AT))).isBefore(now());
        assertThat(lockDocument.content().get(LOCKED_BY)).asString().isNotEmpty();
    }

    private Bucket getBucket() {
        return bucket;
    }

}

@@ -721,16 +721,58 @@
 Bucket;%0A
+import com.couchbase.client.java.Cluster;%0A
 import c
@@ -991,16 +991,141 @@
 cument;%0A
+import com.couchbase.client.java.env.CouchbaseEnvironment;%0Aimport com.couchbase.client.java.env.DefaultCouchbaseEnvironment;%0A
 import n
@@ -1430,24 +1430,78 @@
 BeforeEach;%0A
+import org.testcontainers.couchbase.BucketDefinition;%0A
 import org.t
@@ -2175,123 +2175,8 @@
 st%22;
-%0A    private static final String HOST = %22127.0.0.1%22;%0A    public static final String BUCKET_PASSWORD = %22bucketPass%22;
 %0A%0A  
@@ -2420,153 +2420,372 @@
     
-BucketSettings bucketSettings = DefaultBucketSettings%0A            .builder()%0A            .name(BUCKET_NAME)%0A            .password(BUCKET_PASSWORD
+container = new CouchbaseContainer().withBucket(new BucketDefinition(BUCKET_NAME));%0A        container.start();%0A%0A        CouchbaseEnvironment environment = DefaultCouchbaseEnvironment%0A            .builder()%0A            .bootstrapCarrierDirectPort(container.getBootstrapCarrierDirectPort())%0A            .bootstrapHttpDirectPort(container.getBootstrapHttpDirectPort()
 )%0A  
@@ -2800,24 +2800,25 @@
   .build();%0A
+%0A
         cont
@@ -2810,38 +2810,32 @@
 ;%0A%0A        c
-ontain
+lust
 er =
- new
  CouchbaseCo
@@ -2837,58 +2837,58 @@
 aseC
-ontainer().withNewBucket(bucketSettings);%0A
+luster.create(%0A            environment,%0A  
         
 cont
@@ -2875,32 +2875,34 @@
 ment,%0A          
+  
 container.start(
@@ -2899,69 +2899,125 @@
 ner.
-start();%0A%0A        cluster = container.getCouchbaseCluster(
+getContainerIpAddress()%0A        );%0A%0A        cluster.authenticate(container.getUsername(), container.getPassword()
 );%0A
+%0A
     
@@ -3063,25 +3063,8 @@
 NAME
-, BUCKET_PASSWORD
 );%0A 

Fix CouchBase test