Search in sources :

Example 1 with OverdueConfig

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);
}
Also used : OverdueConfig(org.killbill.billing.overdue.api.OverdueConfig) Test(org.testng.annotations.Test)

Example 2 with OverdueConfig

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);
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext) URI(java.net.URI) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) InvocationOnMock(org.mockito.invocation.InvocationOnMock) OverdueConfig(org.killbill.billing.overdue.api.OverdueConfig) Test(org.testng.annotations.Test)

Example 3 with OverdueConfig

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());
}
Also used : OverdueConfig(org.killbill.billing.overdue.api.OverdueConfig) Test(org.testng.annotations.Test)

Example 4 with OverdueConfig

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();
}
Also used : OverdueJson(org.killbill.billing.jaxrs.json.OverdueJson) TenantContext(org.killbill.billing.util.callcontext.TenantContext) OverdueConfig(org.killbill.billing.overdue.api.OverdueConfig) DefaultOverdueConfig(org.killbill.billing.overdue.config.DefaultOverdueConfig) TimedResource(org.killbill.commons.metrics.TimedResource) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 5 with OverdueConfig

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);
}
Also used : OverdueConfig(org.killbill.billing.overdue.api.OverdueConfig) DefaultOverdueConfig(org.killbill.billing.overdue.config.DefaultOverdueConfig) CallContext(org.killbill.billing.util.callcontext.CallContext) TimedResource(org.killbill.commons.metrics.TimedResource) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

OverdueConfig (org.killbill.billing.overdue.api.OverdueConfig)5 Test (org.testng.annotations.Test)3 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 Produces (javax.ws.rs.Produces)2 DefaultOverdueConfig (org.killbill.billing.overdue.config.DefaultOverdueConfig)2 TimedResource (org.killbill.commons.metrics.TimedResource)2 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 URI (java.net.URI)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Consumes (javax.ws.rs.Consumes)1 GET (javax.ws.rs.GET)1 POST (javax.ws.rs.POST)1 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)1 InternalTenantContext (org.killbill.billing.callcontext.InternalTenantContext)1 OverdueJson (org.killbill.billing.jaxrs.json.OverdueJson)1 CallContext (org.killbill.billing.util.callcontext.CallContext)1 TenantContext (org.killbill.billing.util.callcontext.TenantContext)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1