Skip to content

Commit

Permalink
OLMIS-7989: Refactor currency unit in ProgramOrderableImportPersister…
Browse files Browse the repository at this point in the history
… to make it testable
  • Loading branch information
pwargulak committed Oct 17, 2024
1 parent bd77a3f commit 09b0670
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,16 @@
import org.slf4j.profiler.Profiler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service("programOrderable.csv")
public class ProgramOrderableImportPersister
implements DataImportPersister<
ProgramOrderable, ProgramOrderableCsvModel, ProgramOrderableDto> {

private final CurrencyUnit currencyUnit;
@Value("${currencyCode}")
private String currencyCode;

@Autowired private FileHelper fileHelper;
@Autowired private ProgramOrderableRepository programOrderableRepository;
Expand All @@ -67,10 +69,6 @@ public class ProgramOrderableImportPersister
@Qualifier("importExecutorService")
private ExecutorService importExecutorService;

public ProgramOrderableImportPersister() {
currencyUnit = CurrencyUnit.of(System.getenv("CURRENCY_CODE"));
}

@Override
public List<ProgramOrderableDto> processAndPersist(InputStream dataStream, Profiler profiler)
throws InterruptedException {
Expand Down Expand Up @@ -118,7 +116,8 @@ private List<ProgramOrderable> createOrUpdate(List<ProgramOrderableCsvModel> dto
dto.getDisplayOrder(),
dto.getDosesPerPatient(),
dto.getPricePerPack() != null
? Money.of(currencyUnit, Double.parseDouble(dto.getPricePerPack()))
? Money.of(
CurrencyUnit.of(currencyCode), Double.parseDouble(dto.getPricePerPack()))
: null,
null);

Expand All @@ -131,7 +130,8 @@ private List<ProgramOrderable> createOrUpdate(List<ProgramOrderableCsvModel> dto

if (programOrderable == null) {
programOrderable =
ProgramOrderable.createNew(program, orderableDisplayCategory, orderable, currencyUnit);
ProgramOrderable.createNew(
program, orderableDisplayCategory, orderable, CurrencyUnit.of(currencyCode));
}

programOrderable.updateFrom(programOrderableDto);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@
import java.util.List;
import java.util.function.Supplier;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.EnvironmentVariables;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
Expand All @@ -55,7 +53,6 @@
@RunWith(MockitoJUnitRunner.class)
public class ProgramOrderableImportPersisterTest {

@Rule public EnvironmentVariables environmentVariables = new EnvironmentVariables();
private InputStream dataStream;

@Mock private FileHelper fileHelper;
Expand All @@ -69,7 +66,7 @@ public class ProgramOrderableImportPersisterTest {

@Before
public void setUp() {
environmentVariables.set("CURRENCY_CODE", "USD");
ReflectionTestUtils.setField(programOrderableImportPersister, "currencyCode", "USD");

ReflectionTestUtils.setField(
programOrderableImportPersister,
Expand Down

0 comments on commit 09b0670

Please sign in to comment.