-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
259 additions
and
154 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
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 |
---|---|---|
|
@@ -44,4 +44,6 @@ public String toString() { | |
", images=" + images + | ||
'}'; | ||
} | ||
|
||
|
||
} |
File renamed without changes.
77 changes: 77 additions & 0 deletions
77
src/test/java/org/example/app/Controller/AdControllerTest.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,77 @@ | ||
package org.example.app.Controller; | ||
|
||
import org.example.app.Daos.AdDao; | ||
import org.example.app.Models.Dtos.AdSearchResponse; | ||
import org.example.app.Models.Entities.Ad; | ||
import org.example.app.Models.Entities.User; | ||
import org.example.app.Services.AdsFetching.AdFetchingFilter; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
import org.springframework.boot.test.web.server.LocalServerPort; | ||
import org.springframework.test.context.bean.override.mockito.MockitoBean; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
@ExtendWith(MockitoExtension.class) | ||
@AutoConfigureMockMvc | ||
public class AdControllerTest { | ||
@LocalServerPort | ||
private int port; | ||
|
||
@MockitoBean | ||
private AdDao adDao; | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
|
||
@Test | ||
public void testGetsAllAdsByCategoryElectronics() throws Exception { | ||
UUID categoryId = UUID.randomUUID(); | ||
String url = "http://localhost:" + port + "/ads/filtered-by-category?categoryId="+ categoryId + "&offset=0&limit=10"; | ||
|
||
User user = new User(); | ||
user.setFirstName("user1"); | ||
user.setId(UUID.randomUUID()); | ||
|
||
Ad ad1 = new Ad(); | ||
ad1.setTitle("Title 1"); | ||
ad1.setUser(user); | ||
ad1.setImages(List.of()); | ||
|
||
Ad ad2 = new Ad(); | ||
ad2.setTitle("Title 2"); | ||
ad2.setUser(user); | ||
ad2.setImages(List.of()); | ||
|
||
List<Ad> mockAds = List.of(ad1, ad2); | ||
|
||
AdSearchResponse adSearchResponse = new AdSearchResponse(mockAds, 2); | ||
System.out.println(adSearchResponse); | ||
Mockito.when(adDao.getAdsByCategory(Mockito.any(AdFetchingFilter.class))) | ||
.thenReturn(adSearchResponse); | ||
|
||
|
||
mockMvc.perform(get(url)) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.ads.length()").value(2)) | ||
.andExpect(jsonPath("$.ads[0].title").value("Title 1")) | ||
.andExpect(jsonPath("$.ads[1].title").value("Title 2")); | ||
|
||
} | ||
|
||
|
||
} |
36 changes: 36 additions & 0 deletions
36
src/test/java/org/example/app/Controller/AuthControllerTest.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,36 @@ | ||
package org.example.app.Controller; | ||
|
||
|
||
import org.example.app.Daos.UserDao; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.web.server.LocalServerPort; | ||
import org.springframework.test.context.DynamicPropertyRegistry; | ||
import org.springframework.test.context.DynamicPropertySource; | ||
import org.springframework.test.context.bean.override.mockito.MockitoBean; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.testcontainers.containers.PostgreSQLContainer; | ||
|
||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
@ExtendWith(MockitoExtension.class) | ||
@AutoConfigureMockMvc | ||
public class AuthControllerTest { | ||
@LocalServerPort | ||
private int port; | ||
|
||
@MockitoBean | ||
private UserDao userDao; | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
|
||
|
||
|
||
} |
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 |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
import org.example.app.Models.Entities.Chat; | ||
import org.example.app.Models.Entities.User; | ||
import org.example.app.Services.AdsFetching.AdFetchingFilter; | ||
import org.example.app.TestContainerConfig; | ||
import org.junit.ClassRule; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
|
@@ -25,39 +26,15 @@ | |
import java.time.LocalDateTime; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@Transactional | ||
@Rollback | ||
|
||
@DataJpaTest | ||
@Import({UserDao.class, AdDao.class}) | ||
@Import({UserDao.class, AdDao.class, TestContainerConfig.class}) | ||
public class AdDaoTest { | ||
|
||
static PostgreSQLContainer<?> postgres = new PostgreSQLContainer( | ||
"postgres:16-alpine" | ||
); | ||
|
||
|
||
@BeforeAll | ||
static void beforeAll() { | ||
postgres.start(); | ||
} | ||
|
||
@AfterAll | ||
static void afterAll() { | ||
postgres.stop(); | ||
} | ||
|
||
|
||
@DynamicPropertySource | ||
static void configureProperties(DynamicPropertyRegistry registry) { | ||
registry.add("spring.datasource.url", postgres::getJdbcUrl); | ||
registry.add("spring.datasource.username", postgres::getUsername); | ||
registry.add("spring.datasource.password", postgres::getPassword); | ||
} | ||
|
||
|
||
@Autowired | ||
private AdDao adDao; | ||
|
||
|
@@ -67,96 +44,30 @@ static void configureProperties(DynamicPropertyRegistry registry) { | |
@Autowired | ||
private TestEntityManager testEntityManager; | ||
|
||
private Category electronicsCategory; | ||
|
||
|
||
@BeforeEach | ||
void setUp() { | ||
User userOne = new User(); | ||
userOne.setFirstName("Martin"); | ||
userOne.setLastName("Smith"); | ||
userOne.setPassword("password"); | ||
userOne.setUsername("martin11"); | ||
userOne.setEmail("[email protected]"); | ||
testEntityManager.persist(userOne); | ||
|
||
electronicsCategory = new Category(null, "Electronics"); | ||
testEntityManager.persist(electronicsCategory); | ||
|
||
Category laptopsCategory = new Category(electronicsCategory, "Laptops"); | ||
testEntityManager.persist(laptopsCategory); | ||
|
||
Category booksCategory = new Category(null, "Books"); | ||
testEntityManager.persist(booksCategory); | ||
|
||
Ad ad1 = new Ad( | ||
"Book1", | ||
10, | ||
"book description", | ||
"new", | ||
LocalDateTime.now(), | ||
booksCategory, | ||
LocalDateTime.now(), | ||
userOne, | ||
null, | ||
false | ||
); | ||
testEntityManager.persist(ad1); | ||
|
||
Ad ad2 = new Ad( | ||
"Gaming laptop 1", | ||
500, | ||
"Gaming laptop description", | ||
"new", | ||
LocalDateTime.now(), | ||
laptopsCategory, | ||
LocalDateTime.now(), | ||
userOne, | ||
List.of(), | ||
false | ||
); | ||
testEntityManager.persist(ad2); | ||
|
||
Ad ad3= new Ad( | ||
"Gaming laptop 2", | ||
600, | ||
"Gaming laptop description 2", | ||
"new", | ||
LocalDateTime.now(), | ||
laptopsCategory, | ||
LocalDateTime.now(), | ||
userOne, | ||
List.of(), | ||
false | ||
); | ||
testEntityManager.persist(ad3); | ||
|
||
Ad ad4= new Ad( | ||
"Printer", | ||
600, | ||
"Printer description", | ||
"new", | ||
LocalDateTime.now(), | ||
electronicsCategory, | ||
LocalDateTime.now(), | ||
userOne, | ||
List.of(), | ||
false | ||
); | ||
testEntityManager.persist(ad4); | ||
|
||
} | ||
|
||
|
||
@Test | ||
|
||
void findsAllAdsInsideElectronicsCategory() { | ||
|
||
AdFetchingFilter filter = new AdFetchingFilter.Builder() | ||
.category(electronicsCategory.getId()).limit(10).offset(0).build(); | ||
System.out.println(electronicsCategory.toString()); | ||
.category(UUID.fromString("aefb83ad-4c9f-4c6a-b599-d8f67f4a1676")).limit(10).offset(0).build(); | ||
|
||
AdSearchResponse ads = adDao.getAdsByCategory(filter); | ||
|
||
assertThat(ads.getAds()).extracting(adDto -> adDto.getTitle()) | ||
.containsExactlyInAnyOrder("Printer", "Gaming laptop 2", "Gaming laptop 1"); | ||
} | ||
|
||
|
||
|
||
@Test | ||
void findsLaptopsWhenSearchQueryIsLaptop() { | ||
AdFetchingFilter filter = new AdFetchingFilter.Builder() | ||
.query("Laptop").limit(10).offset(0).build(); | ||
|
||
AdSearchResponse ads = adDao.getSearchedAds(filter); | ||
|
||
assertThat(ads.getAds()).extracting(adDto -> adDto.getTitle()) | ||
.containsExactlyInAnyOrder("Gaming laptop 2", "Gaming laptop 1"); | ||
} | ||
} |
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
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.