@@ -33,12 +33,21 @@ public class BranchAPITest {
3333 static final String UNKNOWN_ENV = "no_such_env_dx31564" ;
3434 static final String SOURCE = "main" ;
3535
36- /** Unique per run, so reruns on the same stack do not collide. */
37- private static final String RUN = "dx31564_" + Long .toString (System .currentTimeMillis (), 36 );
38- private static final String UID_TWO = RUN + "_two" ;
39- private static final String UID_EMPTY = RUN + "_empty" ;
40- private static final String UID_UNKNOWN = RUN + "_unknown" ;
41- private static final String UID_MIXED = RUN + "_mixed" ;
36+ /**
37+ * Unique per run, so reruns on the same stack do not collide. Branch UIDs allow at most
38+ * 15 characters, lowercase letters and digits only: "b" plus the last 5 base-36 digits of
39+ * the current time, then one letter per UID below (7 characters in total).
40+ */
41+ private static final String RUN = runId ();
42+ private static final String UID_TWO = RUN + "a" ;
43+ private static final String UID_EMPTY = RUN + "b" ;
44+ private static final String UID_UNKNOWN = RUN + "c" ;
45+ private static final String UID_MIXED = RUN + "d" ;
46+
47+ private static String runId () {
48+ String time = Long .toString (System .currentTimeMillis (), 36 );
49+ return "b" + time .substring (time .length () - 5 );
50+ }
4251
4352 /** Branches this class created, deleted in {@link #cleanUp()}. */
4453 private static final List <String > CREATED = new ArrayList <>();
@@ -73,8 +82,8 @@ static void cleanUp() {
7382 void createWithTwoEnvironmentNames () throws IOException {
7483 Response <ResponseBody > response = stack .branch ()
7584 .create (body (UID_TWO , Arrays .asList (ENV_DEVELOPMENT , ENV_PRODUCTION ))).execute ();
76- assertTrue (response .isSuccessful (), "create with two environment names should succeed" );
7785 CREATED .add (UID_TWO );
86+ assertTrue (response .isSuccessful (), "create with two environment names should succeed" );
7887 }
7988
8089 @ Test
@@ -91,8 +100,8 @@ void fetchBranchCarriesTwoEnvironmentNames() throws Exception {
91100 void createWithEmptyListClonesNoEnvironments () throws Exception {
92101 Response <ResponseBody > response = stack .branch ()
93102 .create (body (UID_EMPTY , Collections .<String >emptyList ())).execute ();
94- assertTrue (response .isSuccessful (), "create with an empty list should succeed" );
95103 CREATED .add (UID_EMPTY );
104+ assertTrue (response .isSuccessful (), "create with an empty list should succeed" );
96105
97106 Response <ResponseBody > fetched = stack .branch (UID_EMPTY ).fetch ().execute ();
98107 assertTrue (fetched .isSuccessful (), "fetch of the created branch should succeed" );
@@ -106,6 +115,8 @@ void createWithUnknownNameIsRejectedAndCreatesNoBranch() throws Exception {
106115 .create (body (UID_UNKNOWN , Collections .singletonList (UNKNOWN_ENV ))).execute ();
107116 assertEquals (422 , response .code (), "an unknown environment name should give the API's 422" );
108117 assertNotNull (response .errorBody (), "the API's error body should be present" );
118+ assertTrue (response .errorBody ().string ().contains ("publish_config.environments" ),
119+ "the 422 body should name publish_config.environments" );
109120
110121 Response <ResponseBody > fetched = stack .branch (UID_UNKNOWN ).fetch ().execute ();
111122 assertFalse (fetched .isSuccessful (), "no branch should exist after a rejected create" );
@@ -118,6 +129,8 @@ void createWithMixedNamesIsRejectedAndCreatesNoBranch() throws Exception {
118129 .create (body (UID_MIXED , Arrays .asList (ENV_DEVELOPMENT , UNKNOWN_ENV ))).execute ();
119130 assertEquals (422 , response .code (), "a mix with an unknown name should give the API's 422" );
120131 assertNotNull (response .errorBody (), "the API's error body should be present" );
132+ assertTrue (response .errorBody ().string ().contains ("publish_config.environments" ),
133+ "the 422 body should name publish_config.environments" );
121134
122135 Response <ResponseBody > fetched = stack .branch (UID_MIXED ).fetch ().execute ();
123136 assertFalse (fetched .isSuccessful (), "no branch should exist after a rejected create" );
@@ -140,9 +153,13 @@ private static JSONObject body(String uid, List<String> environments) {
140153 private static List <String > environmentsOf (Response <ResponseBody > response ) throws Exception {
141154 JSONObject json = (JSONObject ) new JSONParser ().parse (response .body ().string ());
142155 JSONObject branch = (JSONObject ) json .get ("branch" );
156+ assertNotNull (branch , "the fetch response should carry a 'branch' object" );
143157 JSONObject publishConfig = (JSONObject ) branch .get ("publish_config" );
158+ assertNotNull (publishConfig , "the branch should carry a 'publish_config' object" );
159+ Object environments = publishConfig .get ("environments" );
160+ assertNotNull (environments , "publish_config should carry an 'environments' list" );
144161 List <String > names = new ArrayList <>();
145- for (Object name : (List <?>) publishConfig . get ( " environments" ) ) {
162+ for (Object name : (List <?>) environments ) {
146163 names .add ((String ) name );
147164 }
148165 return names ;
0 commit comments