Use serialization in MemoryControllerDb
@@ -4,108 +4,80 @@ package com.yahoo.vespa.hosted.controller.persistence;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.TenantName;
import com.yahoo.vespa.hosted.controller.Application;
-import com.yahoo.vespa.hosted.controller.NotExistsException;
import com.yahoo.vespa.hosted.controller.tenant.AthenzTenant;
import com.yahoo.vespa.hosted.controller.tenant.Tenant;
import com.yahoo.vespa.hosted.controller.tenant.UserTenant;
-import java.util.ArrayList;
-import java.util.HashMap;
import java.util.List;
-import java.util.Map;
import java.util.Optional;
-import java.util.stream.Collectors;
/**
- * A controller db implementation backed by in-memory structures. Useful for testing.
+ * A controller db implementation backed by a mock curator. Useful for testing.
*
* @author Stian Kristoffersen
*/
public class MemoryControllerDb implements ControllerDb {
- private final Map<TenantName, Tenant> tenants = new HashMap<>();
- private final Map<String, Application> applications = new HashMap<>();
-
- private void createTenant(Tenant tenant) {
- if (tenants.containsKey(tenant.name())) {
- throw new IllegalArgumentException("Tenant '" + tenant + "' already exists");
- }
- tenants.put(tenant.name(), tenant);
- }
-
- private void updateTenant(Tenant tenant) {
- if (!tenants.containsKey(tenant.name())) {
- throw new NotExistsException(tenant.name().value());
- }
- tenants.put(tenant.name(), tenant);
- }
+ private final MockCuratorDb curator = new MockCuratorDb();
@Override
public void createTenant(UserTenant tenant) {
- createTenant((Tenant) tenant);
+ curator.writeTenant(tenant);
}
@Override
public void createTenant(AthenzTenant tenant) {
- createTenant((Tenant) tenant);
+ curator.writeTenant(tenant);
}
@Override
public void updateTenant(AthenzTenant tenant) {
- updateTenant((Tenant) tenant);
+ curator.writeTenant(tenant);
}
@Override
public void deleteTenant(TenantName name) {
- if (tenants.remove(name) == null) {
- throw new NotExistsException(name.value());
- }
+ curator.removeTenant(name);
}
@Override
public Optional<Tenant> getTenant(TenantName name) {
- return getTenant(name, Tenant.class);
+ return curator.readTenant(name);
}
@Override
public Optional<AthenzTenant> getAthenzTenant(TenantName name) {
- return getTenant(name, AthenzTenant.class);
- }
-
- private <T extends Tenant> Optional<T> getTenant(TenantName name, Class<T> type) {
- return Optional.ofNullable(tenants.get(name)).map(type::cast);
+ return curator.readAthenzTenant(name);
}
@Override
public List<Tenant> listTenants() {
- return new ArrayList<>(tenants.values());
+ return curator.readTenants();
}
@Override
public void store(Application application) {
- applications.put(path(application.id()), application);
+ curator.writeApplication(application);
}
@Override
public void deleteApplication(ApplicationId applicationId) {
- applications.remove(path(applicationId));
+ curator.removeApplication(applicationId);
}
@Override
public Optional<Application> getApplication(ApplicationId applicationId) {
- return Optional.ofNullable(applications.get(path(applicationId)));
+ return curator.readApplication(applicationId);
}
@Override
public List<Application> listApplications() {
- return new ArrayList<>(applications.values());
+ return curator.readApplications();
}
@Override
public List<Application> listApplications(TenantName name) {
- return applications.values().stream()
- .filter(a -> a.id().tenant().equals(name))
- .collect(Collectors.toList());
+ return curator.readApplications(name);
}
}
