Skip to content

Commit

Permalink
Do not nullify item details in case on JsonProcessingException, add t…
Browse files Browse the repository at this point in the history
…ests and improve logging
  • Loading branch information
vlaskhilkevich committed Aug 13, 2024
1 parent 2538c47 commit 60b1b9d
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import org.killbill.billing.plugin.vertex.health.VertexHealthcheckServlet;
import org.osgi.framework.BundleContext;

import com.fasterxml.jackson.databind.ObjectMapper;

public class VertexActivator extends KillbillActivatorBase {

public static final String PLUGIN_NAME = "killbill-vertex";
Expand All @@ -58,7 +60,8 @@ public void start(final BundleContext context) throws Exception {
final VertexTaxCalculator vertexTaxCalculator = new VertexTaxCalculator(vertexApiConfigurationHandler,
dao,
clock.getClock(),
killbillAPI);
killbillAPI,
new ObjectMapper());
final VertexInvoicePluginApi pluginApi = new VertexInvoicePluginApi(vertexApiConfigurationHandler,
killbillAPI,
configProperties,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,13 @@ public class VertexTaxCalculator extends PluginTaxCalculator {
public VertexTaxCalculator(final VertexApiConfigurationHandler vertexApiConfigurationHandler,
final VertexDao dao,
final Clock clock,
final OSGIKillbillAPI osgiKillbillAPI) {
final OSGIKillbillAPI osgiKillbillAPI,
final ObjectMapper objectMapper) {
super(osgiKillbillAPI);
this.vertexApiConfigurationHandler = vertexApiConfigurationHandler;
this.clock = clock;
this.dao = dao;
this.objectMapper = new ObjectMapper();
this.objectMapper = objectMapper;
}

public List<InvoiceItem> compute(final Account account,
Expand Down Expand Up @@ -374,8 +375,8 @@ private String addTaxRateToItemDetails(@Nullable final String itemDetails, @Nonn
try {
return objectMapper.writeValueAsString(itemDetailsWithTaxRate);
} catch (JsonProcessingException exception) {
logger.error("Couldn't serialize the tax item details: {}", itemDetailsWithTaxRate, exception);
return null;
logger.error("Couldn't serialize the tax item details {} with tax rate: {}", itemDetailsWithTaxRate, taxRate, exception);
return itemDetails;
}
}

Expand All @@ -391,7 +392,7 @@ private ObjectNode deserializeItemDetails(@Nullable final String itemDetails) {
}
return (ObjectNode) jsonNode;
} catch (JsonProcessingException e) {
logger.error("Couldn't deserialize the tax item details: {}", itemDetails, e);
logger.error("Couldn't deserialize the item details: {}", itemDetails, e);
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import com.fasterxml.jackson.databind.ObjectMapper;

// Note: the test assumes all California authorities are set up to collect sales and use tax (270+)
public class VertexInvoicePluginApiITest extends VertexRemoteTestBase {

Expand Down Expand Up @@ -88,7 +90,7 @@ public void testItemAdjustments() {
final Clock clock = new DefaultClock();
final VertexApiConfigurationHandler vertexApiConfigurationHandler = new VertexApiConfigurationHandler(VertexActivator.PLUGIN_NAME, osgiKillbillAPI);
vertexApiConfigurationHandler.setDefaultConfigurable(vertexApiClient);
vertexTaxCalculator = new VertexTaxCalculator(vertexApiConfigurationHandler, dao, clock, osgiKillbillAPI);
vertexTaxCalculator = new VertexTaxCalculator(vertexApiConfigurationHandler, dao, clock, osgiKillbillAPI, new ObjectMapper());
vertexInvoicePluginApi = new VertexInvoicePluginApi(vertexApiConfigurationHandler,
osgiKillbillAPI,
new OSGIConfigPropertiesService(Mockito.mock(BundleContext.class)),
Expand Down Expand Up @@ -177,7 +179,7 @@ public void testRepair() throws Exception {
final Clock clock = new DefaultClock();
final VertexApiConfigurationHandler vertexApiConfigurationHandler = new VertexApiConfigurationHandler(VertexActivator.PLUGIN_NAME, osgiKillbillAPI);
vertexApiConfigurationHandler.setDefaultConfigurable(vertexApiClient);
vertexTaxCalculator = new VertexTaxCalculator(vertexApiConfigurationHandler, dao, clock, osgiKillbillAPI);
vertexTaxCalculator = new VertexTaxCalculator(vertexApiConfigurationHandler, dao, clock, osgiKillbillAPI, new ObjectMapper());
vertexInvoicePluginApi = new VertexInvoicePluginApi(vertexApiConfigurationHandler,
osgiKillbillAPI,
new OSGIConfigPropertiesService(Mockito.mock(BundleContext.class)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableMap;

public class VertexTaxCalculatorITest extends VertexRemoteTestBase {
Expand Down Expand Up @@ -70,7 +71,7 @@ public void setUp() throws Exception {
final VertexApiConfigurationHandler vertexApiConfigurationHandler = new VertexApiConfigurationHandler(VertexActivator.PLUGIN_NAME, osgiKillbillAPI);
vertexApiConfigurationHandler.setDefaultConfigurable(vertexApiClient);

calculator = new VertexTaxCalculator(vertexApiConfigurationHandler, dao, clock, osgiKillbillAPI);
calculator = new VertexTaxCalculator(vertexApiConfigurationHandler, dao, clock, osgiKillbillAPI, new ObjectMapper());

pluginProperties.add(new PluginProperty(VertexTaxCalculator.SELLER_DIVISION, "328", false));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

Expand Down Expand Up @@ -97,6 +99,8 @@ public class VertexTaxCalculatorTest {
private InvoiceItem adjustment;
@Mock
private TaxesType taxesType;
@Spy
private ObjectMapper objectMapper;

@InjectMocks
private VertexTaxCalculator vertexTaxCalculator;
Expand Down Expand Up @@ -272,14 +276,28 @@ public void testTaxItemDetailsWhenUsageDetailsExists() throws Exception {

//when
List<InvoiceItem> result = vertexTaxCalculator.compute(account, invoice, true, Collections.emptyList(), tenantContext);
final ObjectMapper objectMapper = new ObjectMapper();
final JsonNode expectedNode = objectMapper.readTree(expectedItemDetailsWithTaxRate);
final JsonNode actualNode = objectMapper.readTree(result.get(0).getItemDetails());

//then
assertEquals(expectedNode, actualNode);
}

@Test(groups = "fast")
public void testTaxItemDetailsWhenJsonProcessingException() throws Exception {
//given
final String invoiceItemDetails = "someItemDetails";
given(taxableInvoiceItem.getItemDetails()).willReturn(invoiceItemDetails);
given(objectMapper.writeValueAsString(any())).willThrow(JsonProcessingException.class);

//when
List<InvoiceItem> result = vertexTaxCalculator.compute(account, invoice, true, Collections.emptyList(), tenantContext);

//then
assertEquals(invoiceItemDetails, result.get(0).getItemDetails());
Mockito.reset(objectMapper);
}

@Test(groups = "fast")
public void testComputeWhenTaxEffectiveRateIsNotPresentedByVertex() throws Exception {
//given
Expand Down

0 comments on commit 60b1b9d

Please sign in to comment.