11/*
2- * Copyright 2014-2018 the original author or authors.
2+ * Copyright 2014-2019 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1818
1919import de .codecentric .boot .admin .server .domain .events .InstanceDeregisteredEvent ;
2020import de .codecentric .boot .admin .server .domain .events .InstanceEvent ;
21+ import de .codecentric .boot .admin .server .domain .events .InstanceInfoChangedEvent ;
2122import de .codecentric .boot .admin .server .domain .values .BuildVersion ;
2223import de .codecentric .boot .admin .server .domain .values .Endpoints ;
2324import de .codecentric .boot .admin .server .domain .values .Info ;
2425import de .codecentric .boot .admin .server .domain .values .InstanceId ;
2526import de .codecentric .boot .admin .server .domain .values .Registration ;
2627import de .codecentric .boot .admin .server .domain .values .StatusInfo ;
2728
29+ import java .util .List ;
30+ import java .util .stream .Collectors ;
2831import org .junit .Test ;
2932
3033import static java .util .Collections .singletonMap ;
@@ -145,8 +148,9 @@ public void should_throw_when_applied_wrong_event() {
145148 ))).isInstanceOf (IllegalArgumentException .class )
146149 .hasMessage ("'event' must refer the same instance" );
147150
148- assertThatThrownBy (() -> instance .apply (new InstanceDeregisteredEvent (InstanceId .of ("id" ), 1L ))).isInstanceOf (
149- IllegalArgumentException .class ).hasMessage ("Event 1 doesn't match exptected version 0" );
151+ assertThatThrownBy (() -> instance .apply (new InstanceDeregisteredEvent (InstanceId .of ("id" ), 1L ))
152+ .apply (new InstanceDeregisteredEvent (InstanceId .of ("id" ), 1L ))).isInstanceOf (
153+ IllegalArgumentException .class ).hasMessage ("Event 1 must be greater or equal to 2" );
150154 }
151155
152156 @ Test
@@ -186,8 +190,7 @@ public void should_extract_tags() {
186190 assertThat (instance .getTags ().getValues ()).containsExactly (entry ("environment" , "test" ), entry ("region" , "EU" ));
187191
188192 instance = instance .withInfo (Info .from (singletonMap ("tags" , singletonMap ("region" , "US-East" ))));
189- assertThat (instance .getTags ().getValues ()).containsExactly (
190- entry ("environment" , "test" ),
193+ assertThat (instance .getTags ().getValues ()).containsExactly (entry ("environment" , "test" ),
191194 entry ("region" , "US-East" )
192195 );
193196
@@ -197,4 +200,25 @@ public void should_extract_tags() {
197200 instance = instance .register (registration .toBuilder ().clearMetadata ().build ());
198201 assertThat (instance .getTags ().getValues ()).isEmpty ();
199202 }
203+
204+ @ Test
205+ public void shoud_rebuild_instance () {
206+ Instance instance = Instance .create (InstanceId .of ("id" ))
207+ .register (Registration .create ("test" , "http://test" ).build ())
208+ .withInfo (Info .from (singletonMap ("info" , "remove" )))
209+ .withInfo (Info .from (singletonMap ("info" , "test2" )));
210+
211+ List <InstanceEvent > relevantEvents = instance .getUnsavedEvents ()
212+ .stream ()
213+ .filter (e -> !(e instanceof InstanceInfoChangedEvent &&
214+ ((InstanceInfoChangedEvent ) e ).getInfo ()
215+ .getValues ()
216+ .get ("info" )
217+ .equals ("remove" )))
218+ .collect (Collectors .toList ());
219+
220+ Instance rebuilt = Instance .create (InstanceId .of ("id" )).apply (relevantEvents );
221+
222+ assertThat (rebuilt ).isEqualTo (instance );
223+ }
200224}
0 commit comments