@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); }
@Test(groups = "slow") public void testWeCanStoreAndMatchCredentials() throws Exception { final DefaultTenant tenant = new DefaultTenant(UUID.randomUUID(), null, null, UUID.randomUUID().toString(), UUID.randomUUID().toString(), UUID.randomUUID().toString()); tenantDao.create(new TenantModelDao(tenant), internalCallContext); // Verify we can retrieve it Assert.assertEquals(tenantDao.getTenantByApiKey(tenant.getApiKey()).getId(), tenant.getId()); // Verify we can authenticate against it final AuthenticationInfo authenticationInfo = tenantDao.getAuthenticationInfoForTenant(tenant.getId()); // Good combo final AuthenticationToken goodToken = new UsernamePasswordToken(tenant.getApiKey(), tenant.getApiSecret()); Assert.assertTrue(KillbillCredentialsMatcher.getCredentialsMatcher(securityConfig).doCredentialsMatch(goodToken, authenticationInfo)); // Bad combo final AuthenticationToken badToken = new UsernamePasswordToken(tenant.getApiKey(), tenant.getApiSecret() + "T"); Assert.assertFalse(KillbillCredentialsMatcher.getCredentialsMatcher(securityConfig).doCredentialsMatch(badToken, authenticationInfo)); }
@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); }
@Test(groups = "slow") public void testWeCanStoreAndMatchCredentials() throws Exception { final DefaultTenant tenant = new DefaultTenant(UUID.randomUUID(), null, null, UUID.randomUUID().toString(), UUID.randomUUID().toString(), UUID.randomUUID().toString()); tenantDao.create(new TenantModelDao(tenant), internalCallContext); // Verify we can retrieve it Assert.assertEquals(tenantDao.getTenantByApiKey(tenant.getApiKey()).getId(), tenant.getId()); // Verify we can authenticate against it final AuthenticationInfo authenticationInfo = tenantDao.getAuthenticationInfoForTenant(tenant.getId()); // Good combo final AuthenticationToken goodToken = new UsernamePasswordToken(tenant.getApiKey(), tenant.getApiSecret()); Assert.assertTrue(KillbillCredentialsMatcher.getCredentialsMatcher(securityConfig).doCredentialsMatch(goodToken, authenticationInfo)); // Bad combo final AuthenticationToken badToken = new UsernamePasswordToken(tenant.getApiKey(), tenant.getApiSecret() + "T"); Assert.assertFalse(KillbillCredentialsMatcher.getCredentialsMatcher(securityConfig).doCredentialsMatch(badToken, authenticationInfo)); }
@Test(groups = "slow") public void testTenantKeyValueUpdate() throws Exception { final DefaultTenant tenant = new DefaultTenant(UUID.randomUUID(), null, null, UUID.randomUUID().toString(), UUID.randomUUID().toString(), UUID.randomUUID().toString()); tenantDao.create(new TenantModelDao(tenant), internalCallContext); tenantDao .addTenantKeyValue("MY_KEY", "TheValue1", false, internalCallContext); tenantDao .addTenantKeyValue("MY_KEY", "TheValue2", false, internalCallContext); tenantDao .addTenantKeyValue("MY_KEY", "TheValue3", false, internalCallContext); final List<String> value = tenantDao.getTenantValueForKey("MY_KEY", internalCallContext); Assert.assertEquals(value.size(), 3); tenantDao.updateTenantLastKeyValue("MY_KEY", "NewValue3", internalCallContext); final List<String> newValues = tenantDao.getTenantValueForKey("MY_KEY", internalCallContext); Assert.assertEquals(newValues.size(), 3); Assert.assertEquals(newValues.get(0), "TheValue1"); Assert.assertEquals(newValues.get(1), "TheValue2"); Assert.assertEquals(newValues.get(2), "NewValue3"); }
@Test(groups = "slow") public void testTenantKeyValue() throws Exception { final DefaultTenant tenant = new DefaultTenant(UUID.randomUUID(), null, null, UUID.randomUUID().toString(), UUID.randomUUID().toString(), UUID.randomUUID().toString()); tenantDao.create(new TenantModelDao(tenant), internalCallContext); tenantDao .addTenantKeyValue("THE_KEY", "TheValue", false, internalCallContext); List<String> value = tenantDao.getTenantValueForKey("THE_KEY", internalCallContext); Assert.assertEquals(value.size(), 1); Assert.assertEquals(value.get(0), "TheValue"); tenantDao.addTenantKeyValue("THE_KEY", "TheSecondValue", false, internalCallContext); value = tenantDao.getTenantValueForKey("THE_KEY", internalCallContext); Assert.assertEquals(value.size(), 2); value = tenantDao.getTenantValueForKey("THE_KEY", internalCallContext); Assert.assertEquals(value.size(), 2); tenantDao.deleteTenantKey("THE_KEY", internalCallContext); value = tenantDao.getTenantValueForKey("THE_KEY", internalCallContext); Assert.assertEquals(value.size(), 0); }
@Test(groups = "slow") public void testTenantSearch() throws Exception { final DefaultTenant tenant = new DefaultTenant(UUID.randomUUID(), null, null, UUID.randomUUID().toString(), UUID.randomUUID().toString(), UUID.randomUUID().toString()); tenantDao.create(new TenantModelDao(tenant), internalCallContext); tenantDao .addTenantKeyValue("foobar", "foobar1", false, internalCallContext); tenantDao .addTenantKeyValue("foobar", "foobar2", false, internalCallContext); tenantDao .addTenantKeyValue("foobar", "foobar3", false, internalCallContext); tenantDao.updateTenantLastKeyValue("foo", "foo1", internalCallContext); tenantDao.updateTenantLastKeyValue("fooXX", "fooXX1", internalCallContext); tenantDao.updateTenantLastKeyValue("bar", "bar", internalCallContext); final List<TenantKVModelDao> result = tenantDao.searchTenantKeyValues("foo", internalCallContext); Assert.assertEquals(result.size(), 5); }
@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()); } } }
@Test(groups = "fast") public void testExternalizable() throws IOException { final DefaultTenant tenantdata = new DefaultTenant(UUID.randomUUID(), clock.getUTCNow(), clock.getUTCNow(), "er44TT-yy4r", "TTR445ee2", null); final Codec code = new SerializationCodec(); final ByteBuf byteBuf = code.getValueEncoder().encode(tenantdata); final DefaultTenant tenantData2 = (DefaultTenant) code.getValueDecoder().decode(byteBuf, null); Assert.assertEquals(tenantData2, tenantdata); } }
@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; }
@Test(groups = "slow") public void testTenant() throws Exception { final TenantData tenantdata = new DefaultTenant(UUID.randomUUID(), clock.getUTCNow(), clock.getUTCNow(), "er44TT-yy4r", "TTR445ee2", "dskjhfs^^54R"); tenantUserApi.createTenant(tenantdata, callContext); final Tenant tenant = tenantUserApi.getTenantByApiKey(tenantdata.getApiKey()); Assert.assertEquals(tenant.getApiKey(), tenantdata.getApiKey()); Assert.assertEquals(tenant.getExternalKey(), tenantdata.getExternalKey()); // The second time, the value is already in the cache so the TenantCacheLoader is not invoked final Tenant tenant2 = tenantUserApi.getTenantByApiKey(tenantdata.getApiKey()); Assert.assertEquals(tenant2.getApiKey(), tenantdata.getApiKey()); Assert.assertEquals(tenant2.getExternalKey(), tenantdata.getExternalKey()); }
@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); }
@Test(groups = "slow") public void testTenantKeyValueUpdate() throws Exception { final DefaultTenant tenant = new DefaultTenant(UUID.randomUUID(), null, null, UUID.randomUUID().toString(), UUID.randomUUID().toString(), UUID.randomUUID().toString()); tenantDao.create(new TenantModelDao(tenant), internalCallContext); tenantDao .addTenantKeyValue("MY_KEY", "TheValue1", false, internalCallContext); tenantDao .addTenantKeyValue("MY_KEY", "TheValue2", false, internalCallContext); tenantDao .addTenantKeyValue("MY_KEY", "TheValue3", false, internalCallContext); final List<String> value = tenantDao.getTenantValueForKey("MY_KEY", internalCallContext); Assert.assertEquals(value.size(), 3); tenantDao.updateTenantLastKeyValue("MY_KEY", "NewValue3", internalCallContext); final List<String> newValues = tenantDao.getTenantValueForKey("MY_KEY", internalCallContext); Assert.assertEquals(newValues.size(), 3); Assert.assertEquals(newValues.get(0), "TheValue1"); Assert.assertEquals(newValues.get(1), "TheValue2"); Assert.assertEquals(newValues.get(2), "NewValue3"); }
private void setupTenant() throws TenantApiException { final UUID uuid = UUID.randomUUID(); final String externalKey = uuid.toString(); final String apiKey = externalKey + "-Key"; final String apiSecret = externalKey + "-$3cr3t"; // Only place where we use callContext tenant = tenantUserApi.createTenant(new DefaultTenant(uuid, init, init, externalKey, apiKey, apiSecret), callContext); testCallContext = new DefaultCallContext(null, tenant.getId(), "tester", CallOrigin.EXTERNAL, UserType.TEST, "good reason", "trust me", uuid, clock); }
@Test(groups = "slow") public void testTenantKeyValue() throws Exception { final DefaultTenant tenant = new DefaultTenant(UUID.randomUUID(), null, null, UUID.randomUUID().toString(), UUID.randomUUID().toString(), UUID.randomUUID().toString()); tenantDao.create(new TenantModelDao(tenant), internalCallContext); tenantDao .addTenantKeyValue("THE_KEY", "TheValue", false, internalCallContext); List<String> value = tenantDao.getTenantValueForKey("THE_KEY", internalCallContext); Assert.assertEquals(value.size(), 1); Assert.assertEquals(value.get(0), "TheValue"); tenantDao.addTenantKeyValue("THE_KEY", "TheSecondValue", false, internalCallContext); value = tenantDao.getTenantValueForKey("THE_KEY", internalCallContext); Assert.assertEquals(value.size(), 2); value = tenantDao.getTenantValueForKey("THE_KEY", internalCallContext); Assert.assertEquals(value.size(), 2); tenantDao.deleteTenantKey("THE_KEY", internalCallContext); value = tenantDao.getTenantValueForKey("THE_KEY", internalCallContext); Assert.assertEquals(value.size(), 0); }
@Test(groups = "slow") public void testTenantSearch() throws Exception { final DefaultTenant tenant = new DefaultTenant(UUID.randomUUID(), null, null, UUID.randomUUID().toString(), UUID.randomUUID().toString(), UUID.randomUUID().toString()); tenantDao.create(new TenantModelDao(tenant), internalCallContext); tenantDao .addTenantKeyValue("foobar", "foobar1", false, internalCallContext); tenantDao .addTenantKeyValue("foobar", "foobar2", false, internalCallContext); tenantDao .addTenantKeyValue("foobar", "foobar3", false, internalCallContext); tenantDao.updateTenantLastKeyValue("foo", "foo1", internalCallContext); tenantDao.updateTenantLastKeyValue("fooXX", "fooXX1", internalCallContext); tenantDao.updateTenantLastKeyValue("bar", "bar", internalCallContext); final List<TenantKVModelDao> result = tenantDao.searchTenantKeyValues("foo", internalCallContext); Assert.assertEquals(result.size(), 5); }
@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()); } } }
@Test(groups = "fast") public void testExternalizable() throws IOException { final DefaultTenant tenantdata = new DefaultTenant(UUID.randomUUID(), clock.getUTCNow(), clock.getUTCNow(), "er44TT-yy4r", "TTR445ee2", null); final Codec code = new SerializationCodec(); final ByteBuf byteBuf = code.getValueEncoder().encode(tenantdata); final DefaultTenant tenantData2 = (DefaultTenant) code.getValueDecoder().decode(byteBuf, null); Assert.assertEquals(tenantData2, tenantdata); } }
@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; }