Skip to content

Commit

Permalink
[CBS-24101]-Updated itemDetails json handling. No override even in fa…
Browse files Browse the repository at this point in the history
…ilure
  • Loading branch information
singh-sukhvir committed Aug 16, 2024
1 parent 60b1b9d commit de35ddb
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ public void start(final BundleContext context) throws Exception {
final VertexTaxCalculator vertexTaxCalculator = new VertexTaxCalculator(vertexApiConfigurationHandler,
dao,
clock.getClock(),
killbillAPI,
new ObjectMapper());
killbillAPI);
final VertexInvoicePluginApi pluginApi = new VertexInvoicePluginApi(vertexApiConfigurationHandler,
killbillAPI,
configProperties,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,15 @@ public class VertexTaxCalculator extends PluginTaxCalculator {
private final VertexApiConfigurationHandler vertexApiConfigurationHandler;
private final VertexDao dao;
private final Clock clock;
private final ObjectMapper objectMapper;

public VertexTaxCalculator(final VertexApiConfigurationHandler vertexApiConfigurationHandler,
final VertexDao dao,
final Clock clock,
final OSGIKillbillAPI osgiKillbillAPI,
final ObjectMapper objectMapper) {
final OSGIKillbillAPI osgiKillbillAPI) {
super(osgiKillbillAPI);
this.vertexApiConfigurationHandler = vertexApiConfigurationHandler;
this.clock = clock;
this.dao = dao;
this.objectMapper = objectMapper;
}

public List<InvoiceItem> compute(final Account account,
Expand Down Expand Up @@ -362,7 +359,21 @@ private InvoiceItem createTaxInvoiceItem(final InvoiceItem taxableItem, final UU
}

private String addTaxRateToItemDetails(@Nullable final String itemDetails, @Nonnull final Double taxRate) {
final ObjectNode existingItemsDetailsJson = deserializeItemDetails(itemDetails);
ObjectNode existingItemsDetailsJson = null;
final ObjectMapper objectMapper = new ObjectMapper();

if (itemDetails != null && !itemDetails.isEmpty()) {
try {
final JsonNode jsonNode = objectMapper.readTree(itemDetails);
if (!jsonNode.isObject()) {
return itemDetails;
}
existingItemsDetailsJson = (ObjectNode) jsonNode;
} catch (JsonProcessingException e) {
logger.error("Couldn't deserialize the item details: {}", itemDetails, e);
return itemDetails;
}
}

final Object itemDetailsWithTaxRate;
if (existingItemsDetailsJson != null) {
Expand All @@ -380,23 +391,6 @@ private String addTaxRateToItemDetails(@Nullable final String itemDetails, @Nonn
}
}

private ObjectNode deserializeItemDetails(@Nullable final String itemDetails) {
if (itemDetails == null || itemDetails.isEmpty()) {
return null;
}

try {
final JsonNode jsonNode = objectMapper.readTree(itemDetails);
if (!jsonNode.isObject()) {
return null;
}
return (ObjectNode) jsonNode;
} catch (JsonProcessingException e) {
logger.error("Couldn't deserialize the item details: {}", itemDetails, e);
return null;
}
}

private String getTaxDescription(final TaxesType transactionLineDetailModel) {
final Jurisdiction jurisdiction = transactionLineDetailModel.getJurisdiction();
return jurisdiction != null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,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, new ObjectMapper());
vertexTaxCalculator = new VertexTaxCalculator(vertexApiConfigurationHandler, dao, clock, osgiKillbillAPI);
vertexInvoicePluginApi = new VertexInvoicePluginApi(vertexApiConfigurationHandler,
osgiKillbillAPI,
new OSGIConfigPropertiesService(Mockito.mock(BundleContext.class)),
Expand Down Expand Up @@ -179,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, new ObjectMapper());
vertexTaxCalculator = new VertexTaxCalculator(vertexApiConfigurationHandler, dao, clock, osgiKillbillAPI);
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 @@ -71,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, new ObjectMapper());
calculator = new VertexTaxCalculator(vertexApiConfigurationHandler, dao, clock, osgiKillbillAPI);

pluginProperties.add(new PluginProperty(VertexTaxCalculator.SELLER_DIVISION, "328", false));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import jdk.nashorn.internal.ir.ObjectNode;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyMap;
Expand Down Expand Up @@ -99,8 +100,6 @@ public class VertexTaxCalculatorTest {
private InvoiceItem adjustment;
@Mock
private TaxesType taxesType;
@Spy
private ObjectMapper objectMapper;

@InjectMocks
private VertexTaxCalculator vertexTaxCalculator;
Expand Down Expand Up @@ -276,6 +275,7 @@ public void testTaxItemDetailsWhenUsageDetailsExists() throws Exception {

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

Expand All @@ -284,18 +284,18 @@ public void testTaxItemDetailsWhenUsageDetailsExists() throws Exception {
}

@Test(groups = "fast")
public void testTaxItemDetailsWhenJsonProcessingException() throws Exception {
public void testTaxItemDetailsWhenExistingItemDetailsIsNotJson() throws Exception {
//given
final Double taxRate = 0.09975d;
given(taxesType.getEffectiveRate()).willReturn(taxRate);
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")
Expand Down

0 comments on commit de35ddb

Please sign in to comment.