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
7 changes: 7 additions & 0 deletions ReadMe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# REST-API APP
This is a project to showcase the rest API using Spring boot. It allows you to add, view, update and remove film titles.
## spring Boot
The use of spring data jpa, hibernate as one of the technologies used.
### MYSQL
database used to store data.
#### maven.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@
</exclusion>
</exclusions>
</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
@@ -1,39 +1,46 @@
package com.birichani.code.restapi.api;

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.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
@RequestMapping("/videos")

@RestController
public class VideoApiController {
private VideoRepository videoRepository;

private final VideoRepository videoRepository;
@Autowired
public VideoApiController(VideoRepository videoRepository) {
this.videoRepository = videoRepository;
}

@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") ;

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

switch (videoTopic) {
case "python":
message = InfoMessage.PYTHON_RESPONSE_MESSAGE;
break;
case "java":
message = InfoMessage.JAVA_RESPONSE_MESSAGE;
break;
default:
return String.format("sorry,Incorrect input") ;



message = ErrorMessage.VIDEO_TOPIC_SELECTION_MESSAGE;
}


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

public class ErrorMessage {
public static final String VIDEO_TOPIC_SELECTION_MESSAGE = "Sorry. Incorrect input.";
}
12 changes: 12 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,12 @@
package com.birichani.code.restapi.constant;


import lombok.NoArgsConstructor;

@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.";

}
11 changes: 11 additions & 0 deletions src/main/java/com/birichani/code/restapi/model/Video.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
package com.birichani.code.restapi.model;

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

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

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

public Video(String title, String description, String link) {
this.title = title;
this.description = description;
this.link = link;
}
}
4 changes: 2 additions & 2 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
server.port=8084

# Datasource properties
spring.datasource.url=jdbc:mysql://localhost/dynamiccontents?useSSL=false
spring.datasource.url=jdbc:mysql://localhost:3306/dynamiccontents?allowPublicKeyRetrieval=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.password=Sb34106209
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

# JPA properties
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.birichani.code.restapi;

import com.birichani.code.restapi.api.VideoApiController;
import com.birichani.code.restapi.constant.InfoMessage;
import com.birichani.code.restapi.repository.VideoRepository;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;

import static org.junit.jupiter.api.Assertions.assertEquals;
@DisplayName("Video API Controller Unit Test")
public class VideoControllerUnitTest {

@Mock
VideoRepository videoRepository;
VideoApiController controller = new VideoApiController(videoRepository);

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

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.birichani.code.restapi.repository;

import com.birichani.code.restapi.model.Video;
import com.github.javafaker.Faker;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import java.util.Locale;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
* @author - JoeSeff
* @created - 16/10/2020 02:02
*/

@ExtendWith(SpringExtension.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@TestPropertySource(properties = {
"spring.jpa.hibernate.ddl-auto=create-drop"
})
class VideoRepositoryTest {

@Autowired private VideoRepository videoRepository;
private final Faker faker = new Faker(Locale.ENGLISH);
private String link;

@Test
void testThatRecordsAreInserted() {
assertNotNull(videoRepository);

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

assertEquals(5, videoRepository.count());
}
}
12 changes: 12 additions & 0 deletions src/test/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
server.port=8084

# Datasource properties
spring.datasource.url=jdbc:mysql://localhost:3306/video_db?useSSL=false
spring.datasource.username=test
spring.datasource.password=test123*
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

# JPA properties
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect