// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.provision.os;

import com.yahoo.component.Version;
import com.yahoo.config.provision.NodeType;
import com.yahoo.vespa.hosted.provision.NodeRepositoryTester;
import org.junit.Test;

import java.time.Duration;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
 * @author mpolden
 */
public class OsVersionsTest {

    @Test
    public void test_versions() {
        var versions = new OsVersions(new NodeRepositoryTester().nodeRepository().database(), Duration.ofDays(1));

        assertTrue("No versions set", versions.targets().isEmpty());
        assertSame("Caches empty target versions", versions.targets(), versions.targets());

        // Upgrade OS
        var version1 = new OsVersion(Version.fromString("7.1"), false);
        versions.setTarget(NodeType.host, version1.version(), false);
        var targetVersions = versions.targets();
        assertSame("Caches target versions", targetVersions, versions.targets());
        assertEquals(version1, versions.targetFor(NodeType.host).get());

        // Upgrade OS again
        var version2 = new OsVersion(Version.fromString("7.2"), false);
        versions.setTarget(NodeType.host, version2.version(), false);
        assertNotSame("Cache invalidated", targetVersions, versions.targets());
        assertEquals(version2, versions.targetFor(NodeType.host).get());

        // Target can be (de)activated
        versions.setActive(NodeType.host, true);
        assertTrue("Target version deactivated", versions.targetFor(NodeType.host).get().active());
        versions.setActive(NodeType.host, false);
        assertFalse("Target version deactivated", versions.targetFor(NodeType.host).get().active());

        // Downgrading fails
        try {
            versions.setTarget(NodeType.host, version1.version(), false);
            fail("Expected exception");
        } catch (IllegalArgumentException ignored) {}

        // Forcing downgrade succeeds
        versions.setTarget(NodeType.host, version1.version(), true);
        assertEquals(version1, versions.targetFor(NodeType.host).get());

        // Target can be removed
        versions.removeTarget(NodeType.host);
        assertFalse(versions.targetFor(NodeType.host).isPresent());
    }

}

@@ -1793,34 +1793,32 @@
 %22Target version 
-de
 activated%22, vers
@@ -1802,32 +1802,275 @@
 ersion activated
+%22, versions.targetFor(NodeType.host).get().active());%0A%0A        // Re-setting the same version does not affect active status%0A        versions.setTarget(NodeType.host, version2.version(), false);%0A        assertTrue(%22Target version remains active
 %22, versions.targ
@@ -2103,24 +2103,25 @@
 .active());%0A
+%0A
         vers

Update test