66
77package org .hyperledger .fabric .contract .execution ;
88
9- import static org .hamcrest .Matchers .equalTo ;
10- import static org .junit .Assert .assertThat ;
11- import static org .mockito .ArgumentMatchers .any ;
12- import static org .mockito .Mockito .mock ;
13- import static org .mockito .Mockito .spy ;
14- import static org .mockito .Mockito .verify ;
15- import static org .mockito .Mockito .when ;
16-
17- import java .lang .reflect .InvocationTargetException ;
18- import java .util .ArrayList ;
19-
9+ import contract .SampleContract ;
2010import org .hyperledger .fabric .contract .ChaincodeStubNaiveImpl ;
2111import org .hyperledger .fabric .contract .Context ;
2212import org .hyperledger .fabric .contract .ContractInterface ;
2313import org .hyperledger .fabric .contract .ContractRuntimeException ;
14+ import org .hyperledger .fabric .contract .annotation .Serializer ;
2415import org .hyperledger .fabric .contract .execution .impl .ContractExecutionService ;
16+ import org .hyperledger .fabric .contract .metadata .TypeSchema ;
17+ import org .hyperledger .fabric .contract .routing .ParameterDefinition ;
2518import org .hyperledger .fabric .contract .routing .TxFunction ;
19+ import org .hyperledger .fabric .contract .routing .impl .ParameterDefinitionImpl ;
20+ import org .hyperledger .fabric .contract .routing .impl .SerializerRegistryImpl ;
2621import org .hyperledger .fabric .shim .Chaincode .Response ;
2722import org .hyperledger .fabric .shim .ChaincodeStub ;
2823import org .junit .Rule ;
2924import org .junit .Test ;
3025import org .junit .rules .ExpectedException ;
3126
32- import contract .SampleContract ;
27+ import java .lang .reflect .InvocationTargetException ;
28+ import java .lang .reflect .Method ;
29+ import java .lang .reflect .Parameter ;
30+ import java .util .ArrayList ;
31+ import java .util .Collections ;
32+
33+ import static org .hamcrest .Matchers .equalTo ;
34+ import static org .junit .Assert .assertThat ;
35+ import static org .mockito .ArgumentMatchers .any ;
36+ import static org .mockito .Mockito .mock ;
37+ import static org .mockito .Mockito .spy ;
38+ import static org .mockito .Mockito .verify ;
39+ import static org .mockito .Mockito .when ;
3340
3441public class ContractExecutionServiceTest {
3542 @ Rule
@@ -39,10 +46,9 @@ public class ContractExecutionServiceTest {
3946 @ Test
4047 public void noReturnValue ()
4148 throws IllegalAccessException , InstantiationException , InvocationTargetException , NoSuchMethodException , SecurityException {
42-
4349 JSONTransactionSerializer jts = new JSONTransactionSerializer ();
44-
45- ContractExecutionService ces = new ContractExecutionService (jts );
50+ SerializerRegistryImpl serializerRegistry = spy ( new SerializerRegistryImpl ());
51+ ContractExecutionService ces = new ContractExecutionService (serializerRegistry );
4652
4753 ContractInterface contract = spy (new SampleContract ());
4854 TxFunction txFn = mock (TxFunction .class );
@@ -55,6 +61,7 @@ public void noReturnValue()
5561 when (req .getArgs ()).thenReturn (new ArrayList <byte []>());
5662 when (routing .getMethod ()).thenReturn (SampleContract .class .getMethod ("noReturn" , new Class <?>[] {Context .class }));
5763 when (routing .getContractInstance ()).thenReturn (contract );
64+ when (serializerRegistry .getSerializer (any (), any ())).thenReturn (jts );
5865 ces .executeRequest (txFn , req , stub );
5966
6067 verify (contract ).beforeTransaction (any ());
@@ -65,9 +72,9 @@ public void noReturnValue()
6572 @ Test ()
6673 public void failureToInvoke ()
6774 throws IllegalAccessException , InstantiationException , InvocationTargetException , NoSuchMethodException , SecurityException {
68-
6975 JSONTransactionSerializer jts = new JSONTransactionSerializer ();
70- ContractExecutionService ces = new ContractExecutionService (jts );
76+ SerializerRegistryImpl serializerRegistry = spy (new SerializerRegistryImpl ());
77+ ContractExecutionService ces = new ContractExecutionService (serializerRegistry );
7178
7279 spy (new SampleContract ());
7380 TxFunction txFn = mock (TxFunction .class );
@@ -83,6 +90,7 @@ public void failureToInvoke()
8390
8491 when (routing .getContractInstance ()).thenThrow (IllegalAccessException .class );
8592 when (routing .toString ()).thenReturn ("MockMethodName:MockClassName" );
93+ when (serializerRegistry .getSerializer (any (), any ())).thenReturn (jts );
8694
8795 thrown .expect (ContractRuntimeException .class );
8896 thrown .expectMessage ("Could not execute contract method: MockMethodName:MockClassName" );
@@ -91,4 +99,51 @@ public void failureToInvoke()
9199 assertThat (resp .getStatusCode (), equalTo (500 ));
92100 }
93101
102+ @ SuppressWarnings ({ "serial" })
103+ @ Test ()
104+ public void invokeWithDifferentSerializers ()
105+ throws NoSuchMethodException , InvocationTargetException , IllegalAccessException , InstantiationException {
106+ JSONTransactionSerializer defaultSerializer = spy (new JSONTransactionSerializer ());
107+ SerializerInterface customSerializer = mock (SerializerInterface .class );
108+ SerializerRegistryImpl serializerRegistry = spy (new SerializerRegistryImpl ());
109+ ExecutionService executionService = ExecutionFactory .getInstance ().createExecutionService (serializerRegistry );
110+
111+ TxFunction txFn = mock (TxFunction .class );
112+ InvocationRequest req = mock (InvocationRequest .class );
113+ TxFunction .Routing routing = mock (TxFunction .Routing .class );
114+
115+ TypeSchema ts = TypeSchema .typeConvert (String .class );
116+ Method method = SampleContract .class .getMethod ("t1" , Context .class , String .class );
117+ Parameter [] params = method .getParameters ();
118+ ParameterDefinition pd = new ParameterDefinitionImpl ("arg1" , String .class , ts , params [1 ]);
119+
120+ byte [] arg = "asdf" .getBytes ();
121+ ChaincodeStub stub = new ChaincodeStubNaiveImpl ();
122+ ContractInterface contract = spy (new SampleContract ());
123+
124+ when (req .getArgs ()).thenReturn (Collections .singletonList (arg ));
125+ when (txFn .getRouting ()).thenReturn (routing );
126+ when (txFn .getParamsList ()).thenReturn (Collections .singletonList (pd ));
127+ when (txFn .getReturnSchema ()).thenReturn (ts );
128+ when (routing .getMethod ()).thenReturn (method );
129+ when (routing .getContractInstance ()).thenReturn (contract );
130+
131+ String defaultSerializerName = defaultSerializer .getClass ().getCanonicalName ();
132+ String customSerializerName = "customSerializer" ;
133+
134+ // execute transaction with the default serializer
135+ when (routing .getSerializerName ()).thenReturn (defaultSerializerName );
136+ when (serializerRegistry .getSerializer (defaultSerializerName , Serializer .TARGET .TRANSACTION ))
137+ .thenReturn (defaultSerializer );
138+ executionService .executeRequest (txFn , req , stub );
139+
140+ // execute transaction with the custom serializer
141+ when (routing .getSerializerName ()).thenReturn (customSerializerName );
142+ when (serializerRegistry .getSerializer (customSerializerName , Serializer .TARGET .TRANSACTION ))
143+ .thenReturn (customSerializer );
144+ executionService .executeRequest (txFn , req , stub );
145+
146+ verify (defaultSerializer ).fromBuffer (arg , ts );
147+ verify (customSerializer ).fromBuffer (arg , ts );
148+ }
94149}
0 commit comments