// 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.persistence;

import com.google.common.collect.ImmutableSet;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.ClusterSpec;
import com.yahoo.config.provision.HostName;
import com.yahoo.config.provision.RotationName;
import com.yahoo.config.provision.zone.ZoneId;
import com.yahoo.vespa.hosted.controller.application.RoutingPolicy;
import org.junit.Test;

import java.util.Optional;
import java.util.Set;

import static org.junit.Assert.assertEquals;

/**
 * @author mortent
 */
public class RoutingPolicySerializerTest {

    private final RoutingPolicySerializer serializer = new RoutingPolicySerializer();

    @Test
    public void test_serialization() {
        ApplicationId owner = ApplicationId.defaultId();
        Set<RotationName> rotations = Set.of(RotationName.from("r1"), RotationName.from("r2"));
        Set<RoutingPolicy> loadBalancers = ImmutableSet.of(new RoutingPolicy(owner,
                                                                             ClusterSpec.Id.from("my-cluster1"),
                                                                             ZoneId.from("prod", "us-north-1"),
                                                                             HostName.from("long-and-ugly-name"),
                                                                             Optional.of("zone1"),
                                                                             rotations),
                                                           new RoutingPolicy(owner,
                                                                             ClusterSpec.Id.from("my-cluster2"),
                                                                             ZoneId.from("prod", "us-north-2"),
                                                                             HostName.from("long-and-ugly-name-2"),
                                                                             Optional.empty(),
                                                                             rotations));
        Set<RoutingPolicy> serialized = serializer.fromSlime(owner, serializer.toSlime(loadBalancers));
        assertEquals(loadBalancers, serialized);
    }

}

@@ -532,24 +532,51 @@
 unit.Test;%0A%0A
+import java.util.Iterator;%0A
 import java.
@@ -590,16 +590,16 @@
 tional;%0A
-
 import j
@@ -874,29 +874,19 @@
         
-ApplicationId
+var
  owner =
@@ -925,25 +925,11 @@
     
-Set%3CRotationName%3E
+var
  rot
@@ -1007,39 +1007,19 @@
     
-Set%3CRoutingPolicy%3E loadBalancer
+var policie
 s = 
@@ -1059,36 +1059,16 @@
 (owner,%0A
-                    
         
@@ -1209,36 +1209,16 @@
         
-                    
 ZoneId.f
@@ -1301,36 +1301,16 @@
         
-        
-            
 HostName
@@ -1338,36 +1338,16 @@
 name%22),%0A
-                    
         
@@ -1474,36 +1474,16 @@
         
-                    
 rotation
@@ -1525,36 +1525,16 @@
         
-    
-                
 new Rout
@@ -1550,36 +1550,16 @@
 (owner,%0A
-                    
         
@@ -1700,36 +1700,16 @@
         
-                   
- 
 ZoneId.f
@@ -1735,36 +1735,16 @@
 th-2%22),%0A
-                    
         
@@ -1888,36 +1888,16 @@
         
-       
-             
 Optional
@@ -1967,141 +1967,528 @@
     
-                    rotations));%0A        Set%3CRoutingPolicy%3E serialized = serializer.fromSlime(owner, serializer.toSlime(loadBalancers
+rotations));%0A        var serialized = serializer.fromSlime(owner, serializer.toSlime(policies));%0A        assertEquals(policies.size(), serialized.size());%0A        for (Iterator%3CRoutingPolicy%3E it1 = policies.iterator(), it2 = serialized.iterator(); it1.hasNext();) %7B%0A            var expected = it1.next();%0A            var actual = it2.next();%0A            assertEquals(expected.owner(), actual.owner());%0A            assertEquals(expected.cluster(), actual.cluster());%0A            assertEquals(expected.zone(), actual.zone(
 ));%0A
@@ -2487,32 +2487,36 @@
 one());%0A        
+ 
+   
 assertEquals(loa
@@ -2516,35 +2516,200 @@
 als(
-loadBalancers, serialized);
+expected.canonicalName(), actual.canonicalName());%0A            assertEquals(expected.dnsZone(), actual.dnsZone());%0A            assertEquals(expected.rotations(), actual.rotations());%0A        %7D
 %0A   

Test all fields