2013-01-07 14:57:13 +01:00
package org.gcube.common.scope ;
2022-06-10 17:10:26 +02:00
import static org.junit.Assert.assertEquals ;
import static org.junit.Assert.assertNull ;
2013-01-07 14:57:13 +01:00
import java.util.concurrent.Callable ;
import java.util.concurrent.ExecutorService ;
import java.util.concurrent.Executors ;
2022-06-10 17:10:26 +02:00
import org.gcube.common.security.AuthorizedTasks ;
import org.gcube.common.security.providers.SecretManagerProvider ;
2013-01-07 14:57:13 +01:00
import org.junit.Test ;
public class BindingTest {
private ExecutorService executor = Executors . newSingleThreadExecutor ( ) ;
@Test
public void callablesAreBound ( ) throws Exception {
final String callScope = " somescope " ;
2024-02-21 20:29:54 +01:00
SecretManagerProvider . set ( new TestSecret ( callScope ) ) ;
2013-01-07 14:57:13 +01:00
Callable < Void > unbound = new Callable < Void > ( ) {
@Override
public Void call ( ) throws Exception {
2024-02-21 20:29:54 +01:00
assertEquals ( " task thread is not bound to call scope, but to " + SecretManagerProvider . get ( ) . getContext ( ) , callScope , SecretManagerProvider . get ( ) . getContext ( ) ) ;
2013-01-07 14:57:13 +01:00
return null ;
}
} ;
2022-06-10 17:10:26 +02:00
Callable < Void > bound = AuthorizedTasks . bind ( unbound ) ;
2013-01-07 14:57:13 +01:00
String newScope = " newscope " ;
//scope in current thread changes
2024-02-21 20:29:54 +01:00
SecretManagerProvider . set ( new TestSecret ( newScope ) ) ;
2013-01-07 14:57:13 +01:00
//task is submittted
executor . submit ( bound ) . get ( ) ;
//resetting task
2024-02-21 20:29:54 +01:00
assertEquals ( " call thread does not retain its latest scope " , newScope , SecretManagerProvider . get ( ) . getContext ( ) ) ;
2013-01-07 14:57:13 +01:00
//reset call scope
2024-02-21 20:29:54 +01:00
SecretManagerProvider . reset ( ) ;
2013-01-07 14:57:13 +01:00
Callable < Void > cleanupTest = new Callable < Void > ( ) {
@Override
public Void call ( ) throws Exception {
2024-02-21 20:29:54 +01:00
assertNull ( SecretManagerProvider . get ( ) ) ;
2013-01-07 14:57:13 +01:00
return null ;
}
} ;
executor . submit ( cleanupTest ) . get ( ) ;
}
}