felkesz testek

This commit is contained in:
2026-03-13 19:08:49 +01:00
parent 32512d0dd6
commit 7d365f5a5d
5 changed files with 106 additions and 5 deletions

13
pom.xml
View File

@@ -81,6 +81,11 @@
<version>26.1.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
@@ -100,6 +105,14 @@
<cleanDisabled>false</cleanDisabled>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>9</source>
<target>9</target>
</configuration>
</plugin>
</plugins>
</build>

View File

@@ -37,8 +37,15 @@ public class VideoController {
}
@PostMapping(path = "/videos")
public Video saveNewVideo(@RequestBody SaveNewVideoRequest request){
return videoService.saveNewVideo(request);
//https://www.baeldung.com/exception-handling-for-rest-with-spring
//global exception handling
public ResponseEntity saveNewVideo(@RequestBody SaveNewVideoRequest request){
try {
return ResponseEntity.ok(videoService.saveNewVideo(request));
} catch (RuntimeException e) {
return ResponseEntity.badRequest().build();
}
}
@PostMapping(path = "/playlists")

View File

@@ -1,7 +1,7 @@
spring.application.name=video_downloader
spring.datasource.url = jdbc:postgresql://127.0.0.1:5432/videos
spring.datasource.username = postgres
spring.datasource.password = 5995
spring.datasource.url = jdbc:postgresql://postgres.local:5432/video
spring.datasource.username = istvan
spring.datasource.password = .b0wU?#DXKRqfkgF,oB}da:naL4e^XjbL~}@!aNK,+n>CpKYPoDZ:%BH^W!W4U%^nrso-qMjbUw)5HVCD#2Dh=8%5]~fLsRsXW?*
spring.jpa.hibernate.ddl-auto = none
logging.level.org.flywaydb=DEBUG
logging.level.org.springframework.boot.autoconfigure.flyway=DEBUG

View File

@@ -0,0 +1,70 @@
package com.example.video_downloader;
import com.example.video_downloader.entity.StatusEnum;
import com.example.video_downloader.entity.Video;
import com.example.video_downloader.repositories.VideoRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.util.Assert;
import org.springframework.web.context.WebApplicationContext;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import java.util.List;
@SpringBootTest
public class VideoDownloaderApplicationTest {
@Autowired
VideoRepository videoRepository;
private MockMvc mockMvc;
@BeforeEach
public void setUp(WebApplicationContext webApplicationContext) {
this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
.build();
}
@Test
void contextLoads() {
Video v = new Video();
v.setStatus(StatusEnum.NEW);
videoRepository.save(v);
List<Video> findByStatusList = videoRepository.findByStatus(StatusEnum.NEW);
Assert.isTrue(findByStatusList.size() == 10,"Anyad");
}
@Test
void insertListIntoVideoTest() throws Exception {
mockMvc.perform(post("/v2/videos").contentType(MediaType.APPLICATION_JSON)
.content("{\"url\":\"https://www.youtube.com/watch?v=swiGWilMqeQ&list=RDswiGWilMqeQ\"}")).andDo(MockMvcResultHandlers.print())
.andExpect(status().isBadRequest())
.andReturn();;
}
@Test
void insertVideoTest() throws Exception {
MvcResult mvcResult = mockMvc.perform(post("/v2/videos").contentType(MediaType.APPLICATION_JSON)
.content("{\"url\":\"https://www.youtube.com/watch?v=pOq0pJ6QLhE\"}")).andDo(print())
.andExpect(status().isOk())
.andReturn();
System.out.println(mvcResult.getResponse().getContentAsString());
}
}

View File

@@ -0,0 +1,11 @@
spring.application.name=video_downloader
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto = update
logging.level.org.flywaydb=DEBUG
logging.level.org.springframework.boot.autoconfigure.flyway=DEBUG
spring.flyway.enabled=false
spring.flyway.baseline-on-migrate=true