/*
 * This file is part of Dependency-Track.
 *
 * 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.
 *
 * SPDX-License-Identifier: Apache-2.0
 * Copyright (c) OWASP Foundation. All Rights Reserved.
 */
package org.dependencytrack.util;

import org.dependencytrack.PersistenceCapableTest;
import org.dependencytrack.model.Project;
import org.dependencytrack.util.PersistenceUtil.Diff;
import org.dependencytrack.util.PersistenceUtil.Differ;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import javax.jdo.PersistenceManager;
import javax.jdo.Transaction;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatNoException;
import static org.dependencytrack.util.PersistenceUtil.assertNonPersistent;
import static org.dependencytrack.util.PersistenceUtil.assertPersistent;

class PersistenceUtilTest extends PersistenceCapableTest {

    private PersistenceManager pm;

    @BeforeEach
    public void setUp() {
        pm = qm.getPersistenceManager();
    }

    @Test
    void testAssertPersistentTx() {
        final Transaction trx = pm.currentTransaction();
        try {
            trx.begin();

            final var project = new Project();
            project.setName("foo");
            pm.makePersistent(project);

            assertThatNoException()
                    .isThrownBy(() -> assertPersistent(project, null));
        } finally {
            trx.rollback();
        }
    }

    @Test
    void testAssertPersistentNonTx() {
        final var project = new Project();
        project.setName("foo");
        pm.makePersistent(project);

        assertThatNoException()
                .isThrownBy(() -> assertPersistent(project, null));
    }

    @Test
    void testAssertPersistentWhenTransient() {
        final var project = new Project();
        assertThatExceptionOfType(IllegalStateException.class)
                .isThrownBy(() -> assertPersistent(project, null));
    }

    @Test
    void testAssertPersistentWhenDetached() {
        final var project = new Project();
        project.setName("foo");
        pm.makePersistent(project);

        assertThatExceptionOfType(IllegalStateException.class)
                .isThrownBy(() -> assertPersistent(pm.detachCopy(project), null));
    }

    @Test
    void testAssertNonPersistentTx() {
        final Transaction trx = pm.currentTransaction();
        try {
            trx.begin();

            final var project = new Project();
            project.setName("foo");
            pm.makePersistent(project);

            assertThatExceptionOfType(IllegalStateException.class)
                    .isThrownBy(() -> assertNonPersistent(project, null));
        } finally {
            trx.rollback();
        }
    }

    @Test
    void testAssertNonPersistentNonTx() {
        final var project = new Project();
        project.setName("foo");
        pm.makePersistent(project);

        assertThatExceptionOfType(IllegalStateException.class)
                .isThrownBy(() -> assertNonPersistent(project, null));
    }

    @Test
    void testAssertNonPersistentWhenTransient() {
        final var project = new Project();
        assertThatNoException()
                .isThrownBy(() -> assertNonPersistent(project, null));
    }

    @Test
    void testAssertNonPersistentWhenDetached() {
        final var project = new Project();
        project.setName("foo");
        pm.makePersistent(project);

        assertThatNoException()
                .isThrownBy(() -> assertNonPersistent(pm.detachCopy(project), null));
    }


    @Test
    void testDifferWithChanges() {
        final var projectA = new Project();
        projectA.setName("acme-app-a");
        projectA.setVersion("1.0.0");
        projectA.setDescription("identicalDescription");

        final var projectB = new Project();
        projectB.setName("acme-app-b");
        projectB.setVersion("2.0.0");
        projectB.setDescription("identicalDescription");

        final var differ = new Differ<>(projectA, projectB);
        assertThat(differ.applyIfChanged("name", Project::getName, projectB::setName)).isTrue();
        assertThat(differ.applyIfChanged("version", Project::getVersion, projectB::setVersion)).isTrue();
        assertThat(differ.applyIfChanged("description", Project::getDescription, projectB::setDescription)).isFalse();

        assertThat(differ.getDiffs()).containsOnly(
                Map.entry("name", new Diff("acme-app-a", "acme-app-b")),
                Map.entry("version", new Diff("1.0.0", "2.0.0"))
        );
    }

    @Test
    void testDifferWithoutChanges() {
        final var projectA = new Project();
        projectA.setName("acme-app-a");
        projectA.setVersion("1.0.0");
        projectA.setDescription("identicalDescription");

        final var projectB = new Project();
        projectB.setName("acme-app-a");
        projectB.setVersion("1.0.0");
        projectB.setDescription("identicalDescription");

        final var differ = new Differ<>(projectA, projectB);
        assertThat(differ.applyIfChanged("name", Project::getName, projectB::setName)).isFalse();
        assertThat(differ.applyIfChanged("version", Project::getVersion, projectB::setVersion)).isFalse();
        assertThat(differ.applyIfChanged("description", Project::getDescription, projectB::setDescription)).isFalse();

        assertThat(differ.getDiffs()).isEmpty();
    }

}