Skip to content

Commit

Permalink
Merge pull request #6 from malekch-15/unittestback1
Browse files Browse the repository at this point in the history
Unittestback1
  • Loading branch information
malekch-15 authored Nov 15, 2024
2 parents b759784 + ef4714f commit edcbc59
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 7 deletions.
1 change: 1 addition & 0 deletions backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo.spring3x</artifactId>
Expand Down
3 changes: 2 additions & 1 deletion backend/src/main/java/reacp/repository/RestaurantRepo.java
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> {
}
13 changes: 7 additions & 6 deletions backend/src/main/java/reacp/services/RestaurantService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reacp.model.RestaurantModel;
import reacp.repository.RestaurantRepo;
Expand Down Expand Up @@ -35,8 +36,8 @@ public RestaurantModel addRestaurant(RestaurantModel restaurantModel) {
restaurantModel.description(),
restaurantModel.status()
);
restaurantRepo.save(newRestaurantModel);
return restaurantRepo.findById(newRestaurantModel.id()).orElseThrow();

return restaurantRepo.save(newRestaurantModel);
}


Expand Down Expand Up @@ -67,8 +68,8 @@ public RestaurantModel updateRestaurantWithPut(String id, RestaurantModel restau
restaurantModel.description(),
restaurantModel.status()
);
restaurantRepo.save(newRestaurantModel);
return restaurantRepo.findById(newRestaurantModel.id()).orElseThrow();

return restaurantRepo.save(newRestaurantModel);
} else {
throw new NoSuchElementException("No Restaurant found with the Put-Id:"+id);
}
Expand All @@ -84,8 +85,8 @@ public RestaurantModel updateRestaurantWithPatch(String id, RestaurantModel rest
restaurantModel.description() != null ? restaurantModel.description() : existingRestaurant.description(),
restaurantModel.status() != null ? restaurantModel.status() : existingRestaurant.status()
);
restaurantRepo.save(updatedRestaurant);
return updatedRestaurant;

return restaurantRepo.save(updatedRestaurant);
}

public void deleteRestaurant(String id) {
Expand Down
101 changes: 101 additions & 0 deletions backend/src/test/java/reacp/services/RestaurantServiceTest.java
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);

}
}

0 comments on commit edcbc59

Please sign in to comment.