File tree Expand file tree Collapse file tree 3 files changed +116
-0
lines changed
fabric-chaincode-shim/src
main/java/org/hyperledger/fabric/contract
test/java/org/hyperledger/fabric/contract Expand file tree Collapse file tree 3 files changed +116
-0
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ Copyright IBM Corp., DTCC All Rights Reserved.
3+
4+ SPDX-License-Identifier: Apache-2.0
5+ */
6+
7+ package org .hyperledger .fabric .contract ;
8+
9+ import org .hyperledger .fabric .shim .ChaincodeStub ;
10+
11+ /**
12+ * Interface all contracts should implement
13+ */
14+ public interface ContractInterface {
15+ /**
16+ * Returns instance of context associated with current invocation
17+ * @return
18+ */
19+ default Context getContext () { throw new IllegalStateException ("getContext default implementation can't be directly invoked" ); }
20+
21+ /**
22+ * Create context from {@link ChaincodeStub}, default impl provided, but can be overwritten by contract
23+ * @param stub
24+ * @return
25+ */
26+ default Context createContext (ChaincodeStub stub ) { return ContextFactory .getInstance ().createContext (stub ); }
27+
28+ /**
29+ * Invoked once method for transaction not exist in contract
30+ */
31+ default void unknownTransaction () {
32+ throw new IllegalStateException ("Undefined contract method called" );
33+ }
34+
35+ /**
36+ * Invoke before each transaction method
37+ */
38+ default void beforeTransaction () {}
39+
40+ /**
41+ * Invoke after each transaction method
42+ */
43+ default void afterTransaction () {}
44+ }
Original file line number Diff line number Diff line change 1+ /*
2+ Copyright IBM Corp., DTCC All Rights Reserved.
3+
4+ SPDX-License-Identifier: Apache-2.0
5+ */
6+
7+ package org .hyperledger .fabric .contract .annotation ;
8+
9+ import io .swagger .v3 .oas .annotations .info .Info ;
10+
11+ import java .lang .annotation .ElementType ;
12+ import java .lang .annotation .Retention ;
13+ import java .lang .annotation .Target ;
14+
15+ import static java .lang .annotation .RetentionPolicy .RUNTIME ;
16+
17+ /**
18+ * Class level annotation that identifies this class as being a contract.
19+ */
20+ @ Retention (RUNTIME )
21+ @ Target (ElementType .TYPE )
22+ public @interface Contract {
23+ Info info ();
24+ String namespace () default "" ;
25+ }
Original file line number Diff line number Diff line change 1+ /*
2+ Copyright IBM Corp. All Rights Reserved.
3+
4+ SPDX-License-Identifier: Apache-2.0
5+ */
6+ package org .hyperledger .fabric .contract ;
7+
8+ import org .junit .Rule ;
9+ import org .junit .Test ;
10+ import org .junit .rules .ExpectedException ;
11+
12+ import static org .hamcrest .Matchers .*;
13+ import static org .junit .Assert .*;
14+
15+ public class ContractInterfaceTest {
16+ @ Rule
17+ public ExpectedException thrown = ExpectedException .none ();
18+
19+ @ Test
20+ public void getContext () {
21+ thrown .expect (IllegalStateException .class );
22+ thrown .expectMessage ("getContext default implementation can't be directly invoked" );
23+ new ContractInterface () {}.getContext ();
24+ }
25+
26+ @ Test
27+ public void createContext () {
28+ assertThat ((new ContractInterface (){}).createContext (new ChaincodeStubNaiveImpl ()), is (instanceOf (Context .class )));
29+ }
30+
31+ @ Test
32+ public void unknownTransaction () {
33+ thrown .expect (IllegalStateException .class );
34+ thrown .expectMessage ("Undefined contract method called" );
35+ new ContractInterface () {}.unknownTransaction ();
36+ }
37+
38+ @ Test
39+ public void beforeTransaction () {
40+ new ContractInterface () {}.beforeTransaction ();
41+ }
42+
43+ @ Test
44+ public void afterTransaction () {
45+ new ContractInterface () {}.afterTransaction ();
46+ }
47+ }
You can’t perform that action at this time.
0 commit comments