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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
/.idea/
/.idea/
/target/
/build/
/.gradle/
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# springboot-java8
The project is made on spring boot. The project summarize the new features present in Java 8.
The project is made on Spring Boot 3.3 and Java 21. It summarizes the functional-programming features introduced in Java 8.
It contain list of harcoded topics list. You can call the apis's with POSTMAN to add,delete,update Topic list
In addition, it uses
1) Java 8 NIO methods
Expand Down
41 changes: 22 additions & 19 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.2.RELEASE")
}
plugins {
id 'java'
id 'eclipse'
id 'idea'
id 'org.springframework.boot' version '3.3.4'
id 'io.spring.dependency-management' version '1.1.6'
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'org.springframework'
version = '0.1.0'

java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}

bootJar {
baseName = 'gs-spring-boot'
version = '0.1.0'
archiveBaseName = 'gs-spring-boot'
}

repositories {
mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
testCompile("junit:junit")
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
runtimeOnly 'com.h2database:h2'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {
useJUnitPlatform()
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip
17 changes: 9 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,17 @@

<groupId>org.springframework</groupId>
<artifactId>gs-spring-boot</artifactId>
<packaging>pom</packaging>
<packaging>jar</packaging>
<version>0.1.0</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<version>3.3.4</version>
<relativePath/>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-properties-migrator</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
Expand All @@ -32,10 +28,15 @@
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<properties>
<java.version>1.8</java.version>
<java.version>21</java.version>
</properties>


Expand Down
28 changes: 15 additions & 13 deletions src/main/java/hello/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,26 @@
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class Application implements CommandLineRunner {

private static final Logger log = LoggerFactory.getLogger(Application.class);
private static final String QUOTE_URL = "http://gturnquist-quoters.cfapps.io/api/random";

public static void main(String[] args) {

ApplicationContext ctx = SpringApplication.run(Application.class, args);

System.out.println("Let's inspect the beans provided by Spring Boot:");

String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}

RestTemplate restTemplate = new RestTemplate();
Quote quote = restTemplate.getForObject("http://gturnquist-quoters.cfapps.io/api/random", Quote.class);
log.info(quote.toString());
}


Expand All @@ -49,9 +47,12 @@ public RestTemplate restTemplate(RestTemplateBuilder builder) {
@Bean
public CommandLineRunner run(RestTemplate restTemplate) throws Exception {
return args -> {
Quote quote = restTemplate.getForObject(
"http://gturnquist-quoters.cfapps.io/api/random", Quote.class);
log.info(quote.toString());
try {
Quote quote = restTemplate.getForObject(QUOTE_URL, Quote.class);
log.info(String.valueOf(quote));
} catch (RestClientException e) {
log.warn("Could not fetch quote from {}: {}", QUOTE_URL, e.getMessage());
}
};
}

Expand All @@ -63,8 +64,8 @@ public CommandLineRunner run(RestTemplate restTemplate) throws Exception {
public void run(String... args) throws Exception {
log.info("Creating tables");

jdbcTemplate.execute("DROP TABLE customers IF EXISTS");
jdbcTemplate.execute("CREATE TABLE customers(id SERIAL, first_name VARCHAR(255), last_name VARCHAR(255))");
jdbcTemplate.execute("DROP TABLE IF EXISTS customers");
jdbcTemplate.execute("CREATE TABLE customers(id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, first_name VARCHAR(255), last_name VARCHAR(255))");

// Split up the array of whole names into an array of first/last names
List<Object[]> splitUpNames = Arrays.asList("John Woo", "Jeff Dean", "Josh Bloch", "Josh Long")
Expand All @@ -80,8 +81,9 @@ public void run(String... args) throws Exception {

log.info("Querying for customer records where first_name = 'Josh':");
jdbcTemplate.query(
"SELECT id, first_name, last_name FROM customers WHERE first_name = ?", new Object[]{"Josh"},
(rs, rowNum) -> new Customer(rs.getLong("id"), rs.getString("first_name"), rs.getString("last_name"))
"SELECT id, first_name, last_name FROM customers WHERE first_name = ?",
(rs, rowNum) -> new Customer(rs.getLong("id"), rs.getString("first_name"), rs.getString("last_name")),
"Josh"
).forEach(customer -> log.info(customer.toString()));

}
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/hello/ApplicationTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package hello;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class ApplicationTests {

@Test
void contextLoads() {
}
}
60 changes: 60 additions & 0 deletions src/test/java/hello/controller/GreetingAndHelloControllerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package hello.controller;

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.web.servlet.MockMvc;

import static org.hamcrest.Matchers.containsString;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@SpringBootTest
@AutoConfigureMockMvc
class GreetingAndHelloControllerTest {

@Autowired
private MockMvc mockMvc;

@Test
void greetingDefaultsToWorld() throws Exception {
mockMvc.perform(get("/"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.content").value("Hello, World!"));
}

@Test
void greetingUsesNameParam() throws Exception {
mockMvc.perform(get("/").param("name", "Devin"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.content").value("Hello, Devin!"));
}

@Test
void datetimeEndpoint() throws Exception {
mockMvc.perform(get("/datetime"))
.andExpect(status().isOk())
.andExpect(content().string(containsString("Greetings from Spring Boot!")))
.andExpect(content().string(containsString("Time in California:")));
}

@Test
void stringOperationEndpoint() throws Exception {
mockMvc.perform(get("/topic/string/operation"))
.andExpect(status().isOk())
.andExpect(content().string(containsString("spring:java:javascript")))
.andExpect(content().string(containsString("java:javascript")))
.andExpect(content().string(containsString("[spring]")));
}

@Test
void fileOperationEndpoint() throws Exception {
mockMvc.perform(get("/topic/file/operation"))
.andExpect(status().isOk())
.andExpect(content().string(containsString("Read \"temp.txt\"")))
.andExpect(content().string(containsString(" Hello, this, is, Rehman")));
}
}
81 changes: 81 additions & 0 deletions src/test/java/hello/controller/TopicControllerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package hello.controller;

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.web.servlet.MockMvc;

import static org.hamcrest.Matchers.hasSize;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
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.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@SpringBootTest
@AutoConfigureMockMvc
class TopicControllerTest {

@Autowired
private MockMvc mockMvc;

@Test
void getAllTopicsReturnsSeededTopics() throws Exception {
mockMvc.perform(get("/topic"))
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(3)))
.andExpect(jsonPath("$[0].id").value("spring"));
}

@Test
void getTopicById() throws Exception {
mockMvc.perform(get("/topic/java"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.subjectName").value("Core Java"));
}

@Test
void addUpdateAndDeleteTopic() throws Exception {
mockMvc.perform(post("/topic")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"id\":\"kotlin\",\"subjectName\":\"Kotlin\",\"subjectDescription\":\"Kotlin Desc\"}"))
.andExpect(status().isOk());

mockMvc.perform(get("/topic/kotlin"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.subjectName").value("Kotlin"));

mockMvc.perform(put("/topic/kotlin")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"id\":\"kotlin\",\"subjectName\":\"Kotlin 2\",\"subjectDescription\":\"Updated\"}"))
.andExpect(status().isOk());

mockMvc.perform(get("/topic/kotlin"))
.andExpect(jsonPath("$.subjectName").value("Kotlin 2"));

mockMvc.perform(delete("/topic/kotlin"))
.andExpect(status().isOk());

mockMvc.perform(get("/topic"))
.andExpect(jsonPath("$", hasSize(3)));
}

@Test
void filterByMinimumIdLength() throws Exception {
mockMvc.perform(get("/topic/minimum/length/5"))
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(2)));
}

@Test
void sortTopicsById() throws Exception {
mockMvc.perform(get("/topic/sort"))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].id").value("java"))
.andExpect(jsonPath("$[1].id").value("javascript"))
.andExpect(jsonPath("$[2].id").value("spring"));
}
}
Loading