diff --git a/pom.xml b/pom.xml
index 63f5cbd..2a47c9c 100644
--- a/pom.xml
+++ b/pom.xml
@@ -5,7 +5,7 @@
org.springframework
gs-spring-boot
- pom
+ jar
0.1.0
@@ -32,6 +32,29 @@
com.h2database
h2
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
+
+ net.bytebuddy
+ byte-buddy
+ 1.12.23
+ test
+
+
+ net.bytebuddy
+ byte-buddy-agent
+ 1.12.23
+ test
+
+
+ org.mockito
+ mockito-core
+ 4.11.0
+ test
+
@@ -45,6 +68,21 @@
org.springframework.boot
spring-boot-maven-plugin
+
+ org.jacoco
+ jacoco-maven-plugin
+ 0.8.5
+
+
+ prepare-agent
+
+
+ report
+ test
+ report
+
+
+
diff --git a/src/test/java/hello/ApplicationTest.java b/src/test/java/hello/ApplicationTest.java
new file mode 100644
index 0000000..c77aca6
--- /dev/null
+++ b/src/test/java/hello/ApplicationTest.java
@@ -0,0 +1,31 @@
+package hello;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.boot.CommandLineRunner;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.test.context.TestConfiguration;
+import org.springframework.boot.test.mock.mockito.MockBean;
+import org.springframework.context.annotation.Bean;
+import org.springframework.test.context.junit4.SpringRunner;
+import org.springframework.web.client.RestTemplate;
+
+@RunWith(SpringRunner.class)
+@SpringBootTest(properties = "spring.main.allow-bean-definition-overriding=true")
+public class ApplicationTest {
+
+ @MockBean
+ private RestTemplate restTemplate;
+
+ @TestConfiguration
+ static class TestConfig {
+ @Bean
+ public CommandLineRunner run(RestTemplate restTemplate) {
+ return args -> {};
+ }
+ }
+
+ @Test
+ public void contextLoads() {
+ }
+}
diff --git a/src/test/java/hello/controller/GreetingControllerTest.java b/src/test/java/hello/controller/GreetingControllerTest.java
new file mode 100644
index 0000000..f9fb87c
--- /dev/null
+++ b/src/test/java/hello/controller/GreetingControllerTest.java
@@ -0,0 +1,57 @@
+package hello.controller;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.junit.Before;
+import org.junit.Test;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.MvcResult;
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertTrue;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
+
+public class GreetingControllerTest {
+
+ private MockMvc mockMvc;
+
+ private final ObjectMapper objectMapper = new ObjectMapper();
+
+ @Before
+ public void setUp() {
+ mockMvc = MockMvcBuilders.standaloneSetup(new GreetingController()).build();
+ }
+
+ @Test
+ public void greeting_defaultName_returnsHelloWorld() throws Exception {
+ mockMvc.perform(get("/"))
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("$.content", is("Hello, World!")))
+ .andExpect(jsonPath("$.id").isNumber());
+ }
+
+ @Test
+ public void greeting_withName_returnsHelloName() throws Exception {
+ mockMvc.perform(get("/?name=Devin"))
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("$.content", is("Hello, Devin!")))
+ .andExpect(jsonPath("$.id").isNumber());
+ }
+
+ @Test
+ public void greeting_calledTwice_idIncrements() throws Exception {
+ MvcResult first = mockMvc.perform(get("/"))
+ .andExpect(status().isOk())
+ .andReturn();
+
+ MvcResult second = mockMvc.perform(get("/"))
+ .andExpect(status().isOk())
+ .andReturn();
+
+ long firstId = objectMapper.readTree(first.getResponse().getContentAsString()).get("id").asLong();
+ long secondId = objectMapper.readTree(second.getResponse().getContentAsString()).get("id").asLong();
+
+ assertTrue("ID should increment", secondId > firstId);
+ }
+}
diff --git a/src/test/java/hello/controller/HelloControllerTest.java b/src/test/java/hello/controller/HelloControllerTest.java
new file mode 100644
index 0000000..ab25a9f
--- /dev/null
+++ b/src/test/java/hello/controller/HelloControllerTest.java
@@ -0,0 +1,70 @@
+package hello.controller;
+
+import hello.service.TopicService;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+
+import static org.hamcrest.Matchers.containsString;
+import static org.mockito.Mockito.when;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
+
+public class HelloControllerTest {
+
+ private MockMvc mockMvc;
+
+ @Mock
+ private TopicService topicService;
+
+ @InjectMocks
+ private HelloController helloController;
+
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+ mockMvc = MockMvcBuilders.standaloneSetup(helloController).build();
+ }
+
+ @Test
+ public void datetime_returns200WithExpectedSubstrings() throws Exception {
+ mockMvc.perform(get("/datetime"))
+ .andExpect(status().isOk())
+ .andExpect(content().string(containsString("Datetime now is")))
+ .andExpect(content().string(containsString("leap year")));
+ }
+
+ @Test
+ public void showStringOperation_returns200WithAssembledResponse() throws Exception {
+ when(topicService.returnAllTopicIDWithStringSlicing()).thenReturn("spring:java:javascript");
+ when(topicService.makeDistinctAndSortCharacters("spring:java:javascript")).thenReturn(":acgijnoprstvw");
+ when(topicService.splitAllIdWithColonSelectIDWithJavaKeywordThenSortThenJoin("spring:java:javascript"))
+ .thenReturn("java:javascript");
+ when(topicService.findIdHavingCharacter()).thenReturn("[spring, javascript]");
+
+ mockMvc.perform(get("/topic/string/operation"))
+ .andExpect(status().isOk())
+ .andExpect(content().string(containsString("spring:java:javascript")))
+ .andExpect(content().string(containsString(":acgijnoprstvw")))
+ .andExpect(content().string(containsString("java:javascript")))
+ .andExpect(content().string(containsString("[spring, javascript]")));
+ }
+
+ @Test
+ public void showFileOperation_returns200WithAssembledResponse() throws Exception {
+ when(topicService.findAllFilesInPathAndSort()).thenReturn("build.gradle; pom.xml");
+ when(topicService.findParticularFileInPathAndSort()).thenReturn("gradle; gradlew");
+ when(topicService.findParticularFileInPathAndSortWithWalkFunction()).thenReturn("gradle; gradlew");
+ when(topicService.readFileWithStreamFunction()).thenReturn(" Hello, World");
+
+ mockMvc.perform(get("/topic/file/operation"))
+ .andExpect(status().isOk())
+ .andExpect(content().string(containsString("build.gradle; pom.xml")))
+ .andExpect(content().string(containsString("gradle; gradlew")))
+ .andExpect(content().string(containsString(" Hello, World")));
+ }
+}
diff --git a/src/test/java/hello/controller/TopicControllerTest.java b/src/test/java/hello/controller/TopicControllerTest.java
new file mode 100644
index 0000000..95d1140
--- /dev/null
+++ b/src/test/java/hello/controller/TopicControllerTest.java
@@ -0,0 +1,136 @@
+package hello.controller;
+
+import hello.model.Topic;
+import hello.service.TopicService;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.springframework.http.MediaType;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+
+import java.util.Arrays;
+import java.util.List;
+
+import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.Matchers.is;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
+
+public class TopicControllerTest {
+
+ private MockMvc mockMvc;
+
+ @Mock
+ private TopicService topicService;
+
+ @InjectMocks
+ private TopicController topicController;
+
+ private final ObjectMapper objectMapper = new ObjectMapper();
+
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+ mockMvc = MockMvcBuilders.standaloneSetup(topicController).build();
+ }
+
+ @Test
+ public void getAllTopics_returns200WithJsonArray() throws Exception {
+ List topics = Arrays.asList(
+ new Topic("spring", "Spring Framework", "Spring Description"),
+ new Topic("java", "Core Java", "Java Description")
+ );
+ when(topicService.getAllTopics()).thenReturn(topics);
+
+ mockMvc.perform(get("/topic"))
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("$", hasSize(2)))
+ .andExpect(jsonPath("$[0].id", is("spring")))
+ .andExpect(jsonPath("$[1].id", is("java")));
+ }
+
+ @Test
+ public void getTopicWithID_returns200WithJsonObject() throws Exception {
+ Topic topic = new Topic("spring", "Spring Framework", "Spring Description");
+ when(topicService.getTopicWithId("spring")).thenReturn(topic);
+
+ mockMvc.perform(get("/topic/spring"))
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("$.id", is("spring")))
+ .andExpect(jsonPath("$.subjectName", is("Spring Framework")))
+ .andExpect(jsonPath("$.subjectDescription", is("Spring Description")));
+ }
+
+ @Test
+ public void addTopic_callsServiceAddTopic() throws Exception {
+ Topic topic = new Topic("python", "Python", "Python Desc");
+
+ mockMvc.perform(post("/topic")
+ .contentType(MediaType.APPLICATION_JSON)
+ .content(objectMapper.writeValueAsString(topic)))
+ .andExpect(status().isOk());
+
+ verify(topicService, times(1)).addTopic(any(Topic.class));
+ }
+
+ @Test
+ public void updateTopic_callsServiceUpdateTopic() throws Exception {
+ Topic topic = new Topic("spring", "Spring Boot", "Updated Desc");
+
+ mockMvc.perform(put("/topic/spring")
+ .contentType(MediaType.APPLICATION_JSON)
+ .content(objectMapper.writeValueAsString(topic)))
+ .andExpect(status().isOk());
+
+ verify(topicService, times(1)).updateTopic(eq("spring"), any(Topic.class));
+ }
+
+ @Test
+ public void deleteTopic_callsServiceDeleteTopic() throws Exception {
+ mockMvc.perform(delete("/topic/spring"))
+ .andExpect(status().isOk());
+
+ verify(topicService, times(1)).deleteTopic("spring");
+ }
+
+ @Test
+ public void filterMinimumLengthForId_returns200WithFilteredTopics() throws Exception {
+ List filtered = Arrays.asList(
+ new Topic("spring", "Spring Framework", "Spring Description"),
+ new Topic("javascript", "JavaScript", "JS Description")
+ );
+ when(topicService.filterMinimumLengthForId(4)).thenReturn(filtered);
+
+ mockMvc.perform(get("/topic/minimum/length/4"))
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("$", hasSize(2)))
+ .andExpect(jsonPath("$[0].id", is("spring")))
+ .andExpect(jsonPath("$[1].id", is("javascript")));
+ }
+
+ @Test
+ public void sortTopicsWithID_returns200WithSortedTopics() throws Exception {
+ List sorted = Arrays.asList(
+ new Topic("java", "Core Java", "Java Description"),
+ new Topic("javascript", "JavaScript", "JS Description"),
+ new Topic("spring", "Spring Framework", "Spring Description")
+ );
+ when(topicService.sortTopicsWithID()).thenReturn(sorted);
+
+ mockMvc.perform(get("/topic/sort"))
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("$", hasSize(3)))
+ .andExpect(jsonPath("$[0].id", is("java")))
+ .andExpect(jsonPath("$[1].id", is("javascript")))
+ .andExpect(jsonPath("$[2].id", is("spring")));
+ }
+}
diff --git a/src/test/java/hello/declaration/CustomPredicateTest.java b/src/test/java/hello/declaration/CustomPredicateTest.java
new file mode 100644
index 0000000..9341f13
--- /dev/null
+++ b/src/test/java/hello/declaration/CustomPredicateTest.java
@@ -0,0 +1,28 @@
+package hello.declaration;
+
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+public class CustomPredicateTest {
+
+ @Test
+ public void test_withLambda_returnsTrue() {
+ CustomPredicate isLong = s -> s.length() > 3;
+ assertTrue(isLong.test("hello"));
+ }
+
+ @Test
+ public void test_withLambda_returnsFalse() {
+ CustomPredicate isLong = s -> s.length() > 3;
+ assertFalse(isLong.test("hi"));
+ }
+
+ @Test
+ public void test_withIntegerPredicate() {
+ CustomPredicate isPositive = n -> n > 0;
+ assertTrue(isPositive.test(5));
+ assertFalse(isPositive.test(-1));
+ assertFalse(isPositive.test(0));
+ }
+}
diff --git a/src/test/java/hello/declaration/TimeClientTest.java b/src/test/java/hello/declaration/TimeClientTest.java
new file mode 100644
index 0000000..38f202a
--- /dev/null
+++ b/src/test/java/hello/declaration/TimeClientTest.java
@@ -0,0 +1,32 @@
+package hello.declaration;
+
+import hello.model.SimpleTimeClient;
+import org.junit.Test;
+
+import java.time.ZoneId;
+import java.time.ZonedDateTime;
+
+import static org.junit.Assert.*;
+
+public class TimeClientTest {
+
+ @Test
+ public void getZoneId_validZone_returnsCorrectZoneId() {
+ ZoneId zoneId = TimeClient.getZoneId("America/New_York");
+ assertEquals("America/New_York", zoneId.getId());
+ }
+
+ @Test
+ public void getZoneId_invalidZone_fallsBackToSystemDefault() {
+ ZoneId zoneId = TimeClient.getZoneId("Invalid/Zone");
+ assertEquals(ZoneId.systemDefault(), zoneId);
+ }
+
+ @Test
+ public void getZonedDateTime_viaSimpleTimeClient_returnsNonNull() {
+ SimpleTimeClient client = new SimpleTimeClient();
+ ZonedDateTime zdt = client.getZonedDateTime("Canada/Central");
+ assertNotNull(zdt);
+ assertEquals("Canada/Central", zdt.getZone().getId());
+ }
+}
diff --git a/src/test/java/hello/model/CustomerTest.java b/src/test/java/hello/model/CustomerTest.java
new file mode 100644
index 0000000..b994a3f
--- /dev/null
+++ b/src/test/java/hello/model/CustomerTest.java
@@ -0,0 +1,38 @@
+package hello.model;
+
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+public class CustomerTest {
+
+ @Test
+ public void constructorAndGetters() {
+ Customer customer = new Customer(1L, "John", "Doe");
+ assertEquals(1L, customer.getId());
+ assertEquals("John", customer.getFirstName());
+ assertEquals("Doe", customer.getLastName());
+ }
+
+ @Test
+ public void setters() {
+ Customer customer = new Customer(1L, "John", "Doe");
+ customer.setId(2L);
+ customer.setFirstName("Jane");
+ customer.setLastName("Smith");
+
+ assertEquals(2L, customer.getId());
+ assertEquals("Jane", customer.getFirstName());
+ assertEquals("Smith", customer.getLastName());
+ }
+
+ @Test
+ public void toString_containsAllFields() {
+ Customer customer = new Customer(1L, "John", "Doe");
+ String result = customer.toString();
+ assertTrue(result.contains("id=1"));
+ assertTrue(result.contains("firstName='John'"));
+ assertTrue(result.contains("lastName='Doe'"));
+ assertTrue(result.startsWith("Customer{"));
+ }
+}
diff --git a/src/test/java/hello/model/GreetingTest.java b/src/test/java/hello/model/GreetingTest.java
new file mode 100644
index 0000000..29bced9
--- /dev/null
+++ b/src/test/java/hello/model/GreetingTest.java
@@ -0,0 +1,22 @@
+package hello.model;
+
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+public class GreetingTest {
+
+ @Test
+ public void constructorAndGetters() {
+ Greeting greeting = new Greeting(1L, "Hello, World!");
+ assertEquals(1L, greeting.getId());
+ assertEquals("Hello, World!", greeting.getContent());
+ }
+
+ @Test
+ public void differentValues() {
+ Greeting greeting = new Greeting(42L, "Hello, Devin!");
+ assertEquals(42L, greeting.getId());
+ assertEquals("Hello, Devin!", greeting.getContent());
+ }
+}
diff --git a/src/test/java/hello/model/QuoteTest.java b/src/test/java/hello/model/QuoteTest.java
new file mode 100644
index 0000000..5693c89
--- /dev/null
+++ b/src/test/java/hello/model/QuoteTest.java
@@ -0,0 +1,40 @@
+package hello.model;
+
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+public class QuoteTest {
+
+ @Test
+ public void noArgConstructorAndGettersSetters() {
+ Quote quote = new Quote();
+ assertNull(quote.getType());
+ assertNull(quote.getValue());
+
+ Value value = new Value();
+ value.setId(1L);
+ value.setQuote("test quote");
+
+ quote.setType("success");
+ quote.setValue(value);
+
+ assertEquals("success", quote.getType());
+ assertEquals(value, quote.getValue());
+ }
+
+ @Test
+ public void toString_containsTypeAndValue() {
+ Quote quote = new Quote();
+ quote.setType("success");
+ Value value = new Value();
+ value.setId(1L);
+ value.setQuote("test");
+ quote.setValue(value);
+
+ String result = quote.toString();
+ assertTrue(result.contains("type='success'"));
+ assertTrue(result.contains("value="));
+ assertTrue(result.startsWith("Quote{"));
+ }
+}
diff --git a/src/test/java/hello/model/SimpleTimeClientTest.java b/src/test/java/hello/model/SimpleTimeClientTest.java
new file mode 100644
index 0000000..9b5f2f5
--- /dev/null
+++ b/src/test/java/hello/model/SimpleTimeClientTest.java
@@ -0,0 +1,72 @@
+package hello.model;
+
+import org.junit.Test;
+
+import java.time.LocalDateTime;
+import java.time.ZonedDateTime;
+
+import static org.junit.Assert.*;
+
+public class SimpleTimeClientTest {
+
+ @Test
+ public void defaultConstructor_setsDateAndTimeToApproximatelyNow() {
+ SimpleTimeClient client = new SimpleTimeClient();
+ LocalDateTime now = LocalDateTime.now();
+ LocalDateTime clientTime = client.getLocalDateTime();
+
+ assertEquals(now.getYear(), clientTime.getYear());
+ assertEquals(now.getMonth(), clientTime.getMonth());
+ assertEquals(now.getDayOfMonth(), clientTime.getDayOfMonth());
+ assertEquals(now.getHour(), clientTime.getHour());
+ }
+
+ @Test
+ public void setTime_updatesTimeComponent() {
+ SimpleTimeClient client = new SimpleTimeClient();
+ client.setTime(10, 30, 0);
+ LocalDateTime result = client.getLocalDateTime();
+ assertEquals(10, result.getHour());
+ assertEquals(30, result.getMinute());
+ assertEquals(0, result.getSecond());
+ }
+
+ @Test
+ public void setDate_updatesDateComponent() {
+ SimpleTimeClient client = new SimpleTimeClient();
+ client.setDate(2020, 6, 15);
+ LocalDateTime result = client.getLocalDateTime();
+ assertEquals(2020, result.getYear());
+ assertEquals(6, result.getMonthValue());
+ assertEquals(15, result.getDayOfMonth());
+ }
+
+ @Test
+ public void setDateAndTime_updatesBothComponents() {
+ SimpleTimeClient client = new SimpleTimeClient();
+ client.setDateAndTime(2021, 12, 25, 14, 30, 45);
+ LocalDateTime result = client.getLocalDateTime();
+ assertEquals(2021, result.getYear());
+ assertEquals(12, result.getMonthValue());
+ assertEquals(25, result.getDayOfMonth());
+ assertEquals(14, result.getHour());
+ assertEquals(30, result.getMinute());
+ assertEquals(45, result.getSecond());
+ }
+
+ @Test
+ public void toString_returnsNonNullString() {
+ SimpleTimeClient client = new SimpleTimeClient();
+ String result = client.toString();
+ assertNotNull(result);
+ assertFalse(result.isEmpty());
+ }
+
+ @Test
+ public void getZonedDateTime_returnsNonNull() {
+ SimpleTimeClient client = new SimpleTimeClient();
+ ZonedDateTime zdt = client.getZonedDateTime("America/New_York");
+ assertNotNull(zdt);
+ assertEquals("America/New_York", zdt.getZone().getId());
+ }
+}
diff --git a/src/test/java/hello/model/TopicTest.java b/src/test/java/hello/model/TopicTest.java
new file mode 100644
index 0000000..03c71c8
--- /dev/null
+++ b/src/test/java/hello/model/TopicTest.java
@@ -0,0 +1,41 @@
+package hello.model;
+
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+public class TopicTest {
+
+ @Test
+ public void noArgConstructorAndSetters() {
+ Topic topic = new Topic();
+ topic.setId("test");
+ topic.setSubjectName("Test Subject");
+ topic.setSubjectDescription("Test Description");
+
+ assertEquals("test", topic.getId());
+ assertEquals("Test Subject", topic.getSubjectName());
+ assertEquals("Test Description", topic.getSubjectDescription());
+ }
+
+ @Test
+ public void allArgsConstructorAndGetters() {
+ Topic topic = new Topic("spring", "Spring Framework", "Spring Desc");
+
+ assertEquals("spring", topic.getId());
+ assertEquals("Spring Framework", topic.getSubjectName());
+ assertEquals("Spring Desc", topic.getSubjectDescription());
+ }
+
+ @Test
+ public void settersOverwritePreviousValues() {
+ Topic topic = new Topic("old", "Old Name", "Old Desc");
+ topic.setId("new");
+ topic.setSubjectName("New Name");
+ topic.setSubjectDescription("New Desc");
+
+ assertEquals("new", topic.getId());
+ assertEquals("New Name", topic.getSubjectName());
+ assertEquals("New Desc", topic.getSubjectDescription());
+ }
+}
diff --git a/src/test/java/hello/model/ValueTest.java b/src/test/java/hello/model/ValueTest.java
new file mode 100644
index 0000000..5a36db7
--- /dev/null
+++ b/src/test/java/hello/model/ValueTest.java
@@ -0,0 +1,33 @@
+package hello.model;
+
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+public class ValueTest {
+
+ @Test
+ public void noArgConstructorAndGettersSetters() {
+ Value value = new Value();
+ assertNull(value.getId());
+ assertNull(value.getQuote());
+
+ value.setId(1L);
+ value.setQuote("test quote");
+
+ assertEquals(Long.valueOf(1L), value.getId());
+ assertEquals("test quote", value.getQuote());
+ }
+
+ @Test
+ public void toString_containsIdAndQuote() {
+ Value value = new Value();
+ value.setId(42L);
+ value.setQuote("Spring is great");
+
+ String result = value.toString();
+ assertTrue(result.contains("id=42"));
+ assertTrue(result.contains("quote='Spring is great'"));
+ assertTrue(result.startsWith("Value{"));
+ }
+}
diff --git a/src/test/java/hello/service/TopicServiceTest.java b/src/test/java/hello/service/TopicServiceTest.java
new file mode 100644
index 0000000..4eac377
--- /dev/null
+++ b/src/test/java/hello/service/TopicServiceTest.java
@@ -0,0 +1,157 @@
+package hello.service;
+
+import hello.model.Topic;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.List;
+import java.util.NoSuchElementException;
+
+import static org.junit.Assert.*;
+
+public class TopicServiceTest {
+
+ private TopicService topicService;
+
+ @Before
+ public void setUp() {
+ topicService = new TopicService();
+ }
+
+ @Test
+ public void getAllTopics_returnsThreeDefaultTopics() {
+ List topics = topicService.getAllTopics();
+ assertEquals(3, topics.size());
+ assertEquals("spring", topics.get(0).getId());
+ assertEquals("java", topics.get(1).getId());
+ assertEquals("javascript", topics.get(2).getId());
+ }
+
+ @Test
+ public void getTopicWithId_existingId_returnsTopic() {
+ Topic topic = topicService.getTopicWithId("spring");
+ assertNotNull(topic);
+ assertEquals("spring", topic.getId());
+ assertEquals("Spring Framework", topic.getSubjectName());
+ assertEquals("Spring Framework Description", topic.getSubjectDescription());
+ }
+
+ @Test(expected = NoSuchElementException.class)
+ public void getTopicWithId_nonExistentId_throwsNoSuchElementException() {
+ topicService.getTopicWithId("nonexistent");
+ }
+
+ @Test
+ public void addTopic_addsNewTopicToList() {
+ Topic newTopic = new Topic("python", "Python Language", "Python Description");
+ int sizeBefore = topicService.getAllTopics().size();
+ topicService.addTopic(newTopic);
+ List topics = topicService.getAllTopics();
+ assertEquals(sizeBefore + 1, topics.size());
+ assertEquals("python", topics.get(topics.size() - 1).getId());
+ }
+
+ @Test
+ public void updateTopic_existingId_updatesTopic() {
+ Topic updatedTopic = new Topic("spring", "Spring Boot", "Updated Description");
+ topicService.updateTopic("spring", updatedTopic);
+ Topic result = topicService.getTopicWithId("spring");
+ assertEquals("Spring Boot", result.getSubjectName());
+ assertEquals("Updated Description", result.getSubjectDescription());
+ }
+
+ @Test
+ public void updateTopic_nonExistentId_noOp() {
+ int sizeBefore = topicService.getAllTopics().size();
+ Topic updatedTopic = new Topic("nonexistent", "Name", "Desc");
+ topicService.updateTopic("nonexistent", updatedTopic);
+ assertEquals(sizeBefore, topicService.getAllTopics().size());
+ }
+
+ @Test
+ public void deleteTopic_existingId_removesTopic() {
+ topicService.deleteTopic("java");
+ List topics = topicService.getAllTopics();
+ assertEquals(2, topics.size());
+ assertTrue(topics.stream().noneMatch(t -> t.getId().equals("java")));
+ }
+
+ @Test
+ public void deleteTopic_nonExistentId_noOp() {
+ int sizeBefore = topicService.getAllTopics().size();
+ topicService.deleteTopic("nonexistent");
+ assertEquals(sizeBefore, topicService.getAllTopics().size());
+ }
+
+ @Test
+ public void filterMinimumLengthForId_four_returnsSpringAndJavascript() {
+ List filtered = topicService.filterMinimumLengthForId(4);
+ assertEquals(2, filtered.size());
+ assertTrue(filtered.stream().anyMatch(t -> t.getId().equals("spring")));
+ assertTrue(filtered.stream().anyMatch(t -> t.getId().equals("javascript")));
+ }
+
+ @Test
+ public void filterMinimumLengthForId_hundred_returnsEmpty() {
+ List filtered = topicService.filterMinimumLengthForId(100);
+ assertTrue(filtered.isEmpty());
+ }
+
+ @Test
+ public void sortTopicsWithID_returnsSortedByIdAlphabetically() {
+ List sorted = topicService.sortTopicsWithID();
+ assertEquals("java", sorted.get(0).getId());
+ assertEquals("javascript", sorted.get(1).getId());
+ assertEquals("spring", sorted.get(2).getId());
+ }
+
+ @Test
+ public void returnAllTopicIDWithStringSlicing_returnsColonJoinedIds() {
+ String result = topicService.returnAllTopicIDWithStringSlicing();
+ assertEquals("spring:java:javascript", result);
+ }
+
+ @Test
+ public void makeDistinctAndSortCharacters_returnsSortedDistinctChars() {
+ String result = topicService.makeDistinctAndSortCharacters("abc:abc");
+ String expected = ":abc";
+ assertEquals(expected, result);
+ }
+
+ @Test
+ public void splitAllIdWithColonSelectIDWithJavaKeywordThenSortThenJoin_returnsJavaAndJavascript() {
+ String result = topicService.splitAllIdWithColonSelectIDWithJavaKeywordThenSortThenJoin("spring:java:javascript");
+ assertEquals("java:javascript", result);
+ }
+
+ @Test
+ public void findIdHavingCharacter_returnsIdsContainingG() {
+ String result = topicService.findIdHavingCharacter();
+ assertTrue(result.contains("spring"));
+ assertFalse(result.contains("java"));
+ }
+
+ @Test
+ public void findAllFilesInPathAndSort_returnsNonNullString() {
+ String result = topicService.findAllFilesInPathAndSort();
+ assertNotNull(result);
+ }
+
+ @Test
+ public void findParticularFileInPathAndSort_returnsNonNullString() {
+ String result = topicService.findParticularFileInPathAndSort();
+ assertNotNull(result);
+ }
+
+ @Test
+ public void findParticularFileInPathAndSortWithWalkFunction_returnsNonNullString() {
+ String result = topicService.findParticularFileInPathAndSortWithWalkFunction();
+ assertNotNull(result);
+ }
+
+ @Test
+ public void readFileWithStreamFunction_returnsResultOrIOException() {
+ String result = topicService.readFileWithStreamFunction();
+ assertNotNull(result);
+ }
+}
diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties
new file mode 100644
index 0000000..67ffe05
--- /dev/null
+++ b/src/test/resources/application.properties
@@ -0,0 +1,2 @@
+spring.datasource.url=jdbc:h2:mem:testdb
+spring.datasource.driver-class-name=org.h2.Driver
diff --git a/src/test/resources/temp.txt b/src/test/resources/temp.txt
new file mode 100644
index 0000000..06d8c8c
--- /dev/null
+++ b/src/test/resources/temp.txt
@@ -0,0 +1,5 @@
+print Hello
+print World
+this is a test
+print Java8
+no match here