diff --git a/pom.xml b/pom.xml
index 63f5cbd..5e2a9c1 100644
--- a/pom.xml
+++ b/pom.xml
@@ -5,7 +5,7 @@
org.springframework
gs-spring-boot
- pom
+ jar
0.1.0
@@ -32,10 +32,17 @@
com.h2database
h2
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
1.8
+ 2.28.2
+ 1.9.16
@@ -45,6 +52,25 @@
org.springframework.boot
spring-boot-maven-plugin
+
+ org.jacoco
+ jacoco-maven-plugin
+ 0.8.8
+
+
+
+ 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..fa0f6d1
--- /dev/null
+++ b/src/test/java/hello/ApplicationTest.java
@@ -0,0 +1,72 @@
+package hello;
+
+import hello.model.Quote;
+import hello.model.Value;
+import org.junit.Test;
+import org.springframework.boot.CommandLineRunner;
+import org.springframework.boot.web.client.RestTemplateBuilder;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
+import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
+import org.springframework.web.client.RestTemplate;
+
+import javax.sql.DataSource;
+
+import static org.junit.Assert.*;
+import static org.mockito.ArgumentMatchers.*;
+import static org.mockito.Mockito.*;
+
+public class ApplicationTest {
+
+ @Test
+ public void testRestTemplateBean() {
+ Application app = new Application();
+ RestTemplateBuilder builder = new RestTemplateBuilder();
+ RestTemplate rt = app.restTemplate(builder);
+ assertNotNull(rt);
+ }
+
+ @Test
+ public void testCommandLineRunnerBean() throws Exception {
+ Application app = new Application();
+ RestTemplate mockRestTemplate = mock(RestTemplate.class);
+ Quote quote = new Quote();
+ quote.setType("success");
+ Value value = new Value();
+ value.setId(1L);
+ value.setQuote("Test");
+ quote.setValue(value);
+ when(mockRestTemplate.getForObject(anyString(), eq(Quote.class))).thenReturn(quote);
+
+ CommandLineRunner runner = app.run(mockRestTemplate);
+ assertNotNull(runner);
+ runner.run();
+ verify(mockRestTemplate).getForObject(anyString(), eq(Quote.class));
+ }
+
+ @Test
+ public void testRunJdbcOperations() throws Exception {
+ DataSource dataSource = new EmbeddedDatabaseBuilder()
+ .setType(EmbeddedDatabaseType.H2)
+ .build();
+ JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
+
+ Application app = new Application();
+ java.lang.reflect.Field field = Application.class.getDeclaredField("jdbcTemplate");
+ field.setAccessible(true);
+ field.set(app, jdbcTemplate);
+
+ app.run(new String[]{});
+
+ java.util.List customers = jdbcTemplate.query(
+ "SELECT id, first_name, last_name FROM customers WHERE first_name = ?",
+ new Object[]{"Josh"},
+ (rs, rowNum) -> new hello.model.Customer(
+ rs.getLong("id"),
+ rs.getString("first_name"),
+ rs.getString("last_name")
+ )
+ );
+ assertEquals(2, customers.size());
+ }
+}
diff --git a/src/test/java/hello/controller/GreetingControllerTest.java b/src/test/java/hello/controller/GreetingControllerTest.java
new file mode 100644
index 0000000..b6aaf6d
--- /dev/null
+++ b/src/test/java/hello/controller/GreetingControllerTest.java
@@ -0,0 +1,42 @@
+package hello.controller;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+
+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;
+
+ @Before
+ public void setUp() {
+ mockMvc = MockMvcBuilders.standaloneSetup(new GreetingController()).build();
+ }
+
+ @Test
+ public void testGreetingDefault() throws Exception {
+ mockMvc.perform(get("/"))
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("$.content").value("Hello, World!"))
+ .andExpect(jsonPath("$.id").isNumber());
+ }
+
+ @Test
+ public void testGreetingWithName() throws Exception {
+ mockMvc.perform(get("/").param("name", "Bob"))
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("$.content").value("Hello, Bob!"));
+ }
+
+ @Test
+ public void testGreetingIncrementsId() throws Exception {
+ mockMvc.perform(get("/"))
+ .andExpect(status().isOk());
+ mockMvc.perform(get("/"))
+ .andExpect(status().isOk());
+ }
+}
diff --git a/src/test/java/hello/controller/HelloControllerTest.java b/src/test/java/hello/controller/HelloControllerTest.java
new file mode 100644
index 0000000..e0df3fa
--- /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.mockito.ArgumentMatchers.anyString;
+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 testDatetime() throws Exception {
+ mockMvc.perform(get("/datetime"))
+ .andExpect(status().isOk())
+ .andExpect(content().string(org.hamcrest.Matchers.containsString("Greetings from Spring Boot!")));
+ }
+
+ @Test
+ public void testDatetimeContainsLeapYear() throws Exception {
+ mockMvc.perform(get("/datetime"))
+ .andExpect(status().isOk())
+ .andExpect(content().string(org.hamcrest.Matchers.containsString("Is this a leap year")));
+ }
+
+ @Test
+ public void testShowStringOperation() throws Exception {
+ when(topicService.returnAllTopicIDWithStringSlicing()).thenReturn("spring:java:javascript");
+ when(topicService.makeDistinctAndSortCharacters(anyString())).thenReturn("acegijnoprst");
+ when(topicService.splitAllIdWithColonSelectIDWithJavaKeywordThenSortThenJoin(anyString())).thenReturn("java:javascript");
+ when(topicService.findIdHavingCharacter()).thenReturn("[spring]");
+
+ mockMvc.perform(get("/topic/string/operation"))
+ .andExpect(status().isOk())
+ .andExpect(content().string(org.hamcrest.Matchers.containsString("spring:java:javascript")));
+ }
+
+ @Test
+ public void testShowFileOperation() throws Exception {
+ when(topicService.findAllFilesInPathAndSort()).thenReturn("file1; file2");
+ when(topicService.findParticularFileInPathAndSort()).thenReturn("gradle_file");
+ when(topicService.findParticularFileInPathAndSortWithWalkFunction()).thenReturn("gradle_walk");
+ when(topicService.readFileWithStreamFunction()).thenReturn("print_content");
+
+ mockMvc.perform(get("/topic/file/operation"))
+ .andExpect(status().isOk())
+ .andExpect(content().string(org.hamcrest.Matchers.containsString("file1; file2")));
+ }
+}
diff --git a/src/test/java/hello/controller/TopicControllerTest.java b/src/test/java/hello/controller/TopicControllerTest.java
new file mode 100644
index 0000000..486d13d
--- /dev/null
+++ b/src/test/java/hello/controller/TopicControllerTest.java
@@ -0,0 +1,123 @@
+package hello.controller;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import hello.model.Topic;
+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.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.mockito.ArgumentMatchers.*;
+import static org.mockito.Mockito.*;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
+
+public class TopicControllerTest {
+
+ private MockMvc mockMvc;
+ private ObjectMapper objectMapper = new ObjectMapper();
+
+ @Mock
+ private TopicService topicService;
+
+ @InjectMocks
+ private TopicController topicController;
+
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+ mockMvc = MockMvcBuilders.standaloneSetup(topicController).build();
+ }
+
+ @Test
+ public void testGetAllTopics() throws Exception {
+ List topics = Arrays.asList(
+ new Topic("java", "Core Java", "Java Description"),
+ new Topic("spring", "Spring Framework", "Spring Description")
+ );
+ when(topicService.getAllTopics()).thenReturn(topics);
+
+ mockMvc.perform(get("/topic"))
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("$[0].id").value("java"))
+ .andExpect(jsonPath("$[1].id").value("spring"));
+ }
+
+ @Test
+ public void testGetTopicWithID() throws Exception {
+ Topic topic = new Topic("java", "Core Java", "Java Description");
+ when(topicService.getTopicWithId("java")).thenReturn(topic);
+
+ mockMvc.perform(get("/topic/java"))
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("$.id").value("java"))
+ .andExpect(jsonPath("$.subjectName").value("Core Java"));
+ }
+
+ @Test
+ public void testAddTopic() throws Exception {
+ Topic topic = new Topic("python", "Python", "Python Description");
+
+ mockMvc.perform(post("/topic")
+ .contentType(MediaType.APPLICATION_JSON)
+ .content(objectMapper.writeValueAsString(topic)))
+ .andExpect(status().isOk());
+
+ verify(topicService).addTopic(any(Topic.class));
+ }
+
+ @Test
+ public void testUpdateTopic() throws Exception {
+ Topic topic = new Topic("java", "Updated Java", "Updated Description");
+
+ mockMvc.perform(put("/topic/java")
+ .contentType(MediaType.APPLICATION_JSON)
+ .content(objectMapper.writeValueAsString(topic)))
+ .andExpect(status().isOk());
+
+ verify(topicService).updateTopic(eq("java"), any(Topic.class));
+ }
+
+ @Test
+ public void testDeleteTopic() throws Exception {
+ mockMvc.perform(delete("/topic/java"))
+ .andExpect(status().isOk());
+
+ verify(topicService).deleteTopic("java");
+ }
+
+ @Test
+ public void testFilterMinimumLengthForId() 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("$[0].id").value("spring"));
+ }
+
+ @Test
+ public void testSortTopicsWithID() 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("$[0].id").value("java"));
+ }
+}
diff --git a/src/test/java/hello/declaration/CustomPredicateTest.java b/src/test/java/hello/declaration/CustomPredicateTest.java
new file mode 100644
index 0000000..572ff5b
--- /dev/null
+++ b/src/test/java/hello/declaration/CustomPredicateTest.java
@@ -0,0 +1,21 @@
+package hello.declaration;
+
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+public class CustomPredicateTest {
+
+ @Test
+ public void testPredicateWithString() {
+ CustomPredicate isLong = s -> s.length() > 5;
+ assertTrue(isLong.test("longstring"));
+ assertFalse(isLong.test("hi"));
+ }
+
+ @Test
+ public void testPredicateWithInteger() {
+ CustomPredicate isPositive = i -> i > 0;
+ assertTrue(isPositive.test(5));
+ assertFalse(isPositive.test(-1));
+ }
+}
diff --git a/src/test/java/hello/declaration/TimeClientTest.java b/src/test/java/hello/declaration/TimeClientTest.java
new file mode 100644
index 0000000..5e2cbba
--- /dev/null
+++ b/src/test/java/hello/declaration/TimeClientTest.java
@@ -0,0 +1,28 @@
+package hello.declaration;
+
+import org.junit.Test;
+
+import java.time.ZoneId;
+
+import static org.junit.Assert.*;
+
+public class TimeClientTest {
+
+ @Test
+ public void testGetZoneIdValid() {
+ ZoneId zone = TimeClient.getZoneId("America/New_York");
+ assertEquals("America/New_York", zone.getId());
+ }
+
+ @Test
+ public void testGetZoneIdInvalid() {
+ ZoneId zone = TimeClient.getZoneId("Invalid/Zone");
+ assertEquals(ZoneId.systemDefault(), zone);
+ }
+
+ @Test
+ public void testGetZoneIdUTC() {
+ ZoneId zone = TimeClient.getZoneId("UTC");
+ assertEquals("UTC", zone.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..c5fd6c7
--- /dev/null
+++ b/src/test/java/hello/model/CustomerTest.java
@@ -0,0 +1,35 @@
+package hello.model;
+
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+public class CustomerTest {
+
+ @Test
+ public void testConstructorAndGetters() {
+ Customer customer = new Customer(1L, "John", "Doe");
+ assertEquals(1L, customer.getId());
+ assertEquals("John", customer.getFirstName());
+ assertEquals("Doe", customer.getLastName());
+ }
+
+ @Test
+ public void testSetters() {
+ 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 testToString() {
+ Customer customer = new Customer(1L, "John", "Doe");
+ String result = customer.toString();
+ assertTrue(result.contains("John"));
+ assertTrue(result.contains("Doe"));
+ assertTrue(result.contains("1"));
+ }
+}
diff --git a/src/test/java/hello/model/GreetingTest.java b/src/test/java/hello/model/GreetingTest.java
new file mode 100644
index 0000000..f2b34ff
--- /dev/null
+++ b/src/test/java/hello/model/GreetingTest.java
@@ -0,0 +1,21 @@
+package hello.model;
+
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+public class GreetingTest {
+
+ @Test
+ public void testConstructorAndGetters() {
+ Greeting greeting = new Greeting(1L, "Hello, World!");
+ assertEquals(1L, greeting.getId());
+ assertEquals("Hello, World!", greeting.getContent());
+ }
+
+ @Test
+ public void testDifferentValues() {
+ Greeting greeting = new Greeting(42L, "Test content");
+ assertEquals(42L, greeting.getId());
+ assertEquals("Test content", 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..8807faa
--- /dev/null
+++ b/src/test/java/hello/model/QuoteTest.java
@@ -0,0 +1,44 @@
+package hello.model;
+
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+public class QuoteTest {
+
+ @Test
+ public void testDefaultConstructor() {
+ Quote quote = new Quote();
+ assertNull(quote.getType());
+ assertNull(quote.getValue());
+ }
+
+ @Test
+ public void testSettersAndGetters() {
+ Quote quote = new Quote();
+ quote.setType("success");
+
+ Value value = new Value();
+ value.setId(1L);
+ value.setQuote("Test quote");
+ quote.setValue(value);
+
+ assertEquals("success", quote.getType());
+ assertNotNull(quote.getValue());
+ assertEquals("Test quote", quote.getValue().getQuote());
+ }
+
+ @Test
+ public void testToString() {
+ 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("success"));
+ assertTrue(result.contains("Value{"));
+ }
+}
diff --git a/src/test/java/hello/model/SimpleTimeClientTest.java b/src/test/java/hello/model/SimpleTimeClientTest.java
new file mode 100644
index 0000000..7be01a5
--- /dev/null
+++ b/src/test/java/hello/model/SimpleTimeClientTest.java
@@ -0,0 +1,74 @@
+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 testDefaultConstructor() {
+ SimpleTimeClient client = new SimpleTimeClient();
+ assertNotNull(client.getLocalDateTime());
+ }
+
+ @Test
+ public void testSetTime() {
+ SimpleTimeClient client = new SimpleTimeClient();
+ client.setTime(10, 30, 45);
+ LocalDateTime dt = client.getLocalDateTime();
+ assertEquals(10, dt.getHour());
+ assertEquals(30, dt.getMinute());
+ assertEquals(45, dt.getSecond());
+ }
+
+ @Test
+ public void testSetDate() {
+ SimpleTimeClient client = new SimpleTimeClient();
+ client.setDate(2020, 6, 15);
+ LocalDateTime dt = client.getLocalDateTime();
+ assertEquals(2020, dt.getYear());
+ assertEquals(6, dt.getMonthValue());
+ assertEquals(15, dt.getDayOfMonth());
+ }
+
+ @Test
+ public void testSetDateAndTime() {
+ SimpleTimeClient client = new SimpleTimeClient();
+ client.setDateAndTime(2021, 3, 25, 14, 30, 0);
+ LocalDateTime dt = client.getLocalDateTime();
+ assertEquals(2021, dt.getYear());
+ assertEquals(3, dt.getMonthValue());
+ assertEquals(25, dt.getDayOfMonth());
+ assertEquals(14, dt.getHour());
+ assertEquals(30, dt.getMinute());
+ assertEquals(0, dt.getSecond());
+ }
+
+ @Test
+ public void testToString() {
+ SimpleTimeClient client = new SimpleTimeClient();
+ client.setDateAndTime(2021, 3, 25, 14, 30, 0);
+ String result = client.toString();
+ assertTrue(result.contains("2021"));
+ }
+
+ @Test
+ public void testGetZonedDateTime() {
+ SimpleTimeClient client = new SimpleTimeClient();
+ client.setDateAndTime(2021, 6, 15, 10, 0, 0);
+ ZonedDateTime zonedDateTime = client.getZonedDateTime("America/New_York");
+ assertNotNull(zonedDateTime);
+ assertEquals("America/New_York", zonedDateTime.getZone().getId());
+ }
+
+ @Test
+ public void testGetZonedDateTimeInvalidZone() {
+ SimpleTimeClient client = new SimpleTimeClient();
+ ZonedDateTime zonedDateTime = client.getZonedDateTime("Invalid/Zone");
+ assertNotNull(zonedDateTime);
+ }
+}
diff --git a/src/test/java/hello/model/TopicTest.java b/src/test/java/hello/model/TopicTest.java
new file mode 100644
index 0000000..6a5180a
--- /dev/null
+++ b/src/test/java/hello/model/TopicTest.java
@@ -0,0 +1,34 @@
+package hello.model;
+
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+public class TopicTest {
+
+ @Test
+ public void testDefaultConstructor() {
+ Topic topic = new Topic();
+ assertNull(topic.getId());
+ assertNull(topic.getSubjectName());
+ assertNull(topic.getSubjectDescription());
+ }
+
+ @Test
+ public void testParameterizedConstructor() {
+ Topic topic = new Topic("java", "Core Java", "Java Description");
+ assertEquals("java", topic.getId());
+ assertEquals("Core Java", topic.getSubjectName());
+ assertEquals("Java Description", topic.getSubjectDescription());
+ }
+
+ @Test
+ public void testSetters() {
+ Topic topic = new Topic();
+ topic.setId("spring");
+ topic.setSubjectName("Spring Framework");
+ topic.setSubjectDescription("Spring Description");
+ assertEquals("spring", topic.getId());
+ assertEquals("Spring Framework", topic.getSubjectName());
+ assertEquals("Spring Description", 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..b335bbf
--- /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 testDefaultConstructor() {
+ Value value = new Value();
+ assertNull(value.getId());
+ assertNull(value.getQuote());
+ }
+
+ @Test
+ public void testSettersAndGetters() {
+ Value value = new Value();
+ value.setId(1L);
+ value.setQuote("Test quote");
+ assertEquals(Long.valueOf(1L), value.getId());
+ assertEquals("Test quote", value.getQuote());
+ }
+
+ @Test
+ public void testToString() {
+ Value value = new Value();
+ value.setId(1L);
+ value.setQuote("Hello");
+ String result = value.toString();
+ assertTrue(result.contains("1"));
+ assertTrue(result.contains("Hello"));
+ }
+}
diff --git a/src/test/java/hello/service/TopicServiceTest.java b/src/test/java/hello/service/TopicServiceTest.java
new file mode 100644
index 0000000..5a4ab16
--- /dev/null
+++ b/src/test/java/hello/service/TopicServiceTest.java
@@ -0,0 +1,176 @@
+package hello.service;
+
+import hello.model.Topic;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.List;
+
+import static org.junit.Assert.*;
+
+public class TopicServiceTest {
+
+ private TopicService topicService;
+
+ @Before
+ public void setUp() {
+ topicService = new TopicService();
+ }
+
+ @Test
+ public void testGetAllTopics() {
+ List topics = topicService.getAllTopics();
+ assertNotNull(topics);
+ assertEquals(3, topics.size());
+ }
+
+ @Test
+ public void testGetTopicWithId() {
+ Topic topic = topicService.getTopicWithId("java");
+ assertNotNull(topic);
+ assertEquals("java", topic.getId());
+ assertEquals("Core Java", topic.getSubjectName());
+ }
+
+ @Test
+ public void testGetTopicWithIdSpring() {
+ Topic topic = topicService.getTopicWithId("spring");
+ assertNotNull(topic);
+ assertEquals("spring", topic.getId());
+ }
+
+ @Test
+ public void testAddTopic() {
+ Topic newTopic = new Topic("python", "Python", "Python Description");
+ topicService.addTopic(newTopic);
+ List topics = topicService.getAllTopics();
+ assertEquals(4, topics.size());
+ assertEquals("python", topicService.getTopicWithId("python").getId());
+ }
+
+ @Test
+ public void testUpdateTopicExisting() {
+ Topic updatedTopic = new Topic("java", "Updated Java", "Updated Description");
+ topicService.updateTopic("java", updatedTopic);
+ Topic result = topicService.getTopicWithId("java");
+ assertEquals("Updated Java", result.getSubjectName());
+ }
+
+ @Test
+ public void testUpdateTopicNonExisting() {
+ int sizeBefore = topicService.getAllTopics().size();
+ Topic updatedTopic = new Topic("nonexistent", "No", "No");
+ topicService.updateTopic("nonexistent", updatedTopic);
+ assertEquals(sizeBefore, topicService.getAllTopics().size());
+ }
+
+ @Test
+ public void testDeleteTopic() {
+ topicService.deleteTopic("java");
+ List topics = topicService.getAllTopics();
+ assertEquals(2, topics.size());
+ assertTrue(topics.stream().noneMatch(t -> t.getId().equals("java")));
+ }
+
+ @Test
+ public void testDeleteTopicNonExisting() {
+ int sizeBefore = topicService.getAllTopics().size();
+ topicService.deleteTopic("nonexistent");
+ assertEquals(sizeBefore, topicService.getAllTopics().size());
+ }
+
+ @Test
+ public void testFilterMinimumLengthForId() {
+ List filtered = topicService.filterMinimumLengthForId(4);
+ assertNotNull(filtered);
+ assertTrue(filtered.size() > 0);
+ filtered.forEach(t -> assertTrue(t.getId().length() > 4));
+ }
+
+ @Test
+ public void testFilterMinimumLengthForIdNoResults() {
+ List filtered = topicService.filterMinimumLengthForId(100);
+ assertNotNull(filtered);
+ assertEquals(0, filtered.size());
+ }
+
+ @Test
+ public void testSortTopicsWithID() {
+ List sorted = topicService.sortTopicsWithID();
+ assertNotNull(sorted);
+ assertEquals(3, sorted.size());
+ assertEquals("java", sorted.get(0).getId());
+ assertEquals("javascript", sorted.get(1).getId());
+ assertEquals("spring", sorted.get(2).getId());
+ }
+
+ @Test
+ public void testReturnAllTopicIDWithStringSlicing() {
+ String result = topicService.returnAllTopicIDWithStringSlicing();
+ assertNotNull(result);
+ assertTrue(result.contains("spring"));
+ assertTrue(result.contains("java"));
+ assertTrue(result.contains(":"));
+ }
+
+ @Test
+ public void testMakeDistinctAndSortCharacters() {
+ String result = topicService.makeDistinctAndSortCharacters("hello:world");
+ assertNotNull(result);
+ assertFalse(result.isEmpty());
+ // Should have distinct characters sorted
+ for (int i = 1; i < result.length(); i++) {
+ assertTrue(result.charAt(i - 1) <= result.charAt(i));
+ }
+ }
+
+ @Test
+ public void testSplitAllIdWithColonSelectIDWithJavaKeywordThenSortThenJoin() {
+ String input = "spring:java:javascript:python";
+ String result = topicService.splitAllIdWithColonSelectIDWithJavaKeywordThenSortThenJoin(input);
+ assertNotNull(result);
+ assertTrue(result.contains("java"));
+ assertTrue(result.contains("javascript"));
+ assertFalse(result.contains("spring"));
+ assertFalse(result.contains("python"));
+ }
+
+ @Test
+ public void testSplitAllIdWithNoJavaKeyword() {
+ String input = "spring:python:ruby";
+ String result = topicService.splitAllIdWithColonSelectIDWithJavaKeywordThenSortThenJoin(input);
+ assertNotNull(result);
+ assertEquals("", result);
+ }
+
+ @Test
+ public void testFindIdHavingCharacter() {
+ String result = topicService.findIdHavingCharacter();
+ assertNotNull(result);
+ assertTrue(result.contains("spring"));
+ }
+
+ @Test
+ public void testFindAllFilesInPathAndSort() {
+ String result = topicService.findAllFilesInPathAndSort();
+ assertNotNull(result);
+ }
+
+ @Test
+ public void testFindParticularFileInPathAndSort() {
+ String result = topicService.findParticularFileInPathAndSort();
+ assertNotNull(result);
+ }
+
+ @Test
+ public void testFindParticularFileInPathAndSortWithWalkFunction() {
+ String result = topicService.findParticularFileInPathAndSortWithWalkFunction();
+ assertNotNull(result);
+ }
+
+ @Test
+ public void testReadFileWithStreamFunction() {
+ 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..6ef04a7
--- /dev/null
+++ b/src/test/resources/application.properties
@@ -0,0 +1 @@
+spring.main.web-application-type=servlet