-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from malekch-15/unittestback1
Unittestback1
- Loading branch information
Showing
4 changed files
with
111 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
package reacp.repository; | ||
|
||
import org.springframework.data.mongodb.repository.MongoRepository; | ||
import org.springframework.stereotype.Repository; | ||
import reacp.model.RestaurantModel; | ||
|
||
@Repository | ||
public interface RestaurantRepo extends MongoRepository<RestaurantModel, String> { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
backend/src/test/java/reacp/services/RestaurantServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package reacp.services; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import reacp.model.RestaurantModel; | ||
import reacp.repository.RestaurantRepo; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.Mockito.*; | ||
import static reacp.model.WishlistStatus.NOT_ON_WISHLIST; | ||
import static reacp.model.WishlistStatus.ON_WISHLIST; | ||
|
||
@SpringBootTest | ||
class RestaurantServiceTest { | ||
IdService idService= mock(IdService.class); | ||
RestaurantRepo restaurantRepo = mock(RestaurantRepo.class); | ||
RestaurantService restaurantService = new RestaurantService(idService, restaurantRepo); | ||
//RESTAURANT DATA | ||
RestaurantModel restaurantModel =new RestaurantModel("1","Fischereihafen Restaurant", | ||
"Hamburg","Seafood","Ein gehobenes Restaurant mit Hafenblick, ",ON_WISHLIST); | ||
RestaurantModel restaurantModel2 =new RestaurantModel("2","Fischereihafen Restaurant", | ||
"Hamburg","Seafood","Ein gehobenes Restaurant mit Hafenblick, ",ON_WISHLIST); | ||
List<RestaurantModel> restaurants= List.of(restaurantModel,restaurantModel2); | ||
@Test | ||
void getAllRestaurants() { | ||
//Given | ||
|
||
|
||
when(restaurantRepo.findAll()).thenReturn(restaurants); | ||
//WHEN | ||
List<RestaurantModel> expected=restaurantService.getAllRestaurants(); | ||
//THEN | ||
assertEquals(expected,restaurants); | ||
System.out.println(expected+"and"+ restaurants); | ||
|
||
} | ||
|
||
@Test | ||
void getRestaurantById() { | ||
//Given | ||
when(restaurantRepo.findById("1")).thenReturn(Optional.of(restaurantModel)); | ||
//when | ||
RestaurantModel expected=restaurantService.getRestaurantById("1"); | ||
assertEquals(expected,restaurantModel); | ||
System.out.println(expected+"and"+ restaurants); | ||
|
||
} | ||
|
||
@Test | ||
void addRestaurant() { | ||
//GIVEN | ||
RestaurantModel restaurantModel3=new RestaurantModel("3","Fischereihafen Restaurant", | ||
"Köln","Seafood","Ein gehobenes Restaurant mit Hafenblick, ",ON_WISHLIST); | ||
|
||
RestaurantModel newRestaurant= new RestaurantModel("3",restaurantModel3.name(),restaurantModel3.city() | ||
,restaurantModel3.category(),restaurantModel3.description(),restaurantModel3.status()); | ||
when(idService.generateRandomId()).thenReturn("3"); | ||
when(restaurantRepo.save(any(RestaurantModel.class))).thenReturn(newRestaurant); | ||
//WHEN | ||
RestaurantModel expected=restaurantService.addRestaurant(restaurantModel3); | ||
|
||
|
||
//THEN | ||
assertEquals(newRestaurant, expected); | ||
assertEquals(restaurantModel3.name(),expected.name()); | ||
assertEquals(restaurantModel3.city(), expected.city()); | ||
assertEquals(restaurantModel3.category(), expected.category()); | ||
assertEquals(restaurantModel3.description(), expected.description()); | ||
assertEquals(restaurantModel3.status(), expected.status()); | ||
System.out.println(expected+"and"+ restaurants); | ||
} | ||
|
||
@Test | ||
void updateRestaurantWithPut() { | ||
RestaurantModel exist=new RestaurantModel("3","Fischereihafen Restaurant", | ||
"Köln","Seafood","Ein gehobenes Restaurant mit Hafenblick, ",NOT_ON_WISHLIST); | ||
|
||
RestaurantModel updated = new RestaurantModel("3",exist.name(),exist.city() | ||
,exist.category(),exist.description(),ON_WISHLIST); | ||
when(restaurantRepo.existsById("3")).thenReturn(true); | ||
when(idService.generateRandomId()).thenReturn("3"); | ||
when(restaurantRepo.save(any(RestaurantModel.class))).thenReturn(updated); | ||
|
||
RestaurantModel expected=restaurantService.updateRestaurantWithPut("3",updated); | ||
assertEquals(updated, expected); | ||
} | ||
|
||
|
||
@Test | ||
void deleteRestaurant() { | ||
String restaurantId = "1"; | ||
|
||
restaurantService.deleteRestaurant(restaurantId); | ||
|
||
verify(restaurantRepo, times(1)).deleteById(restaurantId); | ||
verifyNoMoreInteractions(restaurantRepo); | ||
|
||
} | ||
} |