From e77c5f1c6851d9f4a085d2fd8cbe7892acd40b54 Mon Sep 17 00:00:00 2001 From: Shyam Vishwakarma Date: Mon, 21 Oct 2024 10:50:22 +0530 Subject: [PATCH 1/2] add integration tests for GET v1/skills, add test DB configuration --- .../resources/application-test.properties | 8 ++ .../skills/GetAllSkillsIntegrationTest.java | 90 +++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 skill-tree/src/main/resources/application-test.properties create mode 100644 skill-tree/src/test/java/com/RDS/skilltree/skills/GetAllSkillsIntegrationTest.java diff --git a/skill-tree/src/main/resources/application-test.properties b/skill-tree/src/main/resources/application-test.properties new file mode 100644 index 00000000..29948ca7 --- /dev/null +++ b/skill-tree/src/main/resources/application-test.properties @@ -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 diff --git a/skill-tree/src/test/java/com/RDS/skilltree/skills/GetAllSkillsIntegrationTest.java b/skill-tree/src/test/java/com/RDS/skilltree/skills/GetAllSkillsIntegrationTest.java new file mode 100644 index 00000000..fe2f2de0 --- /dev/null +++ b/skill-tree/src/test/java/com/RDS/skilltree/skills/GetAllSkillsIntegrationTest.java @@ -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()); + } +} From f85ac8bdb092b4351f4027b03b2af96c77b67cc5 Mon Sep 17 00:00:00 2001 From: Shyam Vishwakarma Date: Mon, 21 Oct 2024 23:25:51 +0530 Subject: [PATCH 2/2] fix format violation --- .../resources/application-test.properties | 6 +-- .../skills/GetAllSkillsIntegrationTest.java | 47 ++++++++++--------- 2 files changed, 29 insertions(+), 24 deletions(-) diff --git a/skill-tree/src/main/resources/application-test.properties b/skill-tree/src/main/resources/application-test.properties index 29948ca7..02c453f3 100644 --- a/skill-tree/src/main/resources/application-test.properties +++ b/skill-tree/src/main/resources/application-test.properties @@ -1,8 +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.url=jdbc:mysql://${MYSQL_HOST:localhost}:${MYSQL_PORT:3306}/skilltreetestdb +spring.datasource.username=${MYSQL_DB_USERNAME} +spring.datasource.password=${MYSQL_DB_PASSWORD} spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.hibernate.ddl-auto=create-drop spring.jpa.show-sql=true diff --git a/skill-tree/src/test/java/com/RDS/skilltree/skills/GetAllSkillsIntegrationTest.java b/skill-tree/src/test/java/com/RDS/skilltree/skills/GetAllSkillsIntegrationTest.java index fe2f2de0..b16ac200 100644 --- a/skill-tree/src/test/java/com/RDS/skilltree/skills/GetAllSkillsIntegrationTest.java +++ b/skill-tree/src/test/java/com/RDS/skilltree/skills/GetAllSkillsIntegrationTest.java @@ -1,44 +1,43 @@ package com.RDS.skilltree.skills; +import com.RDS.skilltree.enums.SkillTypeEnum; 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 java.util.Arrays; +import org.junit.jupiter.api.BeforeEach; 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.http.MediaType; 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 SkillService skillService; - @Autowired - private SkillRepository skillRepository; + @Autowired private SkillRepository skillRepository; - @Autowired - private MockMvc mockMvc; + @Autowired private MockMvc mockMvc; private Cookie authCookie; @BeforeEach public void setUp() { - authCookie = new Cookie("rds-session-v2-development", "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJzOXpRVW00WGJWRXo3eHpSa2FadiIsInJvbGUiOiJzdXBlcl91c2VyIiwiaWF0IjoxNzI4NjY0NjA2LCJleHAiOjE3MzEyNTY2MDZ9.EyOFKrVcbleuTjUGic3GzOzYRDoLU4IShyoboe0MHlvWFOAfU2pchpXLE4NcyvdGUZ_tvoUecHd4kUkR8MkhxnkRNU3HE7N-1c1tFeYXZL0KfScJE9YzDXAl113Hx3eZVvYbhNjNUttbDlH4s_kR6YABC3sdbLGKEiLfmp9VeAs"); + authCookie = + new Cookie( + "rds-session-v2-development", + "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJzOXpRVW00WGJWRXo3eHpSa2FadiIsInJvbGUiOiJzdXBlcl91c2VyIiwiaWF0IjoxNzI4NjY0NjA2LCJleHAiOjE3MzEyNTY2MDZ9.EyOFKrVcbleuTjUGic3GzOzYRDoLU4IShyoboe0MHlvWFOAfU2pchpXLE4NcyvdGUZ_tvoUecHd4kUkR8MkhxnkRNU3HE7N-1c1tFeYXZL0KfScJE9YzDXAl113Hx3eZVvYbhNjNUttbDlH4s_kR6YABC3sdbLGKEiLfmp9VeAs"); skillRepository.deleteAll(); Skill skill1 = new Skill(); @@ -58,9 +57,11 @@ public void setUp() { @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)) + 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")) @@ -72,9 +73,11 @@ public void getAllSkillsHappyFlow() throws Exception { public void noSkillsAvailable_shouldReturnEmptyList() throws Exception { skillRepository.deleteAll(); - mockMvc.perform(MockMvcRequestBuilders.get("/v1/skills") - .cookie(authCookie) - .accept(MediaType.APPLICATION_JSON)) + mockMvc + .perform( + MockMvcRequestBuilders.get("/v1/skills") + .cookie(authCookie) + .accept(MediaType.APPLICATION_JSON)) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.jsonPath("$").isEmpty()); } @@ -82,9 +85,11 @@ public void noSkillsAvailable_shouldReturnEmptyList() throws Exception { @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)) + mockMvc + .perform( + MockMvcRequestBuilders.get("/v1/skills") + .cookie(new Cookie("cookie1", "eyJhbGciOiJSUz.eyJhbGciOiJSUz.EyJhbGciOiJSUz")) + .accept(MediaType.APPLICATION_JSON)) .andExpect(MockMvcResultMatchers.status().isUnauthorized()); } }