/**
 * 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.test.support.jdbc;

import net.javacrumbs.shedlock.core.ClockProvider;
import net.javacrumbs.shedlock.core.LockConfiguration;
import net.javacrumbs.shedlock.core.SimpleLock;
import net.javacrumbs.shedlock.test.support.AbstractStorageBasedLockProviderIntegrationTest;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import javax.sql.DataSource;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Optional;
import java.util.concurrent.ExecutionException;

import static org.assertj.core.api.Assertions.assertThat;

public abstract class AbstractJdbcLockProviderIntegrationTest extends AbstractStorageBasedLockProviderIntegrationTest {
    protected JdbcTestUtils testUtils;

    @BeforeEach
    public void initTestUtils() {
        testUtils = new JdbcTestUtils(getDbConfig());
    }

    protected abstract DbConfig getDbConfig();

    @AfterEach
    public void cleanup() {
        testUtils.clean();
    }

    public JdbcTestUtils getTestUtils() {
        return testUtils;
    }

    @Override
    protected void assertUnlocked(String lockName) {
        Instant lockedUntil = getLockedUntil(lockName).toInstant();
        assertThat(lockedUntil).isBeforeOrEqualTo(ClockProvider.now().truncatedTo(ChronoUnit.MILLIS).plusMillis(1));
    }

    private Timestamp getLockedUntil(String lockName) {
        return testUtils.getLockedUntil(lockName);
    }

    @Override
    protected void assertLocked(String lockName) {
        Instant lockedUntil = getLockedUntil(lockName).toInstant();
        assertThat(lockedUntil).isAfter(ClockProvider.now());
    }

    @Test
    public void shouldCreateLockIfRecordAlreadyExists() {
        testUtils.getJdbcTemplate().update("INSERT INTO shedlock(name, lock_until, locked_at, locked_by) VALUES(?, now(), now(), ?)", LOCK_NAME1, "me");
        shouldCreateLock();
    }

    @Test
    public void fuzzTestShouldWorkWithTransaction() throws ExecutionException, InterruptedException {
        TransactionalFuzzTester.fuzzTestShouldWorkWithTransaction(getLockProvider(), getDatasource());
    }

    @Test
    public void shouldNotFailIfKeyNameTooLong() {
        LockConfiguration configuration = lockConfig("lock name that is too long Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.");
        Optional<SimpleLock> lock = getLockProvider().lock(configuration);
        assertThat(lock).isEmpty();
    }

    protected DataSource getDatasource() {
        return testUtils.getDatasource();
    }
}

@@ -2396,24 +2396,109 @@
 yExists() %7B%0A
+        Timestamp previousLockTime = Timestamp.from(Instant.now().minusSeconds(60));%0A
         test
@@ -2604,20 +2604,12 @@
 (?, 
-now(), now()
+?, ?
 , ?)
@@ -2622,16 +2622,52 @@
 K_NAME1,
+ previousLockTime, previousLockTime,
  %22me%22);%0A

Make test more robust