@Override protected TenantApiException generateAlreadyExistsException(final TenantModelDao entity, final InternalCallContext context) { return new TenantApiException(ErrorCode.TENANT_ALREADY_EXISTS, entity.getExternalKey()); }
@Test(groups = "slow", description = "Test Tenant creation with External Key over limit") public void testCreateTenantWithExternalKeyOverLimit() throws Exception { final TenantData tenantdata = new DefaultTenant(UUID.randomUUID(), clock.getUTCNow(), clock.getUTCNow(), "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis,.", "TTR445ee2", "dskjhfs^^54R"); try { tenantUserApi.createTenant(tenantdata, callContext); Assert.fail(); } catch (final TenantApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.EXTERNAL_KEY_LIMIT_EXCEEDED.getCode()); } } }
@Override public Response toResponse(final TenantApiException exception) { if (exception.getCode() == ErrorCode.TENANT_ALREADY_EXISTS.getCode()) { return buildConflictingRequestResponse(exception, uriInfo); } else if (exception.getCode() == ErrorCode.TENANT_DOES_NOT_EXIST_FOR_ID.getCode()) { return buildNotFoundResponse(exception, uriInfo); } else if (exception.getCode() == ErrorCode.TENANT_DOES_NOT_EXIST_FOR_KEY.getCode()) { return buildNotFoundResponse(exception, uriInfo); } else if (exception.getCode() == ErrorCode.TENANT_DOES_NOT_EXIST_FOR_API_KEY.getCode()) { return buildNotFoundResponse(exception, uriInfo); } else if (exception.getCode() == ErrorCode.TENANT_CREATION_FAILED.getCode()) { return buildInternalErrorResponse(exception, uriInfo); } else if (exception.getCode() == ErrorCode.TENANT_UPDATE_FAILED.getCode()) { return buildInternalErrorResponse(exception, uriInfo); } else if (exception.getCode() == ErrorCode.TENANT_NO_SUCH_KEY.getCode()) { return buildNotFoundResponse(exception, uriInfo); } else { return fallback(exception, uriInfo); } } }
@Test(groups = "slow", description = "Test Tenant creation with External Key over limit") public void testCreateTenantWithExternalKeyOverLimit() throws Exception { final TenantData tenantdata = new DefaultTenant(UUID.randomUUID(), clock.getUTCNow(), clock.getUTCNow(), "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis,.", "TTR445ee2", "dskjhfs^^54R"); try { tenantUserApi.createTenant(tenantdata, callContext); Assert.fail(); } catch (final TenantApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.EXTERNAL_KEY_LIMIT_EXCEEDED.getCode()); } } }
@Override public Tenant getTenantByApiKey(final String key) throws TenantApiException { final Tenant tenant = tenantCache.get(key, new CacheLoaderArgument(ObjectType.TENANT)); if (tenant == null) { throw new TenantApiException(ErrorCode.TENANT_DOES_NOT_EXIST_FOR_API_KEY, key); } return tenant; }
@Override public Tenant getTenantByApiKey(final String key) throws TenantApiException { final TenantModelDao tenant = tenantDao.getTenantByApiKey(key); if (tenant == null) { throw new TenantApiException(ErrorCode.TENANT_DOES_NOT_EXIST_FOR_API_KEY, key); } return new DefaultTenant(tenant); }
@Override public Tenant getTenantById(final UUID id) throws TenantApiException { // TODO - API cleanup? final TenantModelDao tenant = tenantDao.getById(id, new InternalTenantContext(null)); if (tenant == null) { throw new TenantApiException(ErrorCode.TENANT_DOES_NOT_EXIST_FOR_ID, id); } return new DefaultTenant(tenant); }
@Override public Tenant createTenant(final TenantData data, final CallContext context) throws TenantApiException { final Tenant tenant = new DefaultTenant(data); if (null != tenant.getExternalKey() && tenant.getExternalKey().length() > 255) { throw new TenantApiException(ErrorCode.EXTERNAL_KEY_LIMIT_EXCEEDED); } try { // Not transactional, but there is a db constraint on that column if (data.getApiKey() != null && getTenantByApiKey(data.getApiKey()) != null) { throw new TenantApiException(ErrorCode.TENANT_ALREADY_EXISTS, data.getExternalKey()); } } catch (final RuntimeException e) { if (e.getCause() instanceof IllegalStateException) { // could happen exemption, stating that the key is not found } else { throw e; } } try { tenantDao.create(new TenantModelDao(tenant), internalCallContextFactory.createInternalCallContextWithoutAccountRecordId(context)); } catch (final TenantApiException e) { throw new TenantApiException(e, ErrorCode.TENANT_CREATION_FAILED); } return tenant; }
@Override protected TenantApiException generateAlreadyExistsException(final TenantModelDao entity, final InternalCallContext context) { return new TenantApiException(ErrorCode.TENANT_ALREADY_EXISTS, entity.getExternalKey()); }
@Override public Tenant getTenantByApiKey(final String key) throws TenantApiException { final Tenant tenant = tenantCache.get(key, new CacheLoaderArgument(ObjectType.TENANT)); if (tenant == null) { throw new TenantApiException(ErrorCode.TENANT_DOES_NOT_EXIST_FOR_API_KEY, key); } return tenant; }
@Override public Tenant getTenantByApiKey(final String key) throws TenantApiException { final TenantModelDao tenant = tenantDao.getTenantByApiKey(key); if (tenant == null) { throw new TenantApiException(ErrorCode.TENANT_DOES_NOT_EXIST_FOR_API_KEY, key); } return new DefaultTenant(tenant); }
@Override public Tenant getTenantById(final UUID id) throws TenantApiException { // TODO - API cleanup? final TenantModelDao tenant = tenantDao.getById(id, new InternalTenantContext(null)); if (tenant == null) { throw new TenantApiException(ErrorCode.TENANT_DOES_NOT_EXIST_FOR_ID, id); } return new DefaultTenant(tenant); }
@Override public Tenant createTenant(final TenantData data, final CallContext context) throws TenantApiException { final Tenant tenant = new DefaultTenant(data); if (null != tenant.getExternalKey() && tenant.getExternalKey().length() > 255) { throw new TenantApiException(ErrorCode.EXTERNAL_KEY_LIMIT_EXCEEDED); } try { // Not transactional, but there is a db constraint on that column if (data.getApiKey() != null && getTenantByApiKey(data.getApiKey()) != null) { throw new TenantApiException(ErrorCode.TENANT_ALREADY_EXISTS, data.getExternalKey()); } } catch (final RuntimeException e) { if (e.getCause() instanceof IllegalStateException) { // could happen exemption, stating that the key is not found } else { throw e; } } try { tenantDao.create(new TenantModelDao(tenant), internalCallContextFactory.createInternalCallContextWithoutAccountRecordId(context)); } catch (final TenantApiException e) { throw new TenantApiException(e, ErrorCode.TENANT_CREATION_FAILED); } return tenant; }