diff --git a/spring-mvc-java/pom.xml b/spring-mvc-java/pom.xml index 9c6f9b57a8..068824eba0 100644 --- a/spring-mvc-java/pom.xml +++ b/spring-mvc-java/pom.xml @@ -141,6 +141,13 @@ com.google.code.gson gson + + + + com.rometools + rome + ${rome.version} + @@ -283,7 +290,13 @@ 3.16-beta1 + + 1.10.0 + 2.2.4 + + + com.baeldung.app.Application diff --git a/spring-mvc-java/src/main/java/com/baeldung/rss/RssFeedApplication.java b/spring-mvc-java/src/main/java/com/baeldung/rss/RssFeedApplication.java new file mode 100644 index 0000000000..0f9de840d5 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/rss/RssFeedApplication.java @@ -0,0 +1,23 @@ +package com.baeldung.rss; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * Spring Boot launcher for an application which exposes an RSS Feed. + * + * @author Donato Rimenti + * + */ +@SpringBootApplication +public class RssFeedApplication { + + /** + * Launches a Spring Boot application which exposes an RSS Feed. + * + * @param args null + */ + public static void main(final String[] args) { + SpringApplication.run(RssFeedApplication.class, args); + } +} \ No newline at end of file diff --git a/spring-mvc-java/src/main/java/com/baeldung/rss/RssFeedController.java b/spring-mvc-java/src/main/java/com/baeldung/rss/RssFeedController.java new file mode 100644 index 0000000000..9d4e36dd5f --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/rss/RssFeedController.java @@ -0,0 +1,32 @@ +package com.baeldung.rss; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.servlet.View; + +/** + * REST Controller which returns an RSS Feed created by {@link RssFeedView}. + * + * @author Donato Rimenti + * + */ +@RestController +public class RssFeedController { + + /** + * View used by this controller. + */ + @Autowired + private RssFeedView view; + + /** + * Returns an RSS Feed created by {@link #view}. + * + * @return an RSS Feed + */ + @GetMapping("/rss") + public View getFeed() { + return view; + } +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/rss/RssFeedView.java b/spring-mvc-java/src/main/java/com/baeldung/rss/RssFeedView.java new file mode 100644 index 0000000000..c9b9c51bec --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/rss/RssFeedView.java @@ -0,0 +1,98 @@ +package com.baeldung.rss; + +import java.sql.Date; +import java.time.Instant; +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.view.feed.AbstractRssFeedView; + +import com.rometools.rome.feed.rss.Channel; +import com.rometools.rome.feed.rss.Item; + +/** + * View for a RSS feed. + * + * @author Donato Rimenti + */ +@Component +public class RssFeedView extends AbstractRssFeedView { + + /* + * (non-Javadoc) + * + * @see org.springframework.web.servlet.view.feed.AbstractFeedView# + * buildFeedMetadata(java.util.Map, com.rometools.rome.feed.WireFeed, + * javax.servlet.http.HttpServletRequest) + */ + @Override + protected void buildFeedMetadata(Map model, Channel feed, HttpServletRequest request) { + feed.setTitle("Baeldung RSS Feed"); + feed.setDescription("Learn how to program in Java"); + feed.setLink("http://www.baeldung.com"); + } + + /* + * (non-Javadoc) + * + * @see org.springframework.web.servlet.view.feed.AbstractRssFeedView# + * buildFeedItems(java.util.Map, javax.servlet.http.HttpServletRequest, + * javax.servlet.http.HttpServletResponse) + */ + @Override + protected List buildFeedItems(Map model, HttpServletRequest request, + HttpServletResponse response) { + + // Builds the single entries. + Item entryOne = new Item(); + entryOne.setTitle("JUnit 5 @Test Annotation"); + entryOne.setAuthor("donatohan.rimenti@gmail.com"); + entryOne.setLink("http://www.baeldung.com/junit-5-test-annotation"); + entryOne.setPubDate(Date.from(Instant.parse("2017-12-19T00:00:00Z"))); + + Item entryTwo = new Item(); + entryTwo.setTitle("Creating and Configuring Jetty 9 Server in Java"); + entryTwo.setAuthor("donatohan.rimenti@gmail.com"); + entryTwo.setLink("http://www.baeldung.com/jetty-java-programmatic"); + entryTwo.setPubDate(Date.from(Instant.parse("2018-01-23T00:00:00Z"))); + + Item entryThree = new Item(); + entryThree.setTitle("Flyweight Pattern in Java"); + entryThree.setAuthor("donatohan.rimenti@gmail.com"); + entryThree.setLink("http://www.baeldung.com/java-flyweight"); + entryThree.setPubDate(Date.from(Instant.parse("2018-02-01T00:00:00Z"))); + + Item entryFour = new Item(); + entryFour.setTitle("Multi-Swarm Optimization Algorithm in Java"); + entryFour.setAuthor("donatohan.rimenti@gmail.com"); + entryFour.setLink("http://www.baeldung.com/java-multi-swarm-algorithm"); + entryFour.setPubDate(Date.from(Instant.parse("2018-03-09T00:00:00Z"))); + + Item entryFive = new Item(); + entryFive.setTitle("A Simple Tagging Implementation with MongoDB"); + entryFive.setAuthor("donatohan.rimenti@gmail.com"); + entryFive.setLink("http://www.baeldung.com/mongodb-tagging"); + entryFive.setPubDate(Date.from(Instant.parse("2018-03-27T00:00:00Z"))); + + Item entrySix = new Item(); + entrySix.setTitle("Double-Checked Locking with Singleton"); + entrySix.setAuthor("donatohan.rimenti@gmail.com"); + entrySix.setLink("http://www.baeldung.com/java-singleton-double-checked-locking"); + entrySix.setPubDate(Date.from(Instant.parse("2018-04-23T00:00:00Z"))); + + Item entrySeven = new Item(); + entrySeven.setTitle("Introduction to Dagger 2"); + entrySeven.setAuthor("donatohan.rimenti@gmail.com"); + entrySeven.setLink("http://www.baeldung.com/dagger-2"); + entrySeven.setPubDate(Date.from(Instant.parse("2018-06-30T00:00:00Z"))); + + // Creates the feed. + return Arrays.asList(entryOne, entryTwo, entryThree, entryFour, entryFive, entrySix, entrySeven); + } + +} \ No newline at end of file diff --git a/spring-mvc-java/src/test/java/com/baeldung/rss/RssFeedUnitTest.java b/spring-mvc-java/src/test/java/com/baeldung/rss/RssFeedUnitTest.java new file mode 100644 index 0000000000..f5524c36fe --- /dev/null +++ b/spring-mvc-java/src/test/java/com/baeldung/rss/RssFeedUnitTest.java @@ -0,0 +1,62 @@ +package com.baeldung.rss; + +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +/** + * Test for {@link RssFeedApplication}. + * + * @author Donato Rimenti + * + */ +@RunWith(SpringJUnit4ClassRunner.class) +@SpringBootTest(classes = RssFeedApplication.class) +public class RssFeedUnitTest { + + /** + * Application context. + */ + @Autowired + private WebApplicationContext context; + + /** + * Mock to perform tests on Spring Web Controller. + */ + private MockMvc mvc; + + /** + * Sets the test up. + */ + @Before + public void setup() { + mvc = MockMvcBuilders.webAppContextSetup(context).build(); + } + + /** + * Calls the RSS feed endpoint and checks that the result matches an + * expected one. + * + * @throws Exception + */ + @Test + public void givenRssFeed_whenComparedWithExisting_thenEquals() throws Exception { + // The expected response. + String expectedResult = " Baeldung RSS Feed http://www.baeldung.com Learn how to program in Java JUnit 5 @Test Annotation http://www.baeldung.com/junit-5-test-annotation Tue, 19 Dec 2017 00:00:00 GMT donatohan.rimenti@gmail.com Creating and Configuring Jetty 9 Server in Java http://www.baeldung.com/jetty-java-programmatic Tue, 23 Jan 2018 00:00:00 GMT donatohan.rimenti@gmail.com Flyweight Pattern in Java http://www.baeldung.com/java-flyweight Thu, 01 Feb 2018 00:00:00 GMT donatohan.rimenti@gmail.com Multi-Swarm Optimization Algorithm in Java http://www.baeldung.com/java-multi-swarm-algorithm Fri, 09 Mar 2018 00:00:00 GMT donatohan.rimenti@gmail.com A Simple Tagging Implementation with MongoDB http://www.baeldung.com/mongodb-tagging Tue, 27 Mar 2018 00:00:00 GMT donatohan.rimenti@gmail.com Double-Checked Locking with Singleton http://www.baeldung.com/java-singleton-double-checked-locking Mon, 23 Apr 2018 00:00:00 GMT donatohan.rimenti@gmail.com Introduction to Dagger 2 http://www.baeldung.com/dagger-2 Sat, 30 Jun 2018 00:00:00 GMT donatohan.rimenti@gmail.com "; + + // Performs a post against the RSS feed endpoint and checks that the + // result is equals to the expected one. + mvc.perform(MockMvcRequestBuilders.get("/rss")).andExpect(status().isOk()) + .andExpect(content().xml(expectedResult)); + } + +}