|
| 1 | +package searchUseCase; |
| 2 | + |
| 3 | +import dataAccess.*; |
| 4 | +import login_and_signup_use_case.UserLoginResponseModel; |
| 5 | +import search_use_case.*; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +import java.io.IOException; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.HashMap; |
| 11 | + |
| 12 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 13 | + |
| 14 | +public class SearchUseCaseTest { |
| 15 | + SearchController controller; |
| 16 | + UserLoginResponseModel common_user = new UserLoginResponseModel("common_user", |
| 17 | + false, new HashMap<>()); |
| 18 | + UserLoginResponseModel admin = new UserLoginResponseModel("admin", |
| 19 | + true, new HashMap<>()); |
| 20 | + |
| 21 | + SearchRequestModel setup(String search_input, ArrayList<String> tags, |
| 22 | + UserLoginResponseModel user) throws IOException { |
| 23 | + IFlashcardDataAccess flashcardGateway = new FlashcardDataAccess( |
| 24 | + "src/test/java/searchUseCase/testData/Flashcards.csv"); |
| 25 | + IFlashcardSetDataAccess flashcardSetGateway = new FlashcardSetDataAccess( |
| 26 | + "src/test/java/searchUseCase/testData/FlashcardSets.csv"); |
| 27 | + IUserDataAccess userGateway = new CommonUserDataAccess( |
| 28 | + "src/test/java/searchUseCase/testData/Users.csv"); |
| 29 | + DBGateway gateway = new DBGateway(flashcardGateway, flashcardSetGateway, userGateway); |
| 30 | + |
| 31 | + SearchOutputBoundary search_presenter = new SearchPresenter(); |
| 32 | + SearchInputBoundary search_interactor = new SearchInteractor(search_presenter, gateway); |
| 33 | + this.controller = new SearchController(search_interactor); |
| 34 | + |
| 35 | + return new SearchRequestModel(search_input, tags, user); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + void testSearchAll() throws IOException { |
| 40 | + SearchRequestModel searchRequestModel = this.setup("GET_ALL", new ArrayList<>(), common_user); |
| 41 | + SearchResponseModel responseModel = controller.create(searchRequestModel); |
| 42 | + |
| 43 | + assertEquals(4, responseModel.getResult_set().size()); |
| 44 | + assertEquals("test4", responseModel.getResult_set().get(0).getTitle()); |
| 45 | + assertEquals("test1", responseModel.getResult_set().get(3).getTitle()); |
| 46 | + |
| 47 | + } |
| 48 | + // Test Search results on all 3 tags |
| 49 | + |
| 50 | + @Test |
| 51 | + void testSearchTitle() throws IOException{ |
| 52 | + ArrayList<String> tags = new ArrayList<>(); |
| 53 | + tags.add("Title"); |
| 54 | + |
| 55 | + SearchRequestModel searchRequestModel = this.setup("test1", tags, common_user); |
| 56 | + SearchResponseModel responseModel = controller.create(searchRequestModel); |
| 57 | + |
| 58 | + assertEquals(1, responseModel.getResult_set().size()); |
| 59 | + assertEquals("test1", responseModel.getResult_set().get(0).getTitle()); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void testSearchDescription() throws IOException{ |
| 64 | + ArrayList<String> tags = new ArrayList<>(); |
| 65 | + tags.add("Description"); |
| 66 | + |
| 67 | + SearchRequestModel searchRequestModel = this.setup("d2", tags, common_user); |
| 68 | + SearchResponseModel responseModel = controller.create(searchRequestModel); |
| 69 | + |
| 70 | + assertEquals(1, responseModel.getResult_set().size()); |
| 71 | + assertEquals("d2", responseModel.getResult_set().get(0).getDescription()); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void testSearchOwner() throws IOException{ |
| 76 | + ArrayList<String> tags = new ArrayList<>(); |
| 77 | + tags.add("Owner"); |
| 78 | + |
| 79 | + SearchRequestModel searchRequestModel = this.setup("testUser3", tags, common_user); |
| 80 | + SearchResponseModel responseModel = controller.create(searchRequestModel); |
| 81 | + |
| 82 | + assertEquals(1, responseModel.getResult_set().size()); |
| 83 | + assertEquals("testUser3", responseModel.getResult_set().get(0).getOwnerUsername()); |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | + // Test Admin search ignores privates |
| 88 | + @Test |
| 89 | + void testAdminSearch() throws IOException{ |
| 90 | + SearchRequestModel searchRequestModel = this.setup("GET_ALL", new ArrayList<>(), admin); |
| 91 | + SearchResponseModel responseModel = controller.create(searchRequestModel); |
| 92 | + |
| 93 | + assertEquals(6, responseModel.getResult_set().size()); |
| 94 | + assertEquals("test6", responseModel.getResult_set().get(2).getTitle()); |
| 95 | + assertEquals("testUser1", responseModel.getResult_set().get(5).getOwnerUsername()); |
| 96 | + } |
| 97 | +} |
0 commit comments