1+ /*
2+ * SPDX-License-Identifier: Apache License 2.0
3+ */
4+
5+ package org .example ;
6+
7+ import static java .nio .charset .StandardCharsets .UTF_8 ;
8+ import static org .junit .jupiter .api .Assertions .assertEquals ;
9+ import static org .junit .jupiter .api .Assertions .assertFalse ;
10+ import static org .junit .jupiter .api .Assertions .assertThrows ;
11+ import static org .junit .jupiter .api .Assertions .assertTrue ;
12+ import static org .mockito .Mockito .mock ;
13+ import static org .mockito .Mockito .verify ;
14+ import static org .mockito .Mockito .when ;
15+
16+ import java .nio .charset .StandardCharsets ;
17+ import org .example .contract .MyAsset ;
18+ import org .example .contract .MyAssetContract ;
19+ import org .hyperledger .fabric .contract .Context ;
20+ import org .hyperledger .fabric .shim .ChaincodeServerProperties ;
21+ import org .hyperledger .fabric .shim .ChaincodeStub ;
22+ import org .junit .jupiter .api .Nested ;
23+ import org .junit .jupiter .api .Test ;
24+
25+
26+ public final class MyAssetContractTest {
27+
28+ @ Nested
29+ class AssetExists {
30+
31+ @ Test
32+ public void noProperAsset () {
33+
34+ MyAssetContract contract = new MyAssetContract ();
35+ Context ctx = mock (Context .class );
36+ ChaincodeStub stub = mock (ChaincodeStub .class );
37+ when (ctx .getStub ()).thenReturn (stub );
38+
39+ when (stub .getState ("10001" )).thenReturn (new byte []{});
40+ boolean result = contract .myAssetExists (ctx , "10001" );
41+
42+ assertFalse (result );
43+ }
44+
45+ @ Test
46+ public void assetExists () {
47+
48+ MyAssetContract contract = new MyAssetContract ();
49+ Context ctx = mock (Context .class );
50+ ChaincodeStub stub = mock (ChaincodeStub .class );
51+ when (ctx .getStub ()).thenReturn (stub );
52+
53+ when (stub .getState ("10001" )).thenReturn (new byte []{42 });
54+ boolean result = contract .myAssetExists (ctx , "10001" );
55+
56+ assertTrue (result );
57+
58+ }
59+
60+ @ Test
61+ public void noKey () {
62+ MyAssetContract contract = new MyAssetContract ();
63+ Context ctx = mock (Context .class );
64+ ChaincodeStub stub = mock (ChaincodeStub .class );
65+ when (ctx .getStub ()).thenReturn (stub );
66+
67+ when (stub .getState ("10002" )).thenReturn (null );
68+ boolean result = contract .myAssetExists (ctx , "10002" );
69+
70+ assertFalse (result );
71+
72+ }
73+
74+ }
75+
76+ @ Nested
77+ class AssetCreates {
78+
79+ @ Test
80+ public void newAssetCreate () {
81+ MyAssetContract contract = new MyAssetContract ();
82+ Context ctx = mock (Context .class );
83+ ChaincodeStub stub = mock (ChaincodeStub .class );
84+ when (ctx .getStub ()).thenReturn (stub );
85+
86+ String json = "{\" value\" :\" TheAsset\" }" ;
87+
88+ contract .createMyAsset (ctx , "10001" , "TheAsset" );
89+
90+ verify (stub ).putState ("10001" , json .getBytes (UTF_8 ));
91+ }
92+
93+ @ Test
94+ public void alreadyExists () {
95+ MyAssetContract contract = new MyAssetContract ();
96+ Context ctx = mock (Context .class );
97+ ChaincodeStub stub = mock (ChaincodeStub .class );
98+ when (ctx .getStub ()).thenReturn (stub );
99+
100+ when (stub .getState ("10002" )).thenReturn (new byte []{42 });
101+
102+ Exception thrown = assertThrows (RuntimeException .class , () -> {
103+ contract .createMyAsset (ctx , "10002" , "TheAsset" );
104+ });
105+
106+ assertEquals (thrown .getMessage (), "The asset 10002 already exists" );
107+
108+ }
109+
110+ }
111+
112+ @ Test
113+ public void assetRead () {
114+ MyAssetContract contract = new MyAssetContract ();
115+ Context ctx = mock (Context .class );
116+ ChaincodeStub stub = mock (ChaincodeStub .class );
117+ when (ctx .getStub ()).thenReturn (stub );
118+
119+ MyAsset asset = new MyAsset ();
120+ asset .setValue ("Valuable" );
121+
122+ String json = asset .toJSONString ();
123+ when (stub .getState ("10001" )).thenReturn (json .getBytes (StandardCharsets .UTF_8 ));
124+
125+ MyAsset returnedAsset = contract .readMyAsset (ctx , "10001" );
126+ assertEquals (returnedAsset .getValue (), asset .getValue ());
127+ }
128+
129+ @ Nested
130+ class AssetUpdates {
131+
132+ @ Test
133+ public void updateExisting () {
134+ MyAssetContract contract = new MyAssetContract ();
135+ Context ctx = mock (Context .class );
136+ ChaincodeStub stub = mock (ChaincodeStub .class );
137+ when (ctx .getStub ()).thenReturn (stub );
138+ when (stub .getState ("10001" )).thenReturn (new byte []{42 });
139+
140+ contract .updateMyAsset (ctx , "10001" , "updates" );
141+
142+ String json = "{\" value\" :\" updates\" }" ;
143+ verify (stub ).putState ("10001" , json .getBytes (UTF_8 ));
144+ }
145+
146+ @ Test
147+ public void updateMissing () {
148+ MyAssetContract contract = new MyAssetContract ();
149+ Context ctx = mock (Context .class );
150+ ChaincodeStub stub = mock (ChaincodeStub .class );
151+ when (ctx .getStub ()).thenReturn (stub );
152+
153+ when (stub .getState ("10001" )).thenReturn (null );
154+
155+ Exception thrown = assertThrows (RuntimeException .class , () -> {
156+ contract .updateMyAsset (ctx , "10001" , "TheAsset" );
157+ });
158+
159+ assertEquals (thrown .getMessage (), "The asset 10001 does not exist" );
160+ }
161+
162+ }
163+
164+ @ Test
165+ public void assetDelete () {
166+ MyAssetContract contract = new MyAssetContract ();
167+ Context ctx = mock (Context .class );
168+ ChaincodeStub stub = mock (ChaincodeStub .class );
169+ when (ctx .getStub ()).thenReturn (stub );
170+ when (stub .getState ("10001" )).thenReturn (null );
171+
172+ Exception thrown = assertThrows (RuntimeException .class , () -> {
173+ contract .deleteMyAsset (ctx , "10001" );
174+ });
175+
176+ assertEquals (thrown .getMessage (), "The asset 10001 does not exist" );
177+ }
178+
179+ @ Test
180+ public void test () {
181+ ChaincodeServerProperties chaincodeServerProperties = new ChaincodeServerProperties ();
182+ chaincodeServerProperties .setKeepAliveTimeMinutes (-10 );
183+
184+ assertEquals (true , true );
185+ }
186+
187+ }
0 commit comments