11package ut .com .degustudios .executors ;
22
33import com .degustudios .executors .IdempotentExecutor ;
4+ import org .apache .commons .lang3 .StringUtils ;
45import org .apache .commons .lang3 .concurrent .ConcurrentException ;
56import org .junit .Test ;
67import org .junit .runner .RunWith ;
1011
1112import java .util .Arrays ;
1213import java .util .Collections ;
14+ import java .util .LinkedList ;
1315import java .util .List ;
1416import java .util .concurrent .ExecutionException ;
1517import java .util .concurrent .Future ;
2022import java .util .stream .Collectors ;
2123
2224import static org .awaitility .Awaitility .await ;
25+ import static org .hamcrest .CoreMatchers .hasItem ;
2326import static org .hamcrest .CoreMatchers .is ;
2427import static org .junit .Assert .assertThat ;
2528
2629@ RunWith (MockitoJUnitRunner .class )
2730public class IdempotentExecutorTest {
2831 private static final int TIMEOUT_IN_MS = 1000 ;
2932 private static final Logger logger = LoggerFactory .getLogger (IdempotentExecutorTest .class );
33+ private static final List <String > params = Arrays .asList ("--mockParameter" , "--otherParameter" );
34+ private static final String input = "INPUT PARAMETER" ;
3035
3136 @ Test
3237 public void executeReturnsFutureValueFromFunction () throws ExecutionException , InterruptedException {
@@ -35,9 +40,19 @@ public void executeReturnsFutureValueFromFunction() throws ExecutionException, I
3540 }
3641
3742 @ Test
38- public void executePassesParameterToFunction () throws ExecutionException , InterruptedException {
39- String returnValue = "TEST" ;
40- assertThat (tryExecute ((String x , List <String > y ) -> x , returnValue ).get (), is (returnValue ));
43+ public void executePassesInputParametersToKeyMapFunction () throws ExecutionException , InterruptedException {
44+ String expectedKey = input + StringUtils .join (params );
45+ List <String > mapFuncCalls = new LinkedList <>();
46+
47+ tryExecute (
48+ (String x , List <String > y ) -> x ,
49+ (String x , List <String > y ) -> {
50+ mapFuncCalls .add (x + StringUtils .join (y ));
51+ return "STUB" ;
52+ }).get ();
53+
54+ assertThat (mapFuncCalls .stream ().distinct ().count (), is (1L ));
55+ assertThat (mapFuncCalls , hasItem (expectedKey ));
4156 }
4257
4358 @ Test
@@ -94,31 +109,43 @@ public void willExecuteTheSameParametersAgainIfCacheFunctionReturnsFalse() {
94109 assertThat (invocationCounter .get (), is (2 ));
95110 }
96111
112+ private <T ,R > IdempotentExecutor <T ,R > getDefaultKeyCacheAllExecutor (
113+ BiFunction <T , List <String >, R > executeFunc ,
114+ BiFunction <T , List <String >, String > mapKeyFunc ) {
115+ return new IdempotentExecutor <>(executeFunc , mapKeyFunc , r -> true );
116+ }
117+
97118 private <T ,R > IdempotentExecutor <T ,R > getDefaultKeyCacheAllExecutor (BiFunction <T , List <String >, R > executeFunc ) {
98- return new IdempotentExecutor <>(executeFunc , Object :: toString , r -> true );
119+ return new IdempotentExecutor <>(executeFunc , IdempotentExecutorTest :: defaultKeyMapper , r -> true );
99120 }
100121
101122 private <T ,R > IdempotentExecutor <T ,R > getDefaultKeyCacheNoneExecutor (BiFunction <T , List <String >, R > executeFunc ) {
102- return new IdempotentExecutor <>(executeFunc , Object ::toString , r -> false );
123+ return new IdempotentExecutor <>(executeFunc , IdempotentExecutorTest ::defaultKeyMapper , r -> false );
124+ }
125+
126+ private static <T > String defaultKeyMapper (T input , List <String > params ) {
127+ return input .toString () + StringUtils .join (params );
103128 }
104129
105- private <T > Future <String > tryExecute (BiFunction <T ,List <String >,String > executeFunc , String x ) {
106- return tryExecute (getDefaultKeyCacheAllExecutor (executeFunc ), x );
130+ private Future <String > tryExecute (
131+ BiFunction <String ,List <String >,String > executeFunc ,
132+ BiFunction <String ,List <String >,String > mapFunc ) {
133+ return tryExecute (getDefaultKeyCacheAllExecutor (executeFunc , mapFunc ));
107134 }
108135
109- private < T > Future <String > tryExecute (BiFunction <T ,List <String >,String > executeFunc ) {
136+ private Future <String > tryExecute (BiFunction <String ,List <String >,String > executeFunc ) {
110137 return tryExecute (getDefaultKeyCacheAllExecutor (executeFunc ));
111138 }
112139
113- private Future <String > tryExecute (IdempotentExecutor executor ) {
114- return tryExecute (executor , "STUB" );
140+ private < R > Future <R > tryExecute (IdempotentExecutor < String , R > executor ) {
141+ return tryExecute (executor , input );
115142 }
116143
117- private <V > Future <V > tryExecute (IdempotentExecutor executor , V x ) {
144+ private <T , R > Future <R > tryExecute (IdempotentExecutor < T , R > executor , T x ) {
118145 try {
119- return executor .execute (x , Collections . singletonList ( "--mockParameter" ) );
146+ return executor .execute (x , params );
120147 } catch (ConcurrentException e ) {
121- logger .error ("Exception for conurent excception for exectur : {}" , executor , e );
148+ logger .error ("Exception for concurrent exception for executor : {}" , executor , e );
122149 }
123150 return null ;
124151 }
0 commit comments