66import java .util .ArrayList ;
77import java .util .Arrays ;
88import java .util .List ;
9+ import java .util .Objects ;
910
1011import com .meterware .simplestub .Memento ;
1112import oracle .kubernetes .utils .SystemClockTestSupport ;
1415import org .junit .Before ;
1516import org .junit .Test ;
1617
18+ import static oracle .kubernetes .operator .WebLogicConstants .SHUTDOWN_STATE ;
1719import static oracle .kubernetes .weblogic .domain .model .DomainConditionMatcher .hasCondition ;
1820import static oracle .kubernetes .weblogic .domain .model .DomainConditionType .Available ;
1921import static oracle .kubernetes .weblogic .domain .model .DomainConditionType .Failed ;
@@ -259,25 +261,86 @@ public void verifyThat_addServers_serverSortedInExpectedOrdering() {
259261 domainStatus .addServer (cluster1Server1 ).addServer (cluster2Server1 )
260262 .addServer (cluster1Server2 ).addServer (standAloneServerA ).addServer (adminServer );
261263
262- assertThat (domainStatus .servers ,
264+ assertThat (domainStatus .getServers () ,
263265 contains (adminServer , standAloneServerA , cluster1Server1 , cluster1Server2 , cluster2Server1 ));
264266 }
265267
266268 @ Test
267269 public void verifyThat_setServers_serverSortedInExpectedOrdering () {
268- ServerStatus cluster1Server1 = new ServerStatus ().withClusterName ("cluster-1" ).withServerName ("cluster1-server1" );
269- ServerStatus cluster1Server2 = new ServerStatus ().withClusterName ("cluster-1" ).withServerName ("cluster1-server2" );
270- ServerStatus cluster2Server1 = new ServerStatus ().withClusterName ("cluster-2" ).withServerName ("cluster2-server1" );
271- ServerStatus adminServer = new ServerStatus ().withServerName ("admin-server" ).withIsAdminServer (true );
272- ServerStatus standAloneServerA = new ServerStatus ().withServerName ("a" );
270+ ServerStatus cluster1Server1 = createStatus ().withClusterName ("cluster-1" ).withServerName ("cluster1-server1" );
271+ ServerStatus cluster1Server2 = createStatus ().withClusterName ("cluster-1" ).withServerName ("cluster1-server2" );
272+ ServerStatus cluster2Server1 = createStatus ().withClusterName ("cluster-2" ).withServerName ("cluster2-server1" );
273+ ServerStatus adminServer = createStatus ().withServerName ("admin-server" ).withIsAdminServer (true );
274+ ServerStatus standAloneServerA = createStatus ().withServerName ("a" );
273275
274276 domainStatus .setServers (Arrays .asList (cluster1Server1 ,
275277 cluster2Server1 , cluster1Server2 , standAloneServerA , adminServer ));
276278
277- assertThat (domainStatus .servers ,
279+ assertThat (domainStatus .getServers () ,
278280 contains (adminServer , standAloneServerA , cluster1Server1 , cluster1Server2 , cluster2Server1 ));
279281 }
280282
283+ private ServerStatus createStatus () {
284+ return new ServerStatus ().withState ("a" );
285+ }
286+
287+ @ Test
288+ public void whenMatchingServersExist_setServersUpdatesState () {
289+ domainStatus .addServer (new ServerStatus ().withClusterName ("1" ).withServerName ("1" ).withState ("state1" ));
290+ domainStatus .addServer (new ServerStatus ().withClusterName ("1" ).withServerName ("2" ).withState ("state1" ));
291+ domainStatus .addServer (new ServerStatus ().withClusterName ("1" ).withServerName ("3" ).withState ("state1" ));
292+
293+ domainStatus .setServers (Arrays .asList (
294+ new ServerStatus ().withClusterName ("1" ).withServerName ("1" ).withState ("state1" ),
295+ new ServerStatus ().withClusterName ("1" ).withServerName ("2" ).withState ("state1" ),
296+ new ServerStatus ().withServerName ("admin" ).withIsAdminServer (true ).withState ("state2" )
297+ ));
298+
299+ assertThat (getServer ("1" , "1" ).getState (), equalTo ("state1" ));
300+ assertThat (getServer ("1" , "2" ).getState (), equalTo ("state1" ));
301+ assertThat (getServer (null , "admin" ).getState (), equalTo ("state2" ));
302+ }
303+
304+ @ Test
305+ public void whenSetServerIncludesServerWithoutStateAndNoExistingState_defaultToSHUTDOWN () {
306+ domainStatus .addServer (new ServerStatus ().withClusterName ("1" ).withServerName ("1" ).withState ("state1" ));
307+ domainStatus .addServer (new ServerStatus ().withClusterName ("1" ).withServerName ("2" ).withState ("state1" ));
308+ domainStatus .addServer (new ServerStatus ().withClusterName ("1" ).withServerName ("3" ).withState ("state1" ));
309+
310+ domainStatus .setServers (Arrays .asList (
311+ new ServerStatus ().withClusterName ("1" ).withServerName ("1" ).withState ("state1" ),
312+ new ServerStatus ().withClusterName ("1" ).withServerName ("2" ).withState ("state1" ),
313+ new ServerStatus ().withClusterName ("1" ).withServerName ("3" ).withState ("state2" ),
314+ new ServerStatus ().withClusterName ("2" ).withServerName ("1" )
315+ ));
316+
317+ assertThat (getServer ("2" , "1" ).getState (), equalTo (SHUTDOWN_STATE ));
318+ }
319+
320+ @ Test
321+ public void whenSetServerIncludesServerWithoutStateAndHasExistingState_preserveIt () {
322+ domainStatus .addServer (new ServerStatus ().withClusterName ("1" ).withServerName ("1" ).withState ("state1" ));
323+ domainStatus .addServer (new ServerStatus ().withClusterName ("1" ).withServerName ("2" ).withState ("state1" ));
324+ domainStatus .addServer (new ServerStatus ().withClusterName ("1" ).withServerName ("3" ).withState ("state1" ));
325+
326+ domainStatus .setServers (Arrays .asList (
327+ new ServerStatus ().withClusterName ("1" ).withServerName ("1" ).withState ("state1" ),
328+ new ServerStatus ().withClusterName ("1" ).withServerName ("2" ).withState ("state1" ),
329+ new ServerStatus ().withClusterName ("1" ).withServerName ("3" )
330+ ));
331+
332+ assertThat (getServer ("1" , "3" ).getState (), equalTo ("state1" ));
333+ }
334+
335+ private ServerStatus getServer (String clusterName , String serverName ) {
336+ return domainStatus .getServers ()
337+ .stream ()
338+ .filter (s -> Objects .equals (clusterName , s .getClusterName ()))
339+ .filter (s -> Objects .equals (serverName , s .getServerName ()))
340+ .findFirst ()
341+ .orElse (null );
342+ }
343+
281344 @ Test
282345 public void verifyThat_getServers_serverInExpectedOrdering () {
283346 ServerStatus cluster1Server1 = new ServerStatus ().withClusterName ("cluster-1" ).withServerName ("cluster1-server1" );
@@ -303,7 +366,7 @@ public void verifyThat_addClusters_clustersSortedInExpectedOrdering() {
303366
304367 domainStatus .addCluster (cluster10 ).addCluster (cluster1 ).addCluster (cluster2 );
305368
306- assertThat (domainStatus .clusters , contains (cluster1 , cluster2 , cluster10 ));
369+ assertThat (domainStatus .getClusters () , contains (cluster1 , cluster2 , cluster10 ));
307370 }
308371
309372 @ Test
@@ -314,9 +377,7 @@ public void verifyThat_setClusters_clustersSortedInExpectedOrdering() {
314377
315378 domainStatus .setClusters (Arrays .asList (cluster10 , cluster1 , cluster2 ));
316379
317- List <ClusterStatus > clusterStatuses = domainStatus .clusters ;
318-
319- assertThat (clusterStatuses , contains (cluster1 , cluster2 , cluster10 ));
380+ assertThat (domainStatus .getClusters (), contains (cluster1 , cluster2 , cluster10 ));
320381 }
321382
322383 @ Test
0 commit comments