88import org .junit .Test ;
99
1010import static junit .framework .TestCase .assertNull ;
11+ import static junit .framework .TestCase .fail ;
12+ import static org .hamcrest .CoreMatchers .containsString ;
1113import static org .hamcrest .core .Is .is ;
1214import static org .hamcrest .core .IsCollectionContaining .hasItem ;
1315import static org .junit .Assert .assertThat ;
@@ -21,55 +23,108 @@ public class JsonConfigCollectionTest {
2123 private JsonObject pipeInGroup ;
2224
2325 @ Before
24- public void SetUp ()
25- {
26+ public void setUp () {
2627 jsonCollection = new JsonConfigCollection ();
2728
2829 pipe1 = new JsonObject ();
29- pipe1 .addProperty ("name" ,"pipe1" );
30+ pipe1 .addProperty ("name" , "pipe1" );
3031
3132 pipe2 = new JsonObject ();
32- pipe2 .addProperty ("name" ,"pipe2" );
33+ pipe2 .addProperty ("name" , "pipe2" );
3334
3435 pipeInGroup = new JsonObject ();
35- pipeInGroup .addProperty ("name" ,"pipe3" );
36- pipeInGroup .addProperty ("group" ,"mygroup" );
36+ pipeInGroup .addProperty ("name" , "pipe3" );
37+ pipeInGroup .addProperty ("group" , "mygroup" );
3738
3839 devEnv = new JsonObject ();
39- devEnv .addProperty ("name" ,"dev" );
40+ devEnv .addProperty ("name" , "dev" );
4041 }
4142
4243 @ Test
43- public void shouldReturnTargetVersion ()
44- {
44+ public void shouldReturnDefaultTargetVersionWhenThereAreNoPipelinesOrEnvironmentsDefined () {
45+ jsonCollection . updateVersionFromPipelinesAndEnvironments ();
4546 JsonObject jsonObject = jsonCollection .getJsonObject ();
46- assertThat (jsonObject .get ("target_version" ) instanceof JsonPrimitive ,is (true ));
47- assertThat (jsonObject .getAsJsonPrimitive ("target_version" ).getAsInt (), is (1 ));
47+ assertTargetVersion (jsonObject , 1 );
4848 }
4949
5050 @ Test
51- public void shouldReturnEnvironmentsArrayInJsonObjectWhenEmpty ()
52- {
51+ public void shouldReturnEnvironmentsArrayInJsonObjectWhenEmpty () {
5352 JsonObject jsonObject = jsonCollection .getJsonObject ();
54- assertThat (jsonObject .get ("environments" ) instanceof JsonArray ,is (true ));
53+ assertThat (jsonObject .get ("environments" ) instanceof JsonArray , is (true ));
5554 assertThat (jsonObject .getAsJsonArray ("environments" ), is (new JsonArray ()));
5655 }
5756
5857 @ Test
59- public void shouldAppendPipelinesToPipelinesCollection ()
60- {
61- jsonCollection .addPipeline (pipe1 ,"pipe1.json" );
62- jsonCollection .addPipeline (pipe2 ,"pipe2.json" );
58+ public void shouldAppendPipelinesToPipelinesCollection () {
59+ jsonCollection .addPipeline (pipe1 , "pipe1.json" );
60+ jsonCollection .addPipeline (pipe2 , "pipe2.json" );
6361 JsonObject jsonObject = jsonCollection .getJsonObject ();
64- assertThat (jsonObject .getAsJsonArray ("pipelines" ).size (),is (2 ));
62+ assertThat (jsonObject .getAsJsonArray ("pipelines" ).size (), is (2 ));
6563 }
6664
6765
6866 @ Test
69- public void shouldReturnEnvironmentsInJsonObject ()
70- {
71- jsonCollection .addEnvironment (devEnv ,"dev.json" );
67+ public void shouldReturnEnvironmentsInJsonObject () {
68+ jsonCollection .addEnvironment (devEnv , "dev.json" );
7269 JsonObject jsonObject = jsonCollection .getJsonObject ();
73- assertThat (jsonObject .getAsJsonArray ("environments" ).size (),is (1 ));
70+ assertThat (jsonObject .getAsJsonArray ("environments" ).size (), is (1 ));
71+ }
72+
73+ @ Test
74+ public void shouldUpdateTargetVersionWhenItIsTheSameAcrossAllPipelinesAndEnvironments () {
75+ jsonCollection .addPipeline (pipelineWithVersion (2 ), "pipe1.json" );
76+ jsonCollection .addPipeline (pipelineWithVersion (2 ), "pipe2.json" );
77+ jsonCollection .addEnvironment (envWithVersion (2 ), "env1.json" );
78+ jsonCollection .addEnvironment (envWithVersion (2 ), "env2.json" );
79+
80+ jsonCollection .updateVersionFromPipelinesAndEnvironments ();
81+ JsonObject jsonObject = jsonCollection .getJsonObject ();
82+
83+ assertTargetVersion (jsonObject , 2 );
84+ }
85+
86+ @ Test
87+ public void shouldUpdateTargetVersionWhenItIsTheDefaultOrMissingAcrossAllPipelinesAndEnvironments () {
88+ int defaultVersionExpected = 1 ;
89+
90+ jsonCollection .addPipeline (new JsonObject (), "pipe1.json" );
91+ jsonCollection .addPipeline (pipelineWithVersion (defaultVersionExpected ), "pipe2.json" );
92+ jsonCollection .addEnvironment (new JsonObject (), "env1.json" );
93+ jsonCollection .addEnvironment (envWithVersion (defaultVersionExpected ), "env2.json" );
94+
95+ jsonCollection .updateVersionFromPipelinesAndEnvironments ();
96+ JsonObject jsonObject = jsonCollection .getJsonObject ();
97+
98+ assertTargetVersion (jsonObject , defaultVersionExpected );
99+ }
100+
101+ @ Test
102+ public void shouldFailToUpdateTargetVersionWhenItIs_NOT_TheSameAcrossAllPipelinesAndEnvironments () {
103+ jsonCollection .addPipeline (pipelineWithVersion (1 ), "pipe1.json" );
104+ jsonCollection .addPipeline (pipelineWithVersion (2 ), "pipe2.json" );
105+ jsonCollection .addEnvironment (envWithVersion (1 ), "env1.json" );
106+ jsonCollection .addEnvironment (new JsonObject (), "env2.json" );
107+
108+ try {
109+ jsonCollection .updateVersionFromPipelinesAndEnvironments ();
110+ fail ("Should have failed to find a unique version" );
111+ } catch (RuntimeException e ) {
112+ assertThat (e .getMessage (), containsString ("Versions across files are not unique" ));
113+ }
114+ }
115+
116+ private JsonElement envWithVersion (int version ) {
117+ return pipelineWithVersion (version );
118+ }
119+
120+ private JsonElement pipelineWithVersion (int version ) {
121+ JsonObject jsonObject = new JsonObject ();
122+ jsonObject .addProperty ("format_version" , version );
123+ return jsonObject ;
124+ }
125+
126+ private void assertTargetVersion (JsonObject jsonObject , int expectedVersion ) {
127+ assertThat (jsonObject .get ("target_version" ) instanceof JsonPrimitive , is (true ));
128+ assertThat (jsonObject .getAsJsonPrimitive ("target_version" ).getAsInt (), is (expectedVersion ));
74129 }
75130}
0 commit comments