Skip to content

Commit 712b83b

Browse files
committed
added test to cover new changes.
Signed-off-by: makbn <mehdi74akbarian@gmail.com>
1 parent 5ffd156 commit 712b83b

16 files changed

+2342
-0
lines changed
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
package io.github.makbn.jlmap;
2+
3+
import com.google.gson.Gson;
4+
import io.github.makbn.jlmap.listener.JLAction;
5+
import io.github.makbn.jlmap.listener.OnJLActionListener;
6+
import io.github.makbn.jlmap.listener.event.Event;
7+
import io.github.makbn.jlmap.listener.event.LayerEvent;
8+
import io.github.makbn.jlmap.listener.event.ZoomEvent;
9+
import io.github.makbn.jlmap.model.JLBounds;
10+
import io.github.makbn.jlmap.model.JLLatLng;
11+
import io.github.makbn.jlmap.model.JLMarker;
12+
import org.junit.jupiter.api.BeforeEach;
13+
import org.junit.jupiter.api.Test;
14+
import org.junit.jupiter.api.extension.ExtendWith;
15+
import org.mockito.ArgumentCaptor;
16+
import org.mockito.Mock;
17+
import org.mockito.junit.jupiter.MockitoExtension;
18+
19+
import static org.assertj.core.api.Assertions.*;
20+
import static org.mockito.ArgumentMatchers.any;
21+
import static org.mockito.Mockito.*;
22+
23+
@SuppressWarnings("unchecked")
24+
@ExtendWith(MockitoExtension.class)
25+
class JLMapEventHandlerTest {
26+
27+
private static final Gson GSON = new Gson();
28+
29+
private static final String BOUNDS_JSON = GSON.toJson(JLBounds.builder()
30+
.southWest(new JLLatLng(51.0, 13.0))
31+
.northEast(new JLLatLng(52.0, 14.0))
32+
.build());
33+
34+
@Mock
35+
JLMap<?> map;
36+
37+
JLMapEventHandler eventHandler;
38+
39+
@BeforeEach
40+
void setUp() {
41+
eventHandler = new JLMapEventHandler();
42+
}
43+
44+
@Test
45+
void addJLObject_singleObject_shouldBeDispatchable() {
46+
OnJLActionListener<JLMarker> markerListener = mock(OnJLActionListener.class);
47+
JLMarker marker = JLMarker.builder().id("marker-1").latLng(new JLLatLng(0, 0)).build();
48+
marker.setOnActionListener(markerListener);
49+
50+
eventHandler.addJLObject("marker-1", marker);
51+
52+
String latLng = "{\"lat\":51.5,\"lng\":-0.1}";
53+
eventHandler.functionCalled(map, "add", "jlmarker", "marker-1", null, latLng, BOUNDS_JSON);
54+
55+
ArgumentCaptor<Event> captor = ArgumentCaptor.forClass(Event.class);
56+
verify(markerListener).onAction(any(), captor.capture());
57+
58+
assertThat(captor.getValue()).isInstanceOf(LayerEvent.class);
59+
LayerEvent event = (LayerEvent) captor.getValue();
60+
assertThat(event.action()).isEqualTo(JLAction.ADD);
61+
}
62+
63+
@Test
64+
void addJLObject_twoObjectsOfSameType_bothShouldBeStored() {
65+
OnJLActionListener<JLMarker> listener1 = mock(OnJLActionListener.class);
66+
OnJLActionListener<JLMarker> listener2 = mock(OnJLActionListener.class);
67+
68+
JLMarker marker1 = JLMarker.builder().id("m-1").latLng(new JLLatLng(10, 20)).build();
69+
marker1.setOnActionListener(listener1);
70+
71+
JLMarker marker2 = JLMarker.builder().id("m-2").latLng(new JLLatLng(30, 40)).build();
72+
marker2.setOnActionListener(listener2);
73+
74+
eventHandler.addJLObject("m-1", marker1);
75+
eventHandler.addJLObject("m-2", marker2);
76+
77+
String latLng = "{\"lat\":0.0,\"lng\":0.0}";
78+
79+
eventHandler.functionCalled(map, "add", "jlmarker", "m-1", null, latLng, BOUNDS_JSON);
80+
verify(listener1).onAction(any(), any());
81+
verifyNoInteractions(listener2);
82+
83+
reset(listener1);
84+
eventHandler.functionCalled(map, "add", "jlmarker", "m-2", null, latLng, BOUNDS_JSON);
85+
verify(listener2).onAction(any(), any());
86+
verifyNoInteractions(listener1);
87+
}
88+
89+
@Test
90+
void remove_afterAdd_objectShouldNoLongerBeDispatched() {
91+
OnJLActionListener<JLMarker> markerListener = mock(OnJLActionListener.class);
92+
JLMarker marker = JLMarker.builder().id("marker-r").latLng(new JLLatLng(0, 0)).build();
93+
marker.setOnActionListener(markerListener);
94+
95+
eventHandler.addJLObject("marker-r", marker);
96+
eventHandler.remove(JLMarker.class, "marker-r");
97+
98+
String latLng = "{\"lat\":0.0,\"lng\":0.0}";
99+
eventHandler.functionCalled(map, "add", "jlmarker", "marker-r", null, latLng, BOUNDS_JSON);
100+
101+
verifyNoInteractions(markerListener);
102+
}
103+
104+
@Test
105+
void remove_onNonExistentClass_shouldNotThrow() {
106+
assertThatCode(() -> eventHandler.remove(JLMarker.class, "nonexistent"))
107+
.doesNotThrowAnyException();
108+
}
109+
110+
@Test
111+
void functionCalled_mapLevelZoomEvent_shouldDispatchToMapListener() {
112+
OnJLActionListener mapListener = mock(OnJLActionListener.class);
113+
when(map.getOnActionListener()).thenReturn(mapListener);
114+
115+
eventHandler.functionCalled(map, "zoom", "map", "main_map", "12.5", null, BOUNDS_JSON);
116+
117+
ArgumentCaptor<Event> captor = ArgumentCaptor.forClass(Event.class);
118+
verify(mapListener).onAction(any(), captor.capture());
119+
120+
assertThat(captor.getValue()).isInstanceOf(ZoomEvent.class);
121+
ZoomEvent event = (ZoomEvent) captor.getValue();
122+
assertThat(event.action()).isEqualTo(JLAction.ZOOM);
123+
assertThat(event.zoomLevel()).isCloseTo(12.5, within(0.001));
124+
}
125+
126+
@Test
127+
void functionCalled_objectLevelEvent_shouldDispatchToObjectListener() {
128+
OnJLActionListener<JLMarker> markerListener = mock(OnJLActionListener.class);
129+
JLMarker marker = JLMarker.builder().id("obj-1").latLng(new JLLatLng(48.8566, 2.3522)).build();
130+
marker.setOnActionListener(markerListener);
131+
132+
eventHandler.addJLObject("obj-1", marker);
133+
134+
String latLng = "{\"lat\":48.8566,\"lng\":2.3522}";
135+
String bounds = GSON.toJson(JLBounds.builder()
136+
.southWest(new JLLatLng(48.0, 2.0))
137+
.northEast(new JLLatLng(49.0, 3.0))
138+
.build());
139+
140+
eventHandler.functionCalled(map, "remove", "jlmarker", "obj-1", null, latLng, bounds);
141+
142+
ArgumentCaptor<Event> captor = ArgumentCaptor.forClass(Event.class);
143+
verify(markerListener).onAction(any(), captor.capture());
144+
145+
assertThat(captor.getValue()).isInstanceOf(LayerEvent.class);
146+
LayerEvent event = (LayerEvent) captor.getValue();
147+
assertThat(event.action()).isEqualTo(JLAction.REMOVE);
148+
assertThat(event.latLng()).isNotEmpty();
149+
}
150+
151+
@Test
152+
void functionCalled_unknownType_shouldNotThrow() {
153+
assertThatCode(() ->
154+
eventHandler.functionCalled(map, "zoom", "unknowntype", "some-uuid", "10", null, BOUNDS_JSON))
155+
.doesNotThrowAnyException();
156+
}
157+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package io.github.makbn.jlmap.geojson;
2+
3+
import io.github.makbn.jlmap.exception.JLGeoJsonParserException;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.assertj.core.api.Assertions.assertThat;
7+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
8+
9+
class JLGeoJsonContentTest {
10+
11+
private final JLGeoJsonContent content = new JLGeoJsonContent();
12+
13+
@Test
14+
void load_nullInput_throwsException() {
15+
assertThatThrownBy(() -> content.load(null))
16+
.isInstanceOf(JLGeoJsonParserException.class);
17+
}
18+
19+
@Test
20+
void load_emptyString_throwsException() {
21+
assertThatThrownBy(() -> content.load(""))
22+
.isInstanceOf(JLGeoJsonParserException.class);
23+
}
24+
25+
@Test
26+
void load_validJson_returnsSameString() throws JLGeoJsonParserException {
27+
var json = "{\"type\": \"Point\", \"coordinates\": [0, 0]}";
28+
29+
var result = content.load(json);
30+
31+
assertThat(result).isEqualTo(json);
32+
}
33+
34+
@Test
35+
void load_invalidJson_throwsException() {
36+
assertThatThrownBy(() -> content.load("not json {{{"))
37+
.isInstanceOf(JLGeoJsonParserException.class);
38+
}
39+
40+
@Test
41+
void load_validFeatureCollection_returnsContent() throws JLGeoJsonParserException {
42+
var geoJson = """
43+
{
44+
"type": "FeatureCollection",
45+
"features": [
46+
{
47+
"type": "Feature",
48+
"geometry": {
49+
"type": "Point",
50+
"coordinates": [102.0, 0.5]
51+
},
52+
"properties": {
53+
"name": "test"
54+
}
55+
}
56+
]
57+
}
58+
""";
59+
60+
var result = content.load(geoJson);
61+
62+
assertThat(result).isEqualTo(geoJson);
63+
}
64+
}

0 commit comments

Comments
 (0)