// 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.controller.restapi.systemflags;

import com.yahoo.config.provision.SystemName;
import com.yahoo.vespa.flags.FlagId;
import com.yahoo.vespa.hosted.controller.api.systemflags.v1.FlagsTarget;
import com.yahoo.vespa.hosted.controller.api.systemflags.v1.wire.WireSystemFlagsDeployResult;
import org.junit.Test;

import java.util.List;

import static com.yahoo.vespa.hosted.controller.restapi.systemflags.SystemFlagsDeployResult.*;
import static org.assertj.core.api.Assertions.assertThat;

/**
 * @author bjorncs
 */
public class SystemFlagsDeployResultTest {
    @Test
    public void changes_and_errors_are_present_in_wire_format() {
        FlagsTarget controllerTarget = FlagsTarget.forController(SystemName.cd);
        FlagId flagOne = new FlagId("flagone");
        FlagId flagTwo = new FlagId("flagtwo");
        SystemFlagsDeployResult result = new SystemFlagsDeployResult(
                List.of(
                        FlagDataChange.deleted(flagOne, controllerTarget)),
                List.of(
                        OperationError.deleteFailed("delete failed", controllerTarget, flagTwo)));
        WireSystemFlagsDeployResult wire = result.toWire();

        assertThat(wire.changes).hasSize(1);
        assertThat(wire.changes.get(0).flagId).isEqualTo(flagOne.toString());
        assertThat(wire.errors).hasSize(1);
        assertThat(wire.errors.get(0).flagId).isEqualTo(flagTwo.toString());
    }
}
@@ -420,16 +420,153 @@
 Result;%0A
+import com.yahoo.vespa.hosted.controller.integration.ZoneApiMock;%0Aimport com.yahoo.vespa.hosted.controller.integration.ZoneRegistryMock;%0A
 import o
@@ -1687,10 +1687,1742 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void identical_errors_and_changes_from_multiple_targets_are_merged() %7B%0A        ZoneApiMock prodUsWest1Zone = ZoneApiMock.fromId(%22prod.us-west-1%22);%0A        ZoneRegistryMock registry = new ZoneRegistryMock(SystemName.cd).setZones(prodUsWest1Zone);%0A        FlagsTarget prodUsWest1Target = FlagsTarget.forConfigServer(registry, prodUsWest1Zone.getId());%0A        FlagsTarget controllerTarget = FlagsTarget.forController(SystemName.cd);%0A%0A        FlagId flagOne = new FlagId(%22flagone%22);%0A        FlagId flagTwo = new FlagId(%22flagtwo%22);%0A%0A        SystemFlagsDeployResult resultController =%0A                new SystemFlagsDeployResult(%0A                        List.of(FlagDataChange.deleted(flagOne, controllerTarget)),%0A                        List.of(OperationError.deleteFailed(%22message%22, controllerTarget, flagTwo)));%0A        SystemFlagsDeployResult resultProdUsWest1 =%0A                new SystemFlagsDeployResult(%0A                        List.of(FlagDataChange.deleted(flagOne, prodUsWest1Target)),%0A                        List.of(OperationError.deleteFailed(%22message%22, prodUsWest1Target, flagTwo)));%0A%0A        var results = List.of(resultController, resultProdUsWest1);%0A        SystemFlagsDeployResult mergedResult = merge(results);%0A%0A        List%3CFlagDataChange%3E changes = mergedResult.flagChanges();%0A        assertThat(changes).hasSize(1);%0A        FlagDataChange change = changes.get(0);%0A        assertThat(change.targets()).containsOnly(controllerTarget, prodUsWest1Target);%0A%0A        List%3COperationError%3E errors = mergedResult.errors();%0A        assertThat(errors).hasSize(1);%0A        OperationError error = errors.get(0);%0A        assertThat(error.targets()).containsOnly(controllerTarget, prodUsWest1Target);%0A    %7D
 %0A%7D

Add unit test for result merging