Skip to content

Commit

Permalink
Issue jmprathab#257 improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
DDmit04 committed Mar 12, 2021
1 parent b73fbf1 commit 651b44d
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 12 deletions.
26 changes: 19 additions & 7 deletions service/src/main/java/com/myhome/domain/Amenity.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import javax.persistence.ManyToOne;
import javax.persistence.NamedAttributeNode;
import javax.persistence.NamedEntityGraph;
import javax.persistence.NamedEntityGraphs;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -35,12 +37,22 @@
@Getter
@Setter
@With
@NamedEntityGraph(
name = "Amenity.community",
attributeNodes = {
@NamedAttributeNode("community"),
}
)
@NamedEntityGraphs({
@NamedEntityGraph(
name = "Amenity.community",
attributeNodes = {
@NamedAttributeNode("community"),
}
),
@NamedEntityGraph(
name = "Amenity.community_house",
attributeNodes = {
@NamedAttributeNode("community"),
@NamedAttributeNode("communityHouse")
}
)
})

public class Amenity extends BaseEntity {
@Column(nullable = false, unique = true)
private String amenityId;
Expand All @@ -52,6 +64,6 @@ public class Amenity extends BaseEntity {
private BigDecimal price;
@ManyToOne(fetch = FetchType.LAZY)
private Community community;
@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
private CommunityHouse communityHouse;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ public interface AmenityBookingService {

boolean deleteBooking(String bookingId);

boolean removeAllAmenityBookings(String amenityID);
void removeAllAmenityBookings(String amenityID);
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ public boolean deleteBooking(String bookingId) {
}

@Override
public boolean removeAllAmenityBookings(String amenityId) {
public void removeAllAmenityBookings(String amenityId) {
HashSet<AmenityBookingItem> bookings = bookingItemRepository.findAllByAmenity_AmenityId(amenityId);
bookingItemRepository.deleteAll(bookings);
return false;
if(!bookings.isEmpty()) {
bookingItemRepository.deleteAll(bookings);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.util.HashSet;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertFalse;
Expand All @@ -21,6 +22,7 @@
public class AmenityBookingSDJpaServiceTest {

private static final String TEST_BOOKING_ID = "test-booking-id";
private static final String TEST_AMENITY_ID = "test-amenity-id";

@Mock
private AmenityBookingItemRepository bookingItemRepository;
Expand Down Expand Up @@ -65,9 +67,45 @@ void shouldNotDeleteBookingItemIfNotExists() {
verify(bookingItemRepository, never()).delete(any());
}

@Test
void shouldRemoveAllAmenityBookings() {
// given
HashSet<AmenityBookingItem> testAmenityBookings = new HashSet<AmenityBookingItem>() {{
add(getTestBookingItem("test-amenity-1"));
add(getTestBookingItem("test-amenity-2"));
}};
given(bookingItemRepository.findAllByAmenity_AmenityId(TEST_AMENITY_ID))
.willReturn(testAmenityBookings);

// when
amenityBookingService.removeAllAmenityBookings(TEST_AMENITY_ID);

// then
verify(bookingItemRepository).findAllByAmenity_AmenityId(TEST_AMENITY_ID);
verify(bookingItemRepository).deleteAll(testAmenityBookings);
}

@Test
void ShouldNotRemoveAllAmenityBookingsIfAmenityNotExists() {
// given
given(bookingItemRepository.findAllByAmenity_AmenityId(TEST_AMENITY_ID))
.willReturn(new HashSet<>());

// when
amenityBookingService.removeAllAmenityBookings(TEST_AMENITY_ID);

// then
verify(bookingItemRepository).findAllByAmenity_AmenityId(TEST_AMENITY_ID);
verify(bookingItemRepository, never()).deleteAll(any());
}

private AmenityBookingItem getTestBookingItem() {
return getTestBookingItem(TEST_BOOKING_ID);
}

private AmenityBookingItem getTestBookingItem(String bookingID) {
return new AmenityBookingItem()
.withAmenityBookingItemId(TEST_BOOKING_ID);
.withAmenityBookingItemId(bookingID);
}

}

0 comments on commit 651b44d

Please sign in to comment.