Creating directory in Openmrs dir of Appdata

Hi all,

I don’t know whether I’m being too lazy to fix this but just ‘parent’ me here. What I’m intending to do is to create an ‘mpd’ directory of path “C:\Users\Samuel34\Application Data\OpenMRS\mpd” but depending on OpenmrsUtil.getDirectoryInApplicationDataDirectory(“Directory_NAME”)

However, this acts wickedly. Like for instance in my implementation in the mergepatientdata module, it creates a random/dynamic directory in the “AppData\Local\Temp” directory.

@Test
public void dummyTest() {
	File configFileFolder = OpenmrsUtil.getDirectoryInApplicationDataDirectory("mpd");
	System.out.println(configFileFolder.getAbsolutePath());
}

When you run the above snippet in the mergepatient module, here is the output “C:\Users\Samuel34\AppData\Local\Temp\appdir-for-unit-tests-192667210576031389\mpd”

But when I tested the same Test case in the sync2 module, I got the expected output “C:\Users\Samuel34\Application Data\OpenMRS\mpd”

What could I be missing?

cc: @mseaton , @dkayiwa , @ssmusoke , @wyclif , @mksd etc…

@samuel34 That is the best way to approach since you are leveraging locations that already work within the core functions.

But I want the directory to be created “C:\Users\Samuel34\Application Data\OpenMRS\mpd” not “C:\Users\Samuel34\AppData\Local\Temp\appdir-for-unit-tests-192667210576031389\mpd”. Could you give my question a second glance? @ssmusoke

@samuel34 Why does the directory location matter?

I want to store customized “mpd.json” configuration in a known stable unchanging location just like sync2 does. I’m intending to store it in some dir like “C:\Users\Samuel34\Application Data\OpenMRS\mpd\mpd.json”.

The major challenge I’m facing is for my implementation, the dir is created like randomly, so it keeps changing. Thats not what we want @ssmusoke

@dkayiwa any advice?

@samuel34 AFAIK this is by design. This is done in order for unit tests to have a clean, independent application data directory to use. If you want to read a particular file from the application data directory, and to test this behavior, you should add some code into your unit test to first write the relevant file(s) to the “mpd/” folder before testing the code that reads these in. Or you could mock the behavior (see what @darius did here)

You’ll see when you actually run your module, that things work as intended with the application data directory you are expecting in C:\Users\Samuel34\Application Data\OpenMRS\mpd.

I’m not entirely sure why the sync2 module would behave differently than this - can you point me to the code that is in that repository that you say works differently?

Mike

Hi @mseaton,

Here is the Util method that works differently. I creates the directory in C:\Users\Samuel34\AppData\Local\Temp\appdir-for-unit-tests-232261003667222724 instead of C:\Users\Samuel34\Application Data\OpenMRS

@dkayiwa any advice here!

Did you try @mseaton’s advice?

@dkayiwa @mseaton’s advice looks lke a solution in Test cases. But my use case isn’t writing Test cases.

This is the specimen troubling me :slight_smile:

Can you point me to the sync2 test which you say works correctly?

@dkayiwa Actually, just to explain my blocker in a wider context,

I made this method expecting it to make a dir something like C:\Users\Samuel34\Application Data\OpenMRS\mpd but it acted otherwise. I went ahead and looked at sync2 codebase, I found out that in sync2, things actually work fine here. So in my mind I thought that maybe my implementation could be wrong,

Just for clarity, I made the above test case and tested it with both modules, but stubbornly it produced such “C:\Users\Samuel34\AppData\Local\Temp\appdir-for-unit-tests-192667210576031389\mpd” for my module and “C:\Users\Samuel34\Application Data\OpenMRS\mpd” for sync2.

The one you are pointing to in sync is not a unit test.

Yaaa its not a unit test. I made one myself manually in sync2. Also mine isn’t but its failing.

When are you making the call to MergePatientDataConfigurationUtils.getCustomConfigFilePath()?

As in what class and method?

Do not do it from the constructor because openmrs may not have started to initialize the application data folder.

@dkayiwa As a workaround, I removed it from the constructor, then called it this method which is invoked in the constructor, made a test case

https://github.com/samuelmale/openmrs-module-mergepatientdata/blob/master/api/src/test/java/org/openmrs/module/mergepatientdata/api/MergePatientDataConfigurationServiceTest.java#L17-L20

But I’m still getting the same output. C:\Users\Samuel34\AppData\Local\Temp\appdir-for-unit-tests-5225115059870028729\mpd

Like i said before, do not call it from a constructor.