Thanks @dkayiwa. Yes I had seen that, however I wasn’t sure what to do with the Module instance. But all of a sudden I realise that I could bring in a mock/test one just for the sake of going through this method.
Would be good to have all that baked into BaseModuleContextSensitiveTest actually.
package org.openmrs.module;
import java.util.stream.Stream;
public class TestDaemonToken {
private class Activator extends BaseModuleActivator implements DaemonTokenAware {
private DaemonToken token;
@Override
public void setDaemonToken(DaemonToken token) {
this.token = token;
}
public DaemonToken getDaemonToken() {
return token;
}
}
/**
* Sets a valid Daemon token to a collection of {@link DaemonTokenAware} instances so that they
* can run Daemon threads in tests.
*
* @param awares The collection of {@link DaemonTokenAware} implementations.
*/
public void setDaemonToken(DaemonTokenAware... awares) {
Module module = new Module("Spring Test Module");
module.setModuleId("spring-test-module");
final Activator activator = new Activator();
module.setModuleActivator(activator);
ModuleFactory.passDaemonToken(module);
Stream.of(awares).forEach(a -> a.setDaemonToken(activator.getDaemonToken()));
}
}
Right now it will enter the code base of OAuth 2 Login, but I’ll create a ticket for having this moved to Core and backported.
hello @mksd , @dkayiwa whats the proper way to Pass a daemon token to the given module , like where do i do it from .
I want to Execute the given runnable in a new thread that is authenticated as the daemon user but am still failing to pass a valid DaemonToken here .
I had innitaialy run this in a hacky way ,Opened and Closed ContextSession, and added ProxyPrivileges ,
but i want to instead excute it in a deamon thread .
trying something like this
Daemon.runInDaemonThread(new Runnable() {
@Override
public void run() {
measureService.saveMeasurementEventData(measurementEventData);
}
}, new DaemonToken("radiology"));
HI @mksd ,
i only have issues setting Daemon token in the tests .
Not sure how you were able to autowire DaemonTokenAware here , its faling on my end and i cant seem to find where that was defined as a bean . am depending on OpenMRS version 2.0.0 . did you do any custom Spring Config some where else ??
@mozzy there is a different between units tests and deployed environments, daemon token is only passed via the module activator by implementing DaemonTokenAware which is not called for unit tests. For tests, I would say just mock things.