@Test public void testNonSuperadminReturnsError() throws Exception { assertResponseStatus(doGetExternalAPI("/tenants", ADMIN_HEADERS), HttpResponseStatus.NOT_FOUND); assertResponseStatus(doGetExternalAPI("/tenants/123", ADMIN_HEADERS), HttpResponseStatus.NOT_FOUND); assertResponseStatus(doPostExternalAPI("/tenants", "", ADMIN_HEADERS), HttpResponseStatus.NOT_FOUND); assertResponseStatus(doPutExternalAPI("/tenants/123", "", ADMIN_HEADERS), HttpResponseStatus.NOT_FOUND); }
@Test public void testBadRequestReturns400() throws Exception { // test malformed requests assertResponseStatus(doPostExternalAPI("/tenants", "{}", SUPERADMIN_HEADERS), HttpResponseStatus.BAD_REQUEST); assertResponseStatus(doPostExternalAPI("/tenants", "", SUPERADMIN_HEADERS), HttpResponseStatus.BAD_REQUEST); // id in object does not match id in path TenantSpecification tenantSpecification = new TenantSpecification("name", 10, 10, 10); assertResponseStatus(doPutExternalAPI("/tenants/10", gson.toJson(tenantSpecification), SUPERADMIN_HEADERS), HttpResponseStatus.BAD_REQUEST); // missing id in object tenantSpecification = new TenantSpecification("name", 10, 10, 10); assertResponseStatus(doPutExternalAPI("/tenants/10", gson.toJson(tenantSpecification), SUPERADMIN_HEADERS), HttpResponseStatus.BAD_REQUEST); }
@Test public void testDeleteSuperadminForbidden() throws Exception { assertResponseStatus(doDeleteExternalAPI("/tenants/" + Constants.SUPERADMIN_TENANT, SUPERADMIN_HEADERS), HttpResponseStatus.FORBIDDEN); }
@Test public void testDuplicateTenantNameNotAllowed() throws Exception { String name = "companyX"; TenantSpecification requestedTenant = new TenantSpecification(name, 10, 100, 1000); TenantWriteRequest addRequest = new TenantWriteRequest(requestedTenant); assertResponseStatus(doPostExternalAPI("/tenants", gson.toJson(addRequest), SUPERADMIN_HEADERS), HttpResponseStatus.OK); assertResponseStatus(doPostExternalAPI("/tenants", gson.toJson(addRequest), SUPERADMIN_HEADERS), HttpResponseStatus.CONFLICT); }
@Test public void testMissingObjectReturn404() throws Exception { assertResponseStatus(doGetExternalAPI("/tenants/123", SUPERADMIN_HEADERS), HttpResponseStatus.NOT_FOUND); }
@Test public void testGetTenant() throws Exception { // write tenant to store TenantSpecification spec = new TenantSpecification("companyX", 10, 100, 1000); tenantProvisionerService.writeTenantSpecification(spec); // perform request to get tenant HttpResponse response = doGetExternalAPI("/tenants/" + spec.getName(), SUPERADMIN_HEADERS); assertResponseStatus(response, HttpResponseStatus.OK); Reader reader = new InputStreamReader(response.getEntity().getContent()); Assert.assertEquals(spec, gson.fromJson(reader, TenantSpecification.class)); }
@Test public void testCreateTenantWithTooManyWorkersReturnsConflict() throws Exception { TenantSpecification requestedTenant = new TenantSpecification("companyX", 10000, 100, 1000); TenantWriteRequest addRequest = new TenantWriteRequest(requestedTenant); HttpResponse response = doPostExternalAPI("/tenants", gson.toJson(addRequest), SUPERADMIN_HEADERS); // perform create request assertResponseStatus(response, HttpResponseStatus.CONFLICT); }
@Test public void testDeleteTenant() throws Exception { String id = UUID.randomUUID().toString(); tenantProvisionerService.writeTenantSpecification(new TenantSpecification("companyX", 0, 100, 1000)); // perform request to delete tenant HttpResponse response = doDeleteExternalAPI("/tenants/" + id, SUPERADMIN_HEADERS); assertResponseStatus(response, HttpResponseStatus.OK); Assert.assertNull(tenantStore.getTenantByID(id)); }
@Test public void testGetAllTenants() throws Exception { // write tenants to store TenantSpecification spec1 = new TenantSpecification("companyX", 2, 100, 1000); TenantSpecification spec2 = new TenantSpecification("companyY", 3, 1000, 10000); tenantProvisionerService.writeTenantSpecification(spec1); tenantProvisionerService.writeTenantSpecification(spec2); // perform request to get tenant HttpResponse response = doGetExternalAPI("/tenants", SUPERADMIN_HEADERS); assertResponseStatus(response, HttpResponseStatus.OK); Reader reader = new InputStreamReader(response.getEntity().getContent()); Set<Tenant> actualTenants = gson.fromJson(reader, new TypeToken<Set<TenantSpecification>>() {}.getType()); Assert.assertEquals(ImmutableSet.of(Tenant.DEFAULT_SUPERADMIN.getSpecification(), spec1, spec2), actualTenants); }
@Test public void testDeleteTenantWithNonzeroWorkersFails() throws Exception { String id = UUID.randomUUID().toString(); String name = "companyX"; Tenant tenant = new Tenant(id, new TenantSpecification(name, 10, 100, 1000)); tenantStore.writeTenant(tenant); tenantProvisionerService.rebalanceTenantWorkers(id); // perform request to delete tenant assertResponseStatus(doDeleteExternalAPI("/tenants/" + name, SUPERADMIN_HEADERS), HttpResponseStatus.CONFLICT); }
@Test public void testWriteTenantWithTooManyWorkersReturnsConflict() throws Exception { // write tenant to store String id = UUID.randomUUID().toString(); String name = "companyX"; Tenant actualTenant = new Tenant(id, new TenantSpecification(name, 10, 100, 1000)); tenantStore.writeTenant(actualTenant); // perform request to write tenant TenantSpecification updatedFields = new TenantSpecification(name, 100000, 100, 500); TenantWriteRequest writeRequest = new TenantWriteRequest(updatedFields); HttpResponse response = doPutExternalAPI("/tenants/" + name, gson.toJson(writeRequest), SUPERADMIN_HEADERS); assertResponseStatus(response, HttpResponseStatus.CONFLICT); }
@Test public void testWriteTenant() throws Exception { // write tenant to store String id = UUID.randomUUID().toString(); Tenant actualTenant = new Tenant(id, new TenantSpecification("companyX", 10, 100, 1000)); tenantStore.writeTenant(actualTenant); // perform request to write tenant TenantSpecification updatedTenant = new TenantSpecification("companyX", 10, 100, 500); TenantWriteRequest writeRequest = new TenantWriteRequest(updatedTenant); HttpResponse response = doPutExternalAPI("/tenants/" + updatedTenant.getName(), gson.toJson(writeRequest), SUPERADMIN_HEADERS); assertResponseStatus(response, HttpResponseStatus.OK); Assert.assertEquals(updatedTenant, tenantStore.getTenantByID(actualTenant.getId()).getSpecification()); }
TenantWriteRequest tenantWriteRequest = new TenantWriteRequest(requestedTenant, true); HttpResponse response = doPostExternalAPI("/tenants", gson.toJson(tenantWriteRequest), SUPERADMIN_HEADERS); assertResponseStatus(response, HttpResponseStatus.OK);
@Test public void testCreateTenant() throws Exception { String name = "companyX"; TenantSpecification requestedTenant = new TenantSpecification(name, 10, 100, 1000); TenantWriteRequest tenantWriteRequest = new TenantWriteRequest(requestedTenant); HttpResponse response = doPostExternalAPI("/tenants", gson.toJson(tenantWriteRequest), SUPERADMIN_HEADERS); // perform create request assertResponseStatus(response, HttpResponseStatus.OK); // make sure tenant was actually written TenantSpecification actualTenant = tenantStore.getTenantByName(name).getSpecification(); Assert.assertEquals(requestedTenant.getName(), actualTenant.getName()); Assert.assertEquals(requestedTenant.getWorkers(), actualTenant.getWorkers()); Assert.assertEquals(requestedTenant.getMaxClusters(), actualTenant.getMaxClusters()); Assert.assertEquals(requestedTenant.getMaxNodes(), actualTenant.getMaxNodes()); }