/*
 * Copyright (C) 2026  Tim Zimmermann <tim@aglais.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.aglais.zms.db.core.model;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

/**
 * Testcases for {@link ModifiedStateTracker}
 */
public class ModifiedStateTrackerTest {

    private User createUser() {
        return new User(0, "Admin", new char[0]);
    }

    @Test
    public void testConstructor() {
        User user = createUser();
        ModifiedStateTracker<User> tracker = new ModifiedStateTracker<>(user);

        assertEquals(user, tracker.getData());
        assertFalse(tracker.isModified());
    }

    @Test
    public void testModified() {
        User user = createUser();
        ModifiedStateTracker<User> tracker = new ModifiedStateTracker<>(user);

        tracker.getData().setID(tracker.getData().getID() + 1);
        assertTrue(tracker.isModified());
    }

    @Test
    public void testDiscardChanges() {
        User user = createUser();
        ModifiedStateTracker<User> tracker = new ModifiedStateTracker<>(user);

        tracker.getData().setID(tracker.getData().getID() + 1);
        assertTrue(tracker.isModified());
        tracker.discardChanges();
        assertFalse(tracker.isModified());
    }

    @Test
    public void testManualDiscardChanges() {
        User user = createUser();
        ModifiedStateTracker<User> tracker = new ModifiedStateTracker<>(user);

        tracker.getData().setID(tracker.getData().getID() + 1);
        assertTrue(tracker.isModified());
        tracker.getData().setID(tracker.getData().getID() - 1);
        assertFalse(tracker.isModified());
    }

    @Test
    public void testApplyChanges() {
        User user = createUser();
        ModifiedStateTracker<User> tracker = new ModifiedStateTracker<>(user);

        tracker.getData().setID(tracker.getData().getID() + 1);
        assertTrue(tracker.isModified());
        tracker.applyChanges();
        assertFalse(tracker.isModified());
    }
}
