Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@

<properties>
<java.version>1.8</java.version>
<junit-jupiter.version>5.7.0</junit-jupiter.version>
</properties>

<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
Expand All @@ -34,38 +37,80 @@
<scope>runtime</scope>
<optional>true</optional>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
</exclusion>
<exclusion>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</exclusion>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
<exclusion>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit-jupiter.version}</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit-jupiter.version}</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.mockito/mockito-junit-jupiter -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>3.5.15</version>
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/com.github.javafaker/javafaker -->
<dependency>
<groupId>com.github.javafaker</groupId>
<artifactId>javafaker</artifactId>
<version>1.0.2</version>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,30 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.birichani.code.restapi.service.VideoService;

import java.util.List;

@RequestMapping("/videos")
@RestController
public class VideoApiController {
private VideoRepository videoRepository;
private final VideoRepository videoRepository;
private final VideoService videoService;

@Autowired
public VideoApiController(VideoRepository videoRepository) {
public VideoApiController(VideoRepository videoRepository, VideoService videoService) {
this.videoRepository = videoRepository;
this.videoService = videoService;
}

@GetMapping("/Getvideos/{videoTopic}")
public String video(@PathVariable String videoTopic) {

switch(videoTopic) {
case"python":

return String.format("This is our python contents") ;
case "java":
return String.format("This is our java contents") ;
default:
return String.format("sorry,Incorrect input") ;

@GetMapping("/{videoTopic}")
public String getVideosByTopic(@PathVariable String videoTopic) {
return videoService.getVideosByTopic(videoTopic);
}

}
@GetMapping("/GetAll")
public List<Video> getAllvideos(){
@GetMapping()
public List<Video> getAllVideos(){
return videoRepository.findAll();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.birichani.code.restapi.constant;

/**
* @author - JoeSeff
* @created - 22/10/2020 10:07
*/
public class ErrorMessage {
public static final String VIDEO_TOPIC_SELECTION_MESSAGE = "Sorry. Incorrect input.";
}
13 changes: 13 additions & 0 deletions src/main/java/com/birichani/code/restapi/constant/InfoMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.birichani.code.restapi.constant;

import lombok.NoArgsConstructor;

/**
* @author - JoeSeff
* @created - 22/10/2020 10:05
*/
@NoArgsConstructor
public class InfoMessage {
public static final String PYTHON_RESPONSE_MESSAGE = "This is our python content.";
public static final String JAVA_RESPONSE_MESSAGE = "This is our java content.";
}
6 changes: 5 additions & 1 deletion src/main/java/com/birichani/code/restapi/model/Video.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.birichani.code.restapi.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

Expand All @@ -10,16 +11,19 @@

@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class Video {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long vid;
private String title;
private String description;
private String topic;

public Video(String title, String description) {
public Video(String title, String description, String topic) {
this.title = title;
this.description = description;
this.topic = topic;
}
}
67 changes: 67 additions & 0 deletions src/main/java/com/birichani/code/restapi/service/VideoService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.birichani.code.restapi.service;

import com.birichani.code.restapi.constant.ErrorMessage;
import com.birichani.code.restapi.constant.InfoMessage;
import com.birichani.code.restapi.model.Video;
import com.birichani.code.restapi.repository.VideoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* @author - JoeSeff
* @created - 22/10/2020 23:57
*/

@Service
public class VideoService {
private final VideoRepository videoRepository;

@Autowired
public VideoService(VideoRepository videoRepository) {
this.videoRepository = videoRepository;
}

public String getVideosByTopic(String videoTopic) {
String message;

switch (videoTopic.toLowerCase()) {
case "python":
message = InfoMessage.PYTHON_RESPONSE_MESSAGE;
break;
case "java":
message = InfoMessage.JAVA_RESPONSE_MESSAGE;
break;
default:
message = ErrorMessage.VIDEO_TOPIC_SELECTION_MESSAGE;
}

return message;
}

public List<Video> filterVideosByTopic(String topic) {
List<Video> videoList = videoRepository.findAll();

return videoList.stream()
.filter(Objects::nonNull)
.filter(video -> video.getTopic() != null)
.filter(video -> video.getTopic().equalsIgnoreCase(topic))
.collect(Collectors.toList());
}


// TODO: Implement me
public List<Video> filterVideosByTitle(String title) {
List<Video> videoList = videoRepository.findAll();

return videoList.stream()
.filter(Objects::nonNull)
.filter(video -> video.getTitle() != null)
.filter(video -> video.getTitle().toLowerCase().contains(title.toLowerCase()))
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void testThatRecordsAreInserted() {
assertNotNull(videoRepository);

for (int i = 0; i < 5; i++) {
videoRepository.save( new Video(faker.name().title(), faker.lorem().paragraph()) );
videoRepository.save( new Video(faker.name().title(), faker.lorem().paragraph(), faker.company().industry()) );
}

assertEquals(5, videoRepository.count());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.birichani.code.restapi.service;

import com.birichani.code.restapi.constant.InfoMessage;
import com.birichani.code.restapi.model.Video;
import com.birichani.code.restapi.repository.VideoRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Arrays;
import java.util.List;

import static org.assertj.core.api.BDDAssertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.BDDMockito.when;

/**
* @author - JoeSeff
* @created - 23/10/2020 00:08
*/
@ExtendWith(MockitoExtension.class)
class VideoServiceTest {

@Mock
private VideoRepository videoRepository;

@InjectMocks
private VideoService videoService;

List<Video> videoList;
Video goneIn60Seconds, transporter, whiteHouseDown, loveAtFirstHiccup;
Video blended, pyaarImpossible, iAmInLoveWithAChurchGirl, saw, houseOfWax;
Video exorcismOfEmilyRose, mrBones, funWithDickAndJane, weAreTheMillers, threeIdiots;

@BeforeEach
void setup() {
goneIn60Seconds = new Video(1L, "Gone in 60 Seconds", "None", "Thriller");
transporter = new Video(2L, "Transporter", "None", "Action");
whiteHouseDown = new Video(3L, "White House Down", "None", "Action");
loveAtFirstHiccup = new Video(4L, "Love at First Hiccup", "None", "Romance Comedy");
blended = new Video(5L, "Blended", "None", "Romance Comedy");
pyaarImpossible = new Video(6L, "Pyaar Impossible", "None", "Romance");
iAmInLoveWithAChurchGirl = new Video(7L, "I am In Love with A Church Girl", "None", "Romance");
saw = new Video(8L, "SAW", "None", "Horror");
houseOfWax = new Video(9L, "House of Wax", "None", "Horror");
exorcismOfEmilyRose = new Video(10L, "The Exorcism of Emily Rose", "None", "Horror");
mrBones = new Video(11L, "Mr Bones", "None", "Comedy");
funWithDickAndJane = new Video(12L, "Mr Bones", "None", "Comedy");
weAreTheMillers = new Video(13L, "We Are The Millers", "None", "Comedy");
threeIdiots = new Video(14L, "3 Idiots", "None", "Comedy");

videoList = Arrays.asList(goneIn60Seconds, transporter, whiteHouseDown, loveAtFirstHiccup,
blended, pyaarImpossible, iAmInLoveWithAChurchGirl, saw, houseOfWax, exorcismOfEmilyRose, mrBones,
funWithDickAndJane, weAreTheMillers, threeIdiots);
}

@Test
@DisplayName("Test That Correct Video Topic Is Returned")
void testThatCorrectVideoTopicIsReturned() {
String response = videoService.getVideosByTopic("Java");
assertEquals(InfoMessage.JAVA_RESPONSE_MESSAGE, response);
}

@Test
@DisplayName("Filter videos based on topic attribute")
void shouldFilterVideosByTopic() {
// when
when(videoRepository.findAll()).thenReturn(videoList);

// then
List<Video> filteredVideoList = videoService.filterVideosByTopic("Horror");
assertThat(filteredVideoList).containsExactlyInAnyOrder(saw, houseOfWax, exorcismOfEmilyRose);
}

@Test
@DisplayName("Filter videos based on title attribute")
void shouldFilterVideosByTitle() {
// when
when(videoRepository.findAll()).thenReturn(videoList);

List<Video> videoList = videoService.filterVideosByTitle("the");
assertThat(videoList).contains(exorcismOfEmilyRose);
}

}