# Set a Daemon token in modules context-sensitive tests

**URL:** <https://talk.openmrs.org/t/set-a-daemon-token-in-modules-context-sensitive-tests/32022>\
**Category:** Development\
**Tags:** module, spring, unit-tests\
**Created:** [February 4, 2021, 10:00pm UTC](https://talk.openmrs.org/t/set-a-daemon-token-in-modules-context-sensitive-tests/32022 "2021-02-04T22:00:10Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![mksd](https://talk.openmrs.org/user_avatar/talk.openmrs.org/mksd/32/11729_2.png) [@mksd](https://talk.openmrs.org/u/mksd)\
**Post date:** [February 4, 2021, 10:00pm UTC](https://talk.openmrs.org/t/set-a-daemon-token-in-modules-context-sensitive-tests/32022/1 "2021-02-04T22:00:11Z")

</div>

@dkayiwa @ibacher @mseaton @wyclif, how do you guys setup a Daemon token for context-sensitive tests?

I had to resort to reflection:

```java
@Before
public void setup() throws Exception {
    Field field = ModuleFactory.class.getDeclaredField("daemonTokens");
    field.setAccessible(true);
    Map<String, DaemonToken> daemonTokens = (Map<String, DaemonToken>) field.get(ModuleFactory.class);
    daemonTokens.put("test", new DaemonToken("test"));
    field.setAccessible(false);
}

```

What’s the proper/recommended way?

---

<div class="post-metadata">

**Author:** ![dkayiwa](https://talk.openmrs.org/user_avatar/talk.openmrs.org/dkayiwa/32/179_2.png) [@dkayiwa](https://talk.openmrs.org/u/dkayiwa)\
**Post date:** [February 5, 2021, 8:11am UTC](https://talk.openmrs.org/t/set-a-daemon-token-in-modules-context-sensitive-tests/32022/2 "2021-02-05T08:11:19Z")

</div>

If your test is in the `org.openmrs.module` package, you can use `ModuleFactory.passDaemonToken(module)`

---

<div class="post-metadata">

**Author:** ![mksd](https://talk.openmrs.org/user_avatar/talk.openmrs.org/mksd/32/11729_2.png) [@mksd](https://talk.openmrs.org/u/mksd)\
**Post date:** [February 5, 2021, 9:44am UTC](https://talk.openmrs.org/t/set-a-daemon-token-in-modules-context-sensitive-tests/32022/3 "2021-02-05T09:44:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dkayiwa](https://talk.openmrs.org/user_avatar/talk.openmrs.org/dkayiwa/32/179_2.png) [@dkayiwa](https://talk.openmrs.org/u/dkayiwa)\
**Post date:** [February 5, 2021, 10:32am UTC](https://talk.openmrs.org/t/set-a-daemon-token-in-modules-context-sensitive-tests/32022/4 "2021-02-05T10:32:37Z")

</div>

> [@mksd](#):
>
> 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.

All you need is set the moduleId on a module instance and you are good.

> [@mksd](#):
>
> Would be good to have all that baked into `BaseModuleContextSensitiveTest` actually.

I agree!

---

<div class="post-metadata">

**Author:** ![mksd](https://talk.openmrs.org/user_avatar/talk.openmrs.org/mksd/32/11729_2.png) [@mksd](https://talk.openmrs.org/u/mksd)\
**Post date:** [February 5, 2021, 3:08pm UTC](https://talk.openmrs.org/t/set-a-daemon-token-in-modules-context-sensitive-tests/32022/5 "2021-02-05T15:08:10Z")

</div>

This worked like a charm:

```java
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.

---

<div class="post-metadata">

**Author:** ![dkayiwa](https://talk.openmrs.org/user_avatar/talk.openmrs.org/dkayiwa/32/179_2.png) [@dkayiwa](https://talk.openmrs.org/u/dkayiwa)\
**Post date:** [February 5, 2021, 3:28pm UTC](https://talk.openmrs.org/t/set-a-daemon-token-in-modules-context-sensitive-tests/32022/6 "2021-02-05T15:28:10Z")

</div>

Thanks for sharing the solution! 🙂

---

<div class="post-metadata">

**Author:** ![mksd](https://talk.openmrs.org/user_avatar/talk.openmrs.org/mksd/32/11729_2.png) [@mksd](https://talk.openmrs.org/u/mksd)\
**Post date:** [February 5, 2021, 5:39pm UTC](https://talk.openmrs.org/t/set-a-daemon-token-in-modules-context-sensitive-tests/32022/7 "2021-02-05T17:39:41Z")

</div>

👍

For the record

- `new TestDaemonToken().setDaemonToken(aware)` is used [here](https://github.com/openmrs/openmrs-module-oauth2login/blob/b9a5d53248165a910bf2424aebff10ce55b03731/omod/src/test/java/org/openmrs/module/oauth2login/web/controller/OAuth2IntegrationTest.java#L115).
- `TestDaemonToken` is declared [here](https://github.com/openmrs/openmrs-module-oauth2login/blob/b9a5d53248165a910bf2424aebff10ce55b03731/omod/src/test/java/org/openmrs/module/TestDaemonToken.java#L5).

---

<div class="post-metadata">

**Author:** ![mozzy](https://talk.openmrs.org/user_avatar/talk.openmrs.org/mozzy/32/11815_2.png) [@mozzy](https://talk.openmrs.org/u/mozzy)\
**Post date:** [May 13, 2021, 11:18am UTC](https://talk.openmrs.org/t/set-a-daemon-token-in-modules-context-sensitive-tests/32022/8 "2021-05-13T11:18:26Z")

</div>

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](https://github.com/openmrs/openmrs-core/blob/master/api/src/main/java/org/openmrs/api/context/Daemon.java#L348) .

I had innitaialy run [this](https://gitlab.com/mozzy11/lh-radiology/-/blob/track/api/src/main/java/org/openmrs/module/radiology/viewer/event/MeasurementEventListener.java#L37-49) 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"));
```

---

<div class="post-metadata">

**Author:** ![mozzy](https://talk.openmrs.org/user_avatar/talk.openmrs.org/mozzy/32/11815_2.png) [@mozzy](https://talk.openmrs.org/u/mozzy)\
**Post date:** [May 13, 2021, 11:46am UTC](https://talk.openmrs.org/t/set-a-daemon-token-in-modules-context-sensitive-tests/32022/9 "2021-05-13T11:46:22Z")

</div>

Oh i have got it , i only had to have my activator implement **DaemonTokenAware**  
🙂

---

<div class="post-metadata">

**Author:** ![mozzy](https://talk.openmrs.org/user_avatar/talk.openmrs.org/mozzy/32/11815_2.png) [@mozzy](https://talk.openmrs.org/u/mozzy)\
**Post date:** [May 13, 2021, 12:54pm UTC](https://talk.openmrs.org/t/set-a-daemon-token-in-modules-context-sensitive-tests/32022/10 "2021-05-13T12:54:53Z")

</div>

HI @mksd , i only have issues setting Daemon token in the tests .

Not sure how you were able to autowire DaemonTokenAware [here](https://github.com/openmrs/openmrs-module-oauth2login/blob/b9a5d53248165a910bf2424aebff10ce55b03731/omod/src/test/java/org/openmrs/module/oauth2login/web/controller/OAuth2IntegrationTest.java#L95) , 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 ??

---

<div class="post-metadata">

**Author:** ![mksd](https://talk.openmrs.org/user_avatar/talk.openmrs.org/mksd/32/11729_2.png) [@mksd](https://talk.openmrs.org/u/mksd)\
**Post date:** [May 13, 2021, 1:19pm UTC](https://talk.openmrs.org/t/set-a-daemon-token-in-modules-context-sensitive-tests/32022/11 "2021-05-13T13:19:16Z")

</div>

Here I believe: [openmrs-module-oauth2login/UsernameAuthenticationScheme.java at 611fe5ff60365d912760dcf05a55188ba0c2529c · openmrs/openmrs-module-oauth2login · GitHub](https://github.com/openmrs/openmrs-module-oauth2login/blob/611fe5ff60365d912760dcf05a55188ba0c2529c/api/src/main/java/org/openmrs/module/oauth2login/authscheme/UsernameAuthenticationScheme.java#L33-L34)

`UsernameAuthenticationScheme` is the only `DaemonTokenAware` defined as a bean in the context of that test.

That will not work with an activator, unless you make it a bean just for Spring tests.

---

<div class="post-metadata">

**Author:** ![mozzy](https://talk.openmrs.org/user_avatar/talk.openmrs.org/mozzy/32/11815_2.png) [@mozzy](https://talk.openmrs.org/u/mozzy)\
**Post date:** [May 13, 2021, 1:30pm UTC](https://talk.openmrs.org/t/set-a-daemon-token-in-modules-context-sensitive-tests/32022/12 "2021-05-13T13:30:22Z")

</div>

> [@mksd](#):
>
> Here I believe: [openmrs-module-oauth2login/UsernameAuthenticationScheme.java at 611fe5ff60365d912760dcf05a55188ba0c2529c · openmrs/openmrs-module-oauth2login · GitHub](https://github.com/openmrs/openmrs-module-oauth2login/blob/611fe5ff60365d912760dcf05a55188ba0c2529c/api/src/main/java/org/openmrs/module/oauth2login/authscheme/UsernameAuthenticationScheme.java#L33-L34)

makes sense .

however i have just done a little refactoring

I defined static DaemonToken property from the class i want to acces the token from

```
public class MeasurementEventListener implements ApplicationListener<MeasurementEvent> {
    @Autowired
    MeasurementService measureService;
    
    private static DaemonToken daemonToken;
    public static void setDaemonToken(DaemonToken token) {
		daemonToken = token;
	}

    @Override
    public void onApplicationEvent(MeasurementEvent event) {
       

        Daemon.runInDaemonThread(new Runnable() {
            @Override
            public void run() {
                measureService.saveMeasurementEventData(measurementEventData);
            }
        }, daemonToken);
    }
}

```

i refactored the [TestDaemonToken class](https://github.com/openmrs/openmrs-module-oauth2login/blob/b9a5d53248165a910bf2424aebff10ce55b03731/omod/src/test/java/org/openmrs/module/TestDaemonToken.java#L5) and removed the  
` DaemonTokenAware... awares`  
parameter

```
package org.openmrs.module;

import org.openmrs.module.radiology.viewer.event.MeasurementEventListener;

    public class TestDaemonToken {
    	
    	private class Activator extends BaseModuleActivator implements DaemonTokenAware {
    	
    		@Override
    		public void setDaemonToken(DaemonToken token) {
    			MeasurementEventListener.setDaemonToken(token);
    		}
    		
    	}
    	
    	/**
    	 * setts a valid Daemon Token
    	 */
    	public void setDaemonToken() {
    		Module module = new Module("Spring Test Module");
    		module.setModuleId("Radiology");
    		final Activator activator = new Activator();
    		module.setModuleActivator(activator);
    		ModuleFactory.passDaemonToken(module);
    	}
    }

```

and i use it in the tests as

```
    @Before
	public void init() throws Exception {
		
		new TestDaemonToken().setDaemonToken();
	}

```

and it works fine

---

<div class="post-metadata">

**Author:** ![mksd](https://talk.openmrs.org/user_avatar/talk.openmrs.org/mksd/32/11729_2.png) [@mksd](https://talk.openmrs.org/u/mksd)\
**Post date:** [May 13, 2021, 2:27pm UTC](https://talk.openmrs.org/t/set-a-daemon-token-in-modules-context-sensitive-tests/32022/13 "2021-05-13T14:27:30Z")

</div>

Difficult to say what exact issue you may have encountered without sharing the entire code.

The class that I shared was meant to be entirely generic, hopefully entering Core’s test tooling.

---

<div class="post-metadata">

**Author:** ![wyclif](https://talk.openmrs.org/user_avatar/talk.openmrs.org/wyclif/32/19607_2.png) [@wyclif](https://talk.openmrs.org/u/wyclif)\
**Post date:** [May 13, 2021, 6:03pm UTC](https://talk.openmrs.org/t/set-a-daemon-token-in-modules-context-sensitive-tests/32022/14 "2021-05-13T18:03:21Z")

</div>

@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.
