-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add integration tests for GET v1/skills, add test DB configuration
- Loading branch information
1 parent
a7c39ec
commit e77c5f1
Showing
2 changed files
with
98 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
cookieName=rds-session-v2-development | ||
|
||
spring.datasource.url=jdbc:mysql://localhost:3306/skilltreetestdb | ||
spring.datasource.username=root | ||
spring.datasource.password=root | ||
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver | ||
spring.jpa.hibernate.ddl-auto=create-drop | ||
spring.jpa.show-sql=true |
90 changes: 90 additions & 0 deletions
90
skill-tree/src/test/java/com/RDS/skilltree/skills/GetAllSkillsIntegrationTest.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,90 @@ | ||
package com.RDS.skilltree.skills; | ||
|
||
import com.RDS.skilltree.models.Skill; | ||
import com.RDS.skilltree.repositories.SkillRepository; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.springframework.http.MediaType; | ||
import com.RDS.skilltree.enums.SkillTypeEnum; | ||
import com.RDS.skilltree.services.SkillService; | ||
import jakarta.servlet.http.Cookie; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
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.test.context.ActiveProfiles; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; | ||
import org.springframework.test.web.servlet.result.MockMvcResultHandlers; | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; | ||
|
||
import java.util.Arrays; | ||
|
||
@SpringBootTest | ||
@AutoConfigureMockMvc | ||
@ActiveProfiles("test") | ||
public class GetAllSkillsIntegrationTest { | ||
|
||
@Autowired | ||
private SkillService skillService; | ||
|
||
@Autowired | ||
private SkillRepository skillRepository; | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
private Cookie authCookie; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
authCookie = new Cookie("rds-session-v2-development", "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJzOXpRVW00WGJWRXo3eHpSa2FadiIsInJvbGUiOiJzdXBlcl91c2VyIiwiaWF0IjoxNzI4NjY0NjA2LCJleHAiOjE3MzEyNTY2MDZ9.EyOFKrVcbleuTjUGic3GzOzYRDoLU4IShyoboe0MHlvWFOAfU2pchpXLE4NcyvdGUZ_tvoUecHd4kUkR8MkhxnkRNU3HE7N-1c1tFeYXZL0KfScJE9YzDXAl113Hx3eZVvYbhNjNUttbDlH4s_kR6YABC3sdbLGKEiLfmp9VeAs"); | ||
|
||
skillRepository.deleteAll(); | ||
Skill skill1 = new Skill(); | ||
skill1.setName("Java"); | ||
skill1.setType(SkillTypeEnum.ATOMIC); | ||
skill1.setCreatedBy("s9zQUm4XbVEz7xzRkaZv"); | ||
|
||
Skill skill2 = new Skill(); | ||
skill2.setName("Springboot"); | ||
skill2.setType(SkillTypeEnum.ATOMIC); | ||
skill2.setCreatedBy("s9zQUm4XbVEz7xzRkaZv"); | ||
|
||
skillRepository.saveAll(Arrays.asList(skill1, skill2)); | ||
} | ||
|
||
@Test | ||
@DisplayName("happy flow - returns all skills that are in db") | ||
public void getAllSkillsHappyFlow() throws Exception { | ||
|
||
mockMvc.perform(MockMvcRequestBuilders.get("/v1/skills") | ||
.cookie(authCookie) | ||
.accept(MediaType.APPLICATION_JSON)) | ||
.andExpect(MockMvcResultMatchers.status().isOk()) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$[0].name").value("Java")) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$[1].name").value("Springboot")) | ||
.andDo(MockMvcResultHandlers.print()); | ||
} | ||
|
||
@Test | ||
@DisplayName("if no skills available, return empty list") | ||
public void noSkillsAvailable_shouldReturnEmptyList() throws Exception { | ||
skillRepository.deleteAll(); | ||
|
||
mockMvc.perform(MockMvcRequestBuilders.get("/v1/skills") | ||
.cookie(authCookie) | ||
.accept(MediaType.APPLICATION_JSON)) | ||
.andExpect(MockMvcResultMatchers.status().isOk()) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$").isEmpty()); | ||
} | ||
|
||
@Test | ||
@DisplayName("if invalid cookie, return 401") | ||
public void ifInvalidCoolie_returnUnauthorized() throws Exception { | ||
mockMvc.perform(MockMvcRequestBuilders.get("/v1/skills") | ||
.cookie(new Cookie("cookie1", "eyJhbGciOiJSUz.eyJhbGciOiJSUz.EyJhbGciOiJSUz")) | ||
.accept(MediaType.APPLICATION_JSON)) | ||
.andExpect(MockMvcResultMatchers.status().isUnauthorized()); | ||
} | ||
} |