use of org.killbill.billing.overdue.api.OverdueConfig in project killbill by killbill.
the class TestEhCacheOverdueConfigCache method testDefaultOverdueConfig.
//
// Verify the default OverdueConfig is returned when system property has been set (and OverdueConfigCache has been initialized)
//
@Test(groups = "fast")
public void testDefaultOverdueConfig() throws OverdueApiException {
overdueConfigCache.loadDefaultOverdueConfig(Resources.getResource("OverdueConfig.xml").toExternalForm());
final OverdueConfig result = overdueConfigCache.getOverdueConfig(internalCallContext);
Assert.assertNotNull(result);
Assert.assertEquals(result.getOverdueStatesAccount().getStates().length, 1);
Assert.assertTrue(result.getOverdueStatesAccount().getStates()[0].isClearState());
// Verify the lookup with other contexts
Assert.assertEquals(overdueConfigCache.getOverdueConfig(multiTenantContext), result);
Assert.assertEquals(overdueConfigCache.getOverdueConfig(otherMultiTenantContext), result);
Assert.assertEquals(overdueConfigCache.getOverdueConfig(Mockito.mock(InternalTenantContext.class)), result);
Assert.assertEquals(overdueConfigCache.getOverdueConfig(Mockito.mock(InternalCallContext.class)), result);
}
use of org.killbill.billing.overdue.api.OverdueConfig in project killbill by killbill.
the class TestEhCacheOverdueConfigCache method testExistingTenantOverdue.
//
// Verify OverdueConfigCache returns per tenant overdue config:
// 1. We first mock TenantInternalApi to return a different overdue config than the default one
// 2. We then mock TenantInternalApi to throw RuntimeException which means overdue config was cached and there was no additional call
// to the TenantInternalApi api (otherwise test would fail with RuntimeException)
//
@Test(groups = "fast")
public void testExistingTenantOverdue() throws OverdueApiException, URISyntaxException, IOException {
final InternalCallContext differentMultiTenantContext = Mockito.mock(InternalCallContext.class);
Mockito.when(differentMultiTenantContext.getTenantRecordId()).thenReturn(55667788L);
final AtomicBoolean shouldThrow = new AtomicBoolean(false);
final Long multiTenantRecordId = multiTenantContext.getTenantRecordId();
final Long otherMultiTenantRecordId = otherMultiTenantContext.getTenantRecordId();
final InputStream tenantInputOverdueConfig = UriAccessor.accessUri(new URI(Resources.getResource("OverdueConfig2.xml").toExternalForm()));
final String tenantOverdueConfigXML = CharStreams.toString(new InputStreamReader(tenantInputOverdueConfig, "UTF-8"));
final InputStream otherTenantInputOverdueConfig = UriAccessor.accessUri(new URI(Resources.getResource("OverdueConfig.xml").toExternalForm()));
final String otherTenantOverdueConfigXML = CharStreams.toString(new InputStreamReader(otherTenantInputOverdueConfig, "UTF-8"));
Mockito.when(tenantInternalApi.getTenantOverdueConfig(Mockito.any(InternalTenantContext.class))).thenAnswer(new Answer<String>() {
@Override
public String answer(final InvocationOnMock invocation) throws Throwable {
if (shouldThrow.get()) {
throw new RuntimeException();
}
final InternalTenantContext internalContext = (InternalTenantContext) invocation.getArguments()[0];
if (multiTenantRecordId.equals(internalContext.getTenantRecordId())) {
return tenantOverdueConfigXML;
} else if (otherMultiTenantRecordId.equals(internalContext.getTenantRecordId())) {
return otherTenantOverdueConfigXML;
} else {
return null;
}
}
});
// Verify the lookup for a non-cached tenant. No system config is set yet but EhCacheOverdueConfigCache returns a default no-op one
OverdueConfig differentResult = overdueConfigCache.getOverdueConfig(differentMultiTenantContext);
Assert.assertNotNull(differentResult);
Assert.assertEquals(differentResult.getOverdueStatesAccount().getStates().length, 1);
Assert.assertTrue(differentResult.getOverdueStatesAccount().getStates()[0].isClearState());
// Make sure the cache loader isn't invoked, see https://github.com/killbill/killbill/issues/298
shouldThrow.set(true);
differentResult = overdueConfigCache.getOverdueConfig(differentMultiTenantContext);
Assert.assertNotNull(differentResult);
Assert.assertEquals(differentResult.getOverdueStatesAccount().getStates().length, 1);
Assert.assertTrue(differentResult.getOverdueStatesAccount().getStates()[0].isClearState());
shouldThrow.set(false);
// Set a default config
overdueConfigCache.loadDefaultOverdueConfig(Resources.getResource("OverdueConfig.xml").toExternalForm());
// Verify the lookup for this tenant
final OverdueConfig result = overdueConfigCache.getOverdueConfig(multiTenantContext);
Assert.assertNotNull(result);
Assert.assertEquals(result.getOverdueStatesAccount().getStates().length, 1);
Assert.assertFalse(result.getOverdueStatesAccount().getStates()[0].isClearState());
// Verify the lookup for another tenant
final OverdueConfig otherResult = overdueConfigCache.getOverdueConfig(otherMultiTenantContext);
Assert.assertNotNull(otherResult);
Assert.assertEquals(otherResult.getOverdueStatesAccount().getStates().length, 1);
Assert.assertTrue(otherResult.getOverdueStatesAccount().getStates()[0].isClearState());
shouldThrow.set(true);
// Verify the lookup for this tenant
final OverdueConfig result2 = overdueConfigCache.getOverdueConfig(multiTenantContext);
Assert.assertEquals(result2, result);
// Verify the lookup with another context for the same tenant
final InternalCallContext sameMultiTenantContext = Mockito.mock(InternalCallContext.class);
Mockito.when(sameMultiTenantContext.getAccountRecordId()).thenReturn(9102L);
Mockito.when(sameMultiTenantContext.getTenantRecordId()).thenReturn(multiTenantRecordId);
Assert.assertEquals(overdueConfigCache.getOverdueConfig(sameMultiTenantContext), result);
// Verify the lookup with the other tenant
Assert.assertEquals(overdueConfigCache.getOverdueConfig(otherMultiTenantContext), otherResult);
}
use of org.killbill.billing.overdue.api.OverdueConfig in project killbill by killbill.
the class TestEhCacheOverdueConfigCache method testMissingDefaultOverdueConfig.
//
// Verify the default OverdueConfig is returned when used in mono-tenant and overdue system property has not been set
//
@Test(groups = "fast")
public void testMissingDefaultOverdueConfig() throws OverdueApiException {
overdueConfigCache.loadDefaultOverdueConfig((String) null);
final OverdueConfig result = overdueConfigCache.getOverdueConfig(internalCallContext);
Assert.assertNotNull(result);
Assert.assertEquals(result.getOverdueStatesAccount().getStates().length, 1);
Assert.assertTrue(result.getOverdueStatesAccount().getStates()[0].isClearState());
}
use of org.killbill.billing.overdue.api.OverdueConfig in project killbill by killbill.
the class OverdueResource method getOverdueConfigJson.
@TimedResource
@GET
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Retrieve the overdue config as JSON", response = OverdueJson.class)
@ApiResponses(value = {})
public Response getOverdueConfigJson(@javax.ws.rs.core.Context final HttpServletRequest request) throws Exception {
final TenantContext tenantContext = context.createContext(request);
final OverdueConfig overdueConfig = overdueApi.getOverdueConfig(tenantContext);
final OverdueJson result = new OverdueJson(overdueConfig);
return Response.status(Status.OK).entity(result).build();
}
use of org.killbill.billing.overdue.api.OverdueConfig in project killbill by killbill.
the class OverdueResource method uploadOverdueConfigJson.
@TimedResource
@POST
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Upload the full overdue config as JSON")
@ApiResponses(value = {})
public Response uploadOverdueConfigJson(final OverdueJson overdueJson, @HeaderParam(HDR_CREATED_BY) final String createdBy, @HeaderParam(HDR_REASON) final String reason, @HeaderParam(HDR_COMMENT) final String comment, @javax.ws.rs.core.Context final HttpServletRequest request, @javax.ws.rs.core.Context final UriInfo uriInfo) throws Exception {
final CallContext callContext = context.createContext(createdBy, reason, comment, request);
final OverdueConfig overdueConfig = OverdueJson.toOverdueConfigWithValidation(overdueJson);
overdueApi.uploadOverdueConfig(overdueConfig, callContext);
return uriBuilder.buildResponse(uriInfo, OverdueResource.class, null, null, request);
}
Aggregations