Skip to content

Commit

Permalink
BE-#97: Update test for map widget
Browse files Browse the repository at this point in the history
  • Loading branch information
Drumber committed Apr 25, 2024
1 parent b3aa648 commit 71a7fc9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public interface MapWidgetMapper {

@Mapping(target = "id", ignore = true)
@Mapping(target = "creationDate", ignore = true)
@Mapping(target = "locations", ignore = true)
MapWidget mapToMapWidget(MapWidgetCreateCommand createCommand);

@Mapping(target = "id", ignore = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import lombok.Getter;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Builder(toBuilder = true)
Expand All @@ -15,7 +16,8 @@
public class MapWidget extends Widget {
private final String id;
private final LocalDateTime creationDate;
private List<Location> locations;
@Builder.Default
private List<Location> locations = new ArrayList<>();

@Override
public WidgetType getWidgetType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.dhbw.get2gether.backend.exceptions.OperationNotAllowedException;
import com.dhbw.get2gether.backend.utils.WithMockGuestUser;
import com.dhbw.get2gether.backend.utils.WithMockOAuth2User;
import com.dhbw.get2gether.backend.widget.model.map.Location;
import com.dhbw.get2gether.backend.widget.model.map.LocationAddCommand;
import com.dhbw.get2gether.backend.widget.model.map.MapWidget;
import com.dhbw.get2gether.backend.widget.model.map.MapWidgetCreateCommand;
Expand Down Expand Up @@ -62,7 +63,7 @@ void shouldCreateWidget() {
// then
assertThat(returnedEvent).isNotNull();
assertThat(returnedEvent.getWidgets()).hasSize(1);
assertThat(returnedEvent.getWidgets().get(0)).isInstanceOf(MapWidget.class);
assertThat(returnedEvent.getWidgets().get(0)).isInstanceOf(MapWidget.class).hasNoNullFieldsOrProperties();
}

@Test
Expand Down Expand Up @@ -111,11 +112,11 @@ void shouldAddLocation() {
AuthenticatedPrincipal principal = (AuthenticatedPrincipal)
SecurityContextHolder.getContext().getAuthentication().getPrincipal();
MapWidget widget = MapWidget.builder()
.id("123")
.id("wi-123")
.locations(new ArrayList<>())
.build();
Event event = Event.builder()
.id("123")
.id("ev-123")
.widgets(new ArrayList<>(List.of(widget)))
.build();
LocationAddCommand addCommand = LocationAddCommand.builder()
Expand Down Expand Up @@ -154,11 +155,11 @@ void shouldNotAddLocationIfUserIsGuest() {
AuthenticatedPrincipal principal = (AuthenticatedPrincipal)
SecurityContextHolder.getContext().getAuthentication().getPrincipal();
MapWidget widget = MapWidget.builder()
.id("123")
.id("wi-123")
.locations(new ArrayList<>())
.build();
Event event = Event.builder()
.id("123")
.id("ev-123")
.widgets(new ArrayList<>(List.of(widget)))
.build();
LocationAddCommand addCommand = LocationAddCommand.builder()
Expand All @@ -176,4 +177,44 @@ void shouldNotAddLocationIfUserIsGuest() {
assertThatThrownBy(() -> mapWidgetService.addLocation(principal, event.getId(), widget.getId(), addCommand))
.isInstanceOf(AccessDeniedException.class);
}

@Test
@WithMockOAuth2User
void shouldRemoveLocation() {
// given
AuthenticatedPrincipal principal = (AuthenticatedPrincipal)
SecurityContextHolder.getContext().getAuthentication().getPrincipal();
Location location = Location.builder()
.id("lo-123")
.name("Test")
.placeId("place-123")
.address("address-123")
.latitude(12.34)
.longitude(56.78)
.build();
MapWidget widget = MapWidget.builder()
.id("wi-123")
.locations(new ArrayList<>(List.of(location)))
.build();
Event event = Event.builder()
.id("ev-123")
.widgets(new ArrayList<>(List.of(widget)))
.build();

when(eventService.getSingleEvent(any(), eq(event.getId()))).thenReturn(event);
when(eventService.updateEventWidgets(any(), eq(event.getId()), any()))
.thenAnswer(i -> event.toBuilder()
.widgets(
i.getArgument(2, EventWidgetUpdateCommand.class).getWidgets()
)
.build()
);

// when
MapWidget returnedWidget = mapWidgetService.removeLocation(principal, event.getId(), widget.getId(), location.getId());

// then
assertThat(returnedWidget).isNotNull();
assertThat(returnedWidget.getLocations()).isEmpty();
}
}

0 comments on commit 71a7fc9

Please sign in to comment.