From b2a28796bc3e105ae9d0206ccfa747be5acff15a Mon Sep 17 00:00:00 2001 From: Valerij Fredriksen Date: Thu, 24 Oct 2019 14:36:15 +0200 Subject: Use Set instead and add basic tests --- .../api/integration/ApplicationIdSnapshot.java | 28 +++++----- .../api/integration/ApplicationIdSnapshotTest.java | 59 ++++++++++++++++++++++ 2 files changed, 73 insertions(+), 14 deletions(-) create mode 100644 controller-api/src/test/java/com/yahoo/vespa/hosted/controller/api/integration/ApplicationIdSnapshotTest.java (limited to 'controller-api') diff --git a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/ApplicationIdSnapshot.java b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/ApplicationIdSnapshot.java index e172c53163c..3f5a56207c5 100644 --- a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/ApplicationIdSnapshot.java +++ b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/ApplicationIdSnapshot.java @@ -6,43 +6,43 @@ import com.yahoo.config.provision.ApplicationName; import com.yahoo.config.provision.InstanceName; import com.yahoo.config.provision.TenantName; -import java.util.ArrayList; import java.util.HashMap; -import java.util.List; +import java.util.HashSet; import java.util.Map; import java.util.Optional; +import java.util.Set; import java.util.stream.Collectors; /** * @author freva */ public class ApplicationIdSnapshot { - private final Map>> instanceByApplicationByTenantName; + private final Map>> instanceByApplicationByTenantName; - public ApplicationIdSnapshot(Map>> instanceByApplicationByTenantName) { + public ApplicationIdSnapshot(Map>> instanceByApplicationByTenantName) { this.instanceByApplicationByTenantName = instanceByApplicationByTenantName.entrySet().stream() .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, e -> e.getValue().entrySet().stream() - .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, f -> List.copyOf(f.getValue()))))); + .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, f -> Set.copyOf(f.getValue()))))); } - public List tenants() { - return List.copyOf(instanceByApplicationByTenantName.keySet()); + public Set tenants() { + return instanceByApplicationByTenantName.keySet(); } - public List applications(TenantName tenantName) { + public Set applications(TenantName tenantName) { return Optional.ofNullable(instanceByApplicationByTenantName.get(tenantName)) - .map(a -> List.copyOf(a.keySet())) - .orElseGet(List::of); + .map(Map::keySet) + .orElseGet(Set::of); } - public List instances(TenantName tenantName, ApplicationName applicationName) { + public Set instances(TenantName tenantName, ApplicationName applicationName) { return instanceByApplicationByTenantName.getOrDefault(tenantName, Map.of()) - .getOrDefault(applicationName, List.of()); + .getOrDefault(applicationName, Set.of()); } public static class Builder { - private final Map>> instanceByApplicationByTenantName = new HashMap<>(); + private final Map>> instanceByApplicationByTenantName = new HashMap<>(); public Builder add(TenantName tenantName) { instanceByApplicationByTenantName.computeIfAbsent(tenantName, t -> new HashMap<>()); @@ -51,7 +51,7 @@ public class ApplicationIdSnapshot { public Builder add(TenantName tenantName, ApplicationName applicationName) { instanceByApplicationByTenantName.computeIfAbsent(tenantName, t -> new HashMap<>()) - .computeIfAbsent(applicationName, a -> new ArrayList<>()); + .computeIfAbsent(applicationName, a -> new HashSet<>()); return this; } diff --git a/controller-api/src/test/java/com/yahoo/vespa/hosted/controller/api/integration/ApplicationIdSnapshotTest.java b/controller-api/src/test/java/com/yahoo/vespa/hosted/controller/api/integration/ApplicationIdSnapshotTest.java new file mode 100644 index 00000000000..53d9cdf8f55 --- /dev/null +++ b/controller-api/src/test/java/com/yahoo/vespa/hosted/controller/api/integration/ApplicationIdSnapshotTest.java @@ -0,0 +1,59 @@ +// 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.api.integration; + +import com.yahoo.config.provision.ApplicationName; +import com.yahoo.config.provision.InstanceName; +import com.yahoo.config.provision.TenantName; +import org.junit.Test; + +import java.util.Set; + +import static org.junit.Assert.assertEquals; + +/** + * @author freva + */ +public class ApplicationIdSnapshotTest { + private static final TenantName tenant1 = TenantName.from("tenant1"); + private static final TenantName tenant2 = TenantName.from("tenant2"); + private static final TenantName tenant3 = TenantName.from("tenant3"); + private static final TenantName tenant4 = TenantName.from("tenant4"); + private static final ApplicationName app1 = ApplicationName.from("app1"); + private static final ApplicationName app2 = ApplicationName.from("app2"); + private static final ApplicationName app3 = ApplicationName.from("app3"); + private static final InstanceName instance1 = InstanceName.defaultName(); + private static final InstanceName instance2 = InstanceName.from("instance2"); + private static final InstanceName instance3 = InstanceName.from("instance3"); + + @Test + public void basic() { + ApplicationIdSnapshot snapshot = new ApplicationIdSnapshot.Builder() + .add(tenant1, app1, instance1) + .add(tenant1, app2, instance1) + .add(tenant1, app3) + .add(tenant1, app2, instance2) + .add(tenant2, app2, instance3) + .add(tenant3, app1) + .add(tenant4) + .build(); + + assertEquals(Set.of(tenant1, tenant2, tenant3, tenant4), snapshot.tenants()); + + assertEquals(Set.of(app1, app2, app3), snapshot.applications(tenant1)); + assertEquals(Set.of(app2), snapshot.applications(tenant2)); + assertEquals(Set.of(), snapshot.applications(tenant4)); + + assertEquals(Set.of(instance1), snapshot.instances(tenant1, app1)); + assertEquals(Set.of(instance1, instance2), snapshot.instances(tenant1, app2)); + assertEquals(Set.of(), snapshot.instances(tenant3, app1)); + } + + @Test + public void test_missing() { + ApplicationIdSnapshot snapshot = new ApplicationIdSnapshot.Builder().build(); + + assertEquals(Set.of(), snapshot.tenants()); + assertEquals(Set.of(), snapshot.applications(tenant1)); + assertEquals(Set.of(), snapshot.instances(tenant1, app1)); + } +} \ No newline at end of file -- cgit v1.2.3