33import com .contentstack .cms .TestClient ;
44import com .contentstack .cms .Utils ;
55import com .contentstack .cms .core .Util ;
6+
67import okhttp3 .Request ;
78import okhttp3 .ResponseBody ;
89import retrofit2 .Response ;
910
1011import java .io .IOException ;
1112
1213import org .json .simple .JSONObject ;
14+ import org .json .simple .parser .JSONParser ;
15+ import org .json .simple .parser .ParseException ;
1316import org .junit .jupiter .api .*;
17+ import static org .junit .jupiter .api .Assertions .*;
1418
1519@ Tag ("api" )
1620@ TestInstance (TestInstance .Lifecycle .PER_CLASS )
21+ @ TestMethodOrder (MethodOrderer .OrderAnnotation .class )
22+
1723class ReleaseAPITest {
1824
1925 public static Release releases ;
2026 protected static String API_KEY = TestClient .API_KEY ;
2127 protected static String MANAGEMENT_TOKEN = TestClient .MANAGEMENT_TOKEN ;
2228 protected static Stack stack ;
23- private int _COUNT = 2 ;
24- protected String releaseUid ;
29+ protected String releaseUid1 ;
30+ protected String releaseUid2 ;
31+ JSONParser parser = new JSONParser ();
2532
2633 @ BeforeAll
2734 public void setUp () {
2835 stack = TestClient .getStack ().addHeader (Util .API_KEY , API_KEY )
2936 .addHeader ("api_key" , API_KEY );
30-
3137 }
3238
3339 @ Order (1 )
3440 @ Test
35- void testCreateRelease () throws IOException {
41+ void testCreateRelease () throws IOException , ParseException {
3642 JSONObject requestBody = Utils .readJson ("releases/create_release1.json" );
37- releases = stack .releases ();
38- Response <ResponseBody > response = releases .create (requestBody ).execute ();
39- if (response .isSuccessful ()) {
43+ Response <ResponseBody > response = stack .releases ().create (requestBody ).execute ();
4044
41- System . out . println ( "Release created successfully: " + response . body (). string () );
45+ assertTrue ( response . isSuccessful (), "Release creation should be successful" );
4246
43- } else {
44- System .err .println ("Failed to create release: " + response .errorBody ().string ());
45- }
47+ String responseString = response .body ().string ();
48+ JSONObject responseObject = (JSONObject ) parser .parse (responseString );
49+ JSONObject releaseObject = (JSONObject ) responseObject .get ("release" );
50+ releaseUid1 = (String ) releaseObject .get ("uid" );
51+
52+ assertNotNull (releaseUid1 , "Release UID should not be null" );
53+ assertEquals ("First release" , releaseObject .get ("name" ), "Release name should match" );
54+ assertEquals ("Adding release date" , releaseObject .get ("description" ),
55+ "Release description should match" );
56+ assertEquals ("Release created successfully." , responseObject .get ("notice" ), "Success notice should be present" );
4657 }
4758
4859 @ Order (2 )
4960 @ Test
50- void testFetchReleases () throws IOException {
51- Response <ResponseBody > response = releases .fetch ().execute ();
52- if (response .isSuccessful ()) {
53- System .out .println ("Fetched releases successfully: " + response .body ().string ());
54- } else {
55- System .err .println ("Failed to fetch releases: " + response .errorBody ().string ());
61+ void testFindReleases () throws IOException , ParseException {
62+ Response <ResponseBody > response = stack .releases ().find ().execute ();
63+
64+ assertTrue (response .isSuccessful (), "Fetch releases should be successful" );
65+
66+ String responseString = response .body ().string ();
67+ JSONObject responseObject = (JSONObject ) parser .parse (responseString );
68+ org .json .simple .JSONArray releases = (org .json .simple .JSONArray ) responseObject .get ("releases" );
69+
70+ assertNotNull (releases , "Releases array should not be null" );
71+ assertFalse (releases .isEmpty (), "Releases array should not be empty" );
72+
73+ boolean foundRelease = false ;
74+ for (Object obj : releases ) {
75+ JSONObject release = (JSONObject ) obj ;
76+ if (releaseUid1 .equals (release .get ("uid" ))) {
77+ foundRelease = true ;
78+ assertEquals ("First release" , release .get ("name" ), "Release name should match" );
79+ break ;
80+ }
5681 }
82+
83+ assertTrue (foundRelease , "Created release should be present in the releases list" );
84+ }
85+
86+ @ Test
87+ @ Order (3 )
88+ void testUpdateRelease () throws IOException , ParseException {
89+ assertNotNull (releaseUid1 , "Release UID should be available for updating" );
90+
91+ JSONObject requestBody = Utils .readJson ("releases/update_release1.json" );
92+
93+ Response <ResponseBody > response = stack .releases (releaseUid1 ).update (requestBody ).execute ();
94+ assertTrue (response .isSuccessful (), "Release update should be successful" );
95+
96+ String responseString = response .body ().string ();
97+ JSONObject responseObject = (JSONObject ) parser .parse (responseString );
98+ JSONObject releaseObject = (JSONObject ) responseObject .get ("release" );
99+ assertNotNull (releaseObject , "Release object should not be null" );
100+ assertEquals (releaseUid1 , releaseObject .get ("uid" ), "Release UID should match" );
101+ assertEquals ("First release update" , releaseObject .get ("name" ), "Release name should match" );
102+ assertEquals ("Adding release date" , releaseObject .get ("description" ),
103+ "Release description should match" );
104+ assertEquals ("Release updated successfully." , responseObject .get ("notice" ), "Success notice should be present" );
105+ }
106+
107+ @ Test
108+ @ Order (4 )
109+ void testFetchReleaseByUid () throws IOException , ParseException {
110+ assertNotNull (releaseUid1 , "Release UID should be available for fetching" );
111+
112+ Response <ResponseBody > response = stack .releases (releaseUid1 ).fetch ().execute ();
113+
114+ assertTrue (response .isSuccessful (), "Fetch release by UID should be successful" );
115+
116+ String responseString = response .body ().string ();
117+ JSONObject responseObject = (JSONObject ) parser .parse (responseString );
118+ JSONObject releaseObject = (JSONObject ) responseObject .get ("release" );
119+
120+ assertNotNull (releaseObject , "Release object should not be null" );
121+ assertEquals (releaseUid1 , releaseObject .get ("uid" ), "Release UID should match" );
122+ assertEquals ("First release update" , releaseObject .get ("name" ), "Release name should match" );
123+ }
124+
125+ @ Order (5 )
126+ @ Test
127+ void testCloneRelease () throws IOException , ParseException {
128+ JSONObject requestBody = Utils .readJson ("releases/create_release1.json" );
129+ Response <ResponseBody > response = stack .releases (releaseUid1 ).clone (requestBody ).execute ();
130+
131+ assertTrue (response .isSuccessful (), "Clone release should be successful" );
132+
133+ String responseString = response .body ().string ();
134+ JSONObject responseObject = (JSONObject ) parser .parse (responseString );
135+ JSONObject releaseObject = (JSONObject ) responseObject .get ("release" );
136+ releaseUid2 = (String ) releaseObject .get ("uid" );
137+
138+ assertNotNull (releaseUid2 , "Clone release UID should not be null" );
139+ assertEquals ("First release" , releaseObject .get ("name" ), "Clone release name should match" );
140+ assertEquals ("Adding release date" , releaseObject .get ("description" ),
141+ "Second release description should match" );
142+ assertEquals ("Release cloned successfully." , responseObject .get ("notice" ), "Success notice should be present" );
143+ }
144+
145+ @ Order (6 )
146+ @ Test
147+ void testDeployRelease () throws IOException , ParseException {
148+ JSONObject requestBody = Utils .readJson ("releases/create_release1.json" );
149+
150+ assertNotNull (releaseUid2 , "Release UID should be available for deployment" );
151+ Request request = stack .releases (releaseUid1 ).deploy (requestBody ).request ();
152+ Assertions .assertEquals ("POST" , request .method (), "Request method should be PUT" );
153+ Assertions .assertTrue (request .url ().toString ().contains (releaseUid1 ),
154+ "Request URL should contain the release UID" );
155+ Assertions .assertEquals ("releases" , request .url ().pathSegments ().get (1 ));
156+ Assertions .assertEquals ("v3" , request .url ().pathSegments ().get (0 ));
157+ }
158+
159+ @ Order (7 )
160+ @ Test
161+ void testDeleteRelease1 () throws IOException , ParseException {
162+ assertNotNull (releaseUid1 , "Release UID should be available for deletion" );
163+
164+ Response <ResponseBody > response = stack .releases (releaseUid1 ).delete ().execute ();
165+
166+ assertTrue (response .isSuccessful (), "Release deletion should be successful" );
167+
168+ String responseString = response .body ().string ();
169+ JSONObject responseObject = (JSONObject ) parser .parse (responseString );
170+
171+ assertEquals ("Release deleted successfully." , responseObject .get ("notice" ), "Success notice should be present" );
172+ }
173+
174+ @ Order (8 )
175+ @ Test
176+ void testDeleteRelease2 () throws IOException , ParseException {
177+ assertNotNull (releaseUid2 , "Release UID should be available for deletion" );
178+
179+ Response <ResponseBody > response = stack .releases (releaseUid2 ).delete ().execute ();
180+
181+ assertTrue (response .isSuccessful (), "Release deletion should be successful" );
182+
183+ String responseString = response .body ().string ();
184+ JSONObject responseObject = (JSONObject ) parser .parse (responseString );
185+
186+ assertEquals ("Release deleted successfully." , responseObject .get ("notice" ), "Success notice should be present" );
57187 }
58188}
0 commit comments