Skip to content

Commit

Permalink
Merge pull request #752 from nueue23/datautils_junit
Browse files Browse the repository at this point in the history
update deprecated code and add junit
  • Loading branch information
shopizer-ecommerce authored Jun 16, 2022
2 parents 0004117 + 7c7236b commit acadb7c
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.salesmanager.core.business.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;

import com.salesmanager.core.constants.MeasureUnit;
import com.salesmanager.core.model.merchant.MerchantStore;
Expand Down Expand Up @@ -33,23 +34,21 @@ public static double getWeight(double weight, MerchantStore store,
double weightConstant = 2.2;
if (base.equals(MeasureUnit.LB.name())) {
if (store.getWeightunitcode().equals(MeasureUnit.LB.name())) {
return new BigDecimal(String.valueOf(weight)).setScale(2,
BigDecimal.ROUND_HALF_UP).doubleValue();
return new BigDecimal(String.valueOf(weight))
.setScale(2, RoundingMode.HALF_UP).doubleValue();
} else {// pound = kilogram
double answer = weight * weightConstant;
BigDecimal w = new BigDecimal(answer);
return w.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
return w.setScale(2, RoundingMode.HALF_UP).doubleValue();
}
} else {// need KG
if (store.getWeightunitcode().equals(MeasureUnit.KG.name())) {
return new BigDecimal(String.valueOf(weight)).setScale(2,
BigDecimal.ROUND_HALF_UP).doubleValue();
RoundingMode.HALF_UP).doubleValue();
} else {

double answer = weight / weightConstant;
BigDecimal w = new BigDecimal(answer);
return w.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

return w.setScale(2, RoundingMode.HALF_UP).doubleValue();
}
}
}
Expand All @@ -70,25 +69,25 @@ public static double getMeasure(double measure, MerchantStore store,
if (base.equals(MeasureUnit.IN.name())) {
if (store.getSeizeunitcode().equals(MeasureUnit.IN.name())) {
return new BigDecimal(String.valueOf(measure)).setScale(2,
BigDecimal.ROUND_HALF_UP).doubleValue();
RoundingMode.HALF_UP).doubleValue();
} else {// centimeter (inch to centimeter)
double measureConstant = 2.54;

double answer = measure * measureConstant;
BigDecimal w = new BigDecimal(answer);
return w.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
return w.setScale(2, RoundingMode.HALF_UP).doubleValue();

}
} else {// need CM
if (store.getSeizeunitcode().equals(MeasureUnit.CM.name())) {
return new BigDecimal(String.valueOf(measure)).setScale(2)
.doubleValue();
return new BigDecimal(String.valueOf(measure)).setScale(2,
RoundingMode.HALF_UP).doubleValue();
} else {// in (centimeter to inch)
double measureConstant = 0.39;

double answer = measure * measureConstant;
BigDecimal w = new BigDecimal(answer);
return w.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
return w.setScale(2, RoundingMode.HALF_UP).doubleValue();

}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package com.salesmanager.test.business.utils;

import com.salesmanager.core.business.utils.DataUtils;
import com.salesmanager.core.constants.MeasureUnit;
import com.salesmanager.core.model.merchant.MerchantStore;

import org.junit.Test;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

public class DataUtilsTest {

/**
* This methods tests {@link DataUtils#trimPostalCode(String)}
*/
@Test
public void testTrimPostalCode(){
String result = DataUtils.trimPostalCode(" 364856-A56 - B888@ ");
assertEquals("364856A56B888", result);
}

/**
* This methods tests {@link DataUtils#getWeight(double, MerchantStore, String)}
*/
@Test
public void testGetWeight_When_StoreUnit_LB_MeasurementUnit_LB(){
MerchantStore store = mock(MerchantStore.class);
when(store.getWeightunitcode()).thenReturn(MeasureUnit.LB.name());
double result = DataUtils.getWeight(100.789, store, MeasureUnit.LB.name());
assertEquals(100.79, result, 0);
}

/**
* This methods tests {@link DataUtils#getWeight(double, MerchantStore, String)}
*/
@Test
public void testGetWeight_When_StoreUnit_KG_MeasurementUnit_LB(){
MerchantStore store = mock(MerchantStore.class);
when(store.getWeightunitcode()).thenReturn(MeasureUnit.KG.name());
double result = DataUtils.getWeight(100.789, store, MeasureUnit.LB.name());
assertEquals(221.74, result, 0);
}

/**
* This methods tests {@link DataUtils#getWeight(double, MerchantStore, String)}
*/
@Test
public void testGetWeight_When_StoreUnit_KG_MeasurementUnit_KG(){
MerchantStore store = mock(MerchantStore.class);
when(store.getWeightunitcode()).thenReturn(MeasureUnit.KG.name());
double result = DataUtils.getWeight(100.789, store, MeasureUnit.KG.name());
assertEquals(100.79, result, 0);
}

/**
* This methods tests {@link DataUtils#getWeight(double, MerchantStore, String)}
*/
@Test
public void testGetWeight_When_StoreUnit_LB_MeasurementUnit_KG(){
MerchantStore store = mock(MerchantStore.class);
when(store.getWeightunitcode()).thenReturn(MeasureUnit.LB.name());
double result = DataUtils.getWeight(100.789, store, MeasureUnit.KG.name());
assertEquals(45.81, result, 0);
}

/**
* This methods tests {@link DataUtils#getMeasure(double, MerchantStore, String)}
*/
@Test
public void testGetMeasureWhen_StoreUnit_IN_MeasurementUnit_IN(){
MerchantStore store = mock(MerchantStore.class);
when(store.getSeizeunitcode()).thenReturn(MeasureUnit.IN.name());
double result = DataUtils.getMeasure(100.789, store, MeasureUnit.IN.name());
assertEquals(100.79, result, 0);
}

/**
* This methods tests {@link DataUtils#getMeasure(double, MerchantStore, String)}
*/
@Test
public void testGetMeasureWhen_StoreUnit_CM_MeasurementUnit_IN(){
MerchantStore store = mock(MerchantStore.class);
when(store.getSeizeunitcode()).thenReturn(MeasureUnit.CM.name());
double result = DataUtils.getMeasure(100.789, store, MeasureUnit.IN.name());
assertEquals(256.00, result, 0);
}

/**
* This methods tests {@link DataUtils#getMeasure(double, MerchantStore, String)}
*/
@Test
public void testGetMeasureWhen_StoreUnit_CM_MeasurementUnit_CM(){
MerchantStore store = mock(MerchantStore.class);
when(store.getSeizeunitcode()).thenReturn(MeasureUnit.CM.name());
double result = DataUtils.getMeasure(100.789, store, MeasureUnit.CM.name());
assertEquals(100.79, result, 0);
}

/**
* This methods tests {@link DataUtils#getMeasure(double, MerchantStore, String)}
*/
@Test
public void testGetMeasureWhen_StoreUnit_IN_MeasurementUnit_CM(){
MerchantStore store = mock(MerchantStore.class);
when(store.getSeizeunitcode()).thenReturn(MeasureUnit.IN.name());
double result = DataUtils.getMeasure(100.789, store, MeasureUnit.CM.name());
assertEquals(39.31, result, 0);
}
}

0 comments on commit acadb7c

Please sign in to comment.