Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
2163d9b672
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.streamApi;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class JoinerSplitter {
|
||||
|
||||
public static String join ( String[] arrayOfString ) {
|
||||
return Arrays.asList(arrayOfString)
|
||||
.stream()
|
||||
.map(x -> x)
|
||||
.collect(Collectors.joining(","));
|
||||
}
|
||||
|
||||
public static List<String> split ( String str ) {
|
||||
return Stream.of(str.split(","))
|
||||
.map (elem -> new String(elem))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package com.baeldung.stream;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.streamApi.JoinerSplitter;
|
||||
|
||||
public class JoinerSplitterTest {
|
||||
|
||||
@Test
|
||||
public void provided_array_convert_to_stream_and_convert_to_string() {
|
||||
String[] programming_languages = {"java", "python", "nodejs", "ruby"};
|
||||
String expectation = "java,python,nodejs,ruby";
|
||||
|
||||
String result = JoinerSplitter.join(programming_languages);
|
||||
assertEquals(result, expectation);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void provided_list_convert_to_stream_and_convert_to_list() {
|
||||
String programming_languages = "java,python,nodejs,ruby";
|
||||
|
||||
List<String> expectation = new ArrayList<String>();
|
||||
expectation.add("java");
|
||||
expectation.add("python");
|
||||
expectation.add("nodejs");
|
||||
expectation.add("ruby");
|
||||
|
||||
List<String> result = JoinerSplitter.split(programming_languages);
|
||||
|
||||
assertEquals(result, expectation);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>spring-boot</artifactId>
|
||||
|
@ -12,8 +12,8 @@
|
|||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.4.2.RELEASE</version>
|
||||
<relativePath /> <!-- lookup parent from repository -->
|
||||
<version>1.5.1.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -24,6 +24,26 @@
|
|||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-tomcat</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.geronimo.specs</groupId>
|
||||
<artifactId>geronimo-osgi-locator</artifactId>
|
||||
<version>1.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.geronimo.components</groupId>
|
||||
<artifactId>geronimo-jaspi</artifactId>
|
||||
<version>2.0.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
|
@ -87,6 +107,20 @@
|
|||
<artifactId>jquery</artifactId>
|
||||
<version>${jquery.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.tomee</groupId>
|
||||
<artifactId>arquillian-tomee-embedded</artifactId>
|
||||
<version>${arquillian-tomee-embedded.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.tomee</groupId>
|
||||
<artifactId>javaee-api</artifactId>
|
||||
<version>${tomee-javaee-api.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -166,7 +200,7 @@
|
|||
</executions>
|
||||
<configuration>
|
||||
<systemPropertyVariables>
|
||||
<test.mime>json</test.mime>
|
||||
<test.mime>json</test.mime>
|
||||
</systemPropertyVariables>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
@ -186,6 +220,8 @@
|
|||
<jquery.version>3.1.1</jquery.version>
|
||||
<bootstrap.version>3.3.7-1</bootstrap.version>
|
||||
<subethasmtp.version>3.1.7</subethasmtp.version>
|
||||
<arquillian-tomee-embedded.version>7.0.2</arquillian-tomee-embedded.version>
|
||||
<tomee-javaee-api.version>7.0-1</tomee-javaee-api.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
package com.baeldung.annotation.servletcomponentscan;
|
||||
|
||||
import javax.enterprise.context.ApplicationScoped;
|
||||
import javax.enterprise.context.Initialized;
|
||||
import javax.enterprise.event.Observes;
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
@ApplicationScoped
|
||||
public class JavaEEApp {
|
||||
|
||||
private ServletContext context;
|
||||
|
||||
/**
|
||||
* act as a servletContext provider
|
||||
*/
|
||||
private void setContext(@Observes @Initialized(ApplicationScoped.class) final ServletContext context) {
|
||||
if (this.context != null) {
|
||||
throw new IllegalStateException("app context started twice");
|
||||
}
|
||||
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
public ServletContext getContext() {
|
||||
return context;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package com.baeldung.annotation.servletcomponentscan;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.web.servlet.ServletComponentScan;
|
||||
|
||||
/**
|
||||
* using the following annotations are equivalent:
|
||||
* <ul><li>
|
||||
* <code>@ServletComponentScan</code>
|
||||
* </li><li>
|
||||
* <code>@ServletComponentScan(basePackages = "com.baeldung.annotation.servletcomponentscan.javaee")</code>
|
||||
* </li><li>
|
||||
* <code>@ServletComponentScan(basePackageClasses = {AttrListener.class, HelloFilter.class, HelloServlet.class, EchoServlet.class})</code>
|
||||
* </li></ul>
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@ServletComponentScan("com.baeldung.annotation.servletcomponentscan.javaee")
|
||||
public class SpringBootAnnotatedApp {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringBootAnnotatedApp.class, args);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.annotation.servletcomponentscan;
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
@SpringBootApplication
|
||||
@ComponentScan(basePackages = "com.baeldung.annotation.servletcomponentscan.javaee")
|
||||
public class SpringBootPlainApp {
|
||||
|
||||
public static void main(String[] args) {
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.annotation.servletcomponentscan.javaee;
|
||||
|
||||
import javax.servlet.ServletContextEvent;
|
||||
import javax.servlet.ServletContextListener;
|
||||
import javax.servlet.annotation.WebListener;
|
||||
|
||||
@WebListener
|
||||
public class AttrListener implements ServletContextListener {
|
||||
|
||||
@Override
|
||||
public void contextInitialized(ServletContextEvent servletContextEvent) {
|
||||
servletContextEvent
|
||||
.getServletContext()
|
||||
.setAttribute("servlet-context-attr", "test");
|
||||
System.out.println("context init");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void contextDestroyed(ServletContextEvent servletContextEvent) {
|
||||
System.out.println("context destroy");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package com.baeldung.annotation.servletcomponentscan.javaee;
|
||||
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.CopyOption;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
|
||||
@WebServlet(name = "echo servlet", urlPatterns = "/echo")
|
||||
public class EchoServlet extends HttpServlet {
|
||||
|
||||
@Override
|
||||
public void doPost(HttpServletRequest request, HttpServletResponse response) {
|
||||
try {
|
||||
Path path = File
|
||||
.createTempFile("echo", "tmp")
|
||||
.toPath();
|
||||
Files.copy(request.getInputStream(), path, StandardCopyOption.REPLACE_EXISTING);
|
||||
Files.copy(path, response.getOutputStream());
|
||||
Files.delete(path);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.baeldung.annotation.servletcomponentscan.javaee;
|
||||
|
||||
import javax.servlet.*;
|
||||
import javax.servlet.annotation.WebFilter;
|
||||
import javax.servlet.annotation.WebInitParam;
|
||||
import java.io.IOException;
|
||||
|
||||
@WebFilter(urlPatterns = "/hello", description = "a filter for hello servlet", initParams = { @WebInitParam(name = "msg", value = "filtering ") }, filterName = "hello filter", servletNames = { "echo servlet" })
|
||||
public class HelloFilter implements Filter {
|
||||
|
||||
private FilterConfig filterConfig;
|
||||
|
||||
@Override
|
||||
public void init(FilterConfig filterConfig) throws ServletException {
|
||||
System.out.println("filter init");
|
||||
this.filterConfig = filterConfig;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
|
||||
servletResponse
|
||||
.getOutputStream()
|
||||
.print(filterConfig.getInitParameter("msg"));
|
||||
filterChain.doFilter(servletRequest, servletResponse);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
System.out.println("filter destroy");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package com.baeldung.annotation.servletcomponentscan.javaee;
|
||||
|
||||
import javax.servlet.ServletConfig;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.annotation.WebInitParam;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
@WebServlet(urlPatterns = "/hello", initParams = { @WebInitParam(name = "msg", value = "hello")})
|
||||
public class HelloServlet extends HttpServlet {
|
||||
|
||||
private ServletConfig servletConfig;
|
||||
|
||||
@Override
|
||||
public void init(ServletConfig servletConfig){
|
||||
this.servletConfig = servletConfig;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doGet(HttpServletRequest request, HttpServletResponse response) {
|
||||
try {
|
||||
response
|
||||
.getOutputStream()
|
||||
.write(servletConfig.getInitParameter("msg").getBytes());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung;
|
||||
package com.baeldung.webjar;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
|
@ -1,7 +1,8 @@
|
|||
package com.baeldung;
|
||||
package com.baeldung.webjar;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
@SpringBootApplication
|
||||
public class WebjarsdemoApplication {
|
|
@ -1,8 +1,8 @@
|
|||
package org.baeldung.common.error;
|
||||
|
||||
import javax.servlet.Servlet;
|
||||
import org.springframework.boot.web.servlet.ServletRegistrationBean;
|
||||
|
||||
import org.springframework.boot.context.embedded.ServletRegistrationBean;
|
||||
import javax.servlet.Servlet;
|
||||
|
||||
public class SpringHelloServletRegistrationBean extends ServletRegistrationBean {
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@ package org.baeldung.common.properties;
|
|||
|
||||
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
|
||||
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
|
||||
import org.springframework.boot.context.embedded.ErrorPage;
|
||||
import org.springframework.boot.web.servlet.ErrorPage;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
|
|
|
@ -0,0 +1,88 @@
|
|||
package com.baeldung.annotation.servletcomponentscan;
|
||||
|
||||
import com.baeldung.annotation.servletcomponentscan.javaee.AttrListener;
|
||||
import com.baeldung.annotation.servletcomponentscan.javaee.EchoServlet;
|
||||
import com.baeldung.annotation.servletcomponentscan.javaee.HelloFilter;
|
||||
import com.baeldung.annotation.servletcomponentscan.javaee.HelloServlet;
|
||||
import org.jboss.arquillian.container.test.api.Deployment;
|
||||
import org.jboss.arquillian.container.test.api.RunAsClient;
|
||||
import org.jboss.arquillian.junit.Arquillian;
|
||||
import org.jboss.arquillian.test.api.ArquillianResource;
|
||||
import org.jboss.shrinkwrap.api.ShrinkWrap;
|
||||
import org.jboss.shrinkwrap.api.spec.WebArchive;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.servlet.FilterRegistration;
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.ws.rs.client.Client;
|
||||
import javax.ws.rs.client.ClientBuilder;
|
||||
import javax.ws.rs.client.Entity;
|
||||
import javax.ws.rs.client.WebTarget;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@RunWith(Arquillian.class)
|
||||
public class JavaEEAppTest {
|
||||
|
||||
@Deployment
|
||||
public static WebArchive createDeployment() {
|
||||
return ShrinkWrap
|
||||
.create(WebArchive.class)
|
||||
.addClass(JavaEEApp.class)
|
||||
.addClasses(AttrListener.class, HelloFilter.class, HelloServlet.class, EchoServlet.class);
|
||||
}
|
||||
|
||||
@Inject private ServletContext servletContext;
|
||||
|
||||
@Test
|
||||
public void givenServletContextListener_whenAccessSpecialAttrs_thenFound() throws MalformedURLException {
|
||||
assertNotNull(servletContext);
|
||||
assertNotNull(servletContext.getAttribute("servlet-context-attr"));
|
||||
assertEquals("test", servletContext.getAttribute("servlet-context-attr"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenServletContext_whenCheckHelloFilterMappings_thenCorrect() throws MalformedURLException {
|
||||
assertNotNull(servletContext);
|
||||
FilterRegistration filterRegistration = servletContext.getFilterRegistration("hello filter");
|
||||
|
||||
assertNotNull(filterRegistration);
|
||||
assertTrue(filterRegistration
|
||||
.getServletNameMappings()
|
||||
.contains("echo servlet"));
|
||||
}
|
||||
|
||||
@ArquillianResource private URL base;
|
||||
|
||||
@Test
|
||||
@RunAsClient
|
||||
public void givenFilterAndServlet_whenGetHello_thenRespondFilteringHello() throws MalformedURLException {
|
||||
Client client = ClientBuilder.newClient();
|
||||
WebTarget target = client.target(URI.create(new URL(base, "hello").toExternalForm()));
|
||||
Response response = target
|
||||
.request()
|
||||
.get();
|
||||
|
||||
assertEquals("filtering hello", response.readEntity(String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@RunAsClient
|
||||
public void givenFilterAndServlet_whenPostEcho_thenEchoFiltered() throws MalformedURLException {
|
||||
Client client = ClientBuilder.newClient();
|
||||
WebTarget target = client.target(URI.create(new URL(base, "echo").toExternalForm()));
|
||||
Response response = target
|
||||
.request()
|
||||
.post(Entity.entity("echo", MediaType.TEXT_PLAIN_TYPE));
|
||||
|
||||
assertEquals("filtering echo", response.readEntity(String.class));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,65 @@
|
|||
package com.baeldung.annotation.servletcomponentscan;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
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.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import javax.servlet.FilterRegistration;
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = SpringBootAnnotatedApp.class)
|
||||
@AutoConfigureMockMvc
|
||||
@TestPropertySource(properties = { "security.basic.enabled=false", "server.tomcat.additional-tld-skip-patterns=tomee-*.jar,tomcat-*.jar,openejb-*.jar,cxf-*.jar,activemq-*.jar" })
|
||||
public class SpringBootWithServletComponentTest {
|
||||
|
||||
@Autowired private ServletContext servletContext;
|
||||
|
||||
@Test
|
||||
public void givenServletContext_whenAccessAttrs_thenFoundAttrsPutInServletListner() {
|
||||
assertNotNull(servletContext);
|
||||
assertNotNull(servletContext.getAttribute("servlet-context-attr"));
|
||||
assertEquals("test", servletContext.getAttribute("servlet-context-attr"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenServletContext_whenCheckHelloFilterMappings_thenCorrect() {
|
||||
assertNotNull(servletContext);
|
||||
FilterRegistration filterRegistration = servletContext.getFilterRegistration("hello filter");
|
||||
|
||||
assertNotNull(filterRegistration);
|
||||
assertTrue(filterRegistration
|
||||
.getServletNameMappings()
|
||||
.contains("echo servlet"));
|
||||
}
|
||||
|
||||
@Autowired private TestRestTemplate restTemplate;
|
||||
|
||||
@Test
|
||||
public void givenServletFilter_whenGetHello_thenRequestFiltered() {
|
||||
ResponseEntity<String> responseEntity = this.restTemplate.getForEntity("/hello", String.class);
|
||||
assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
|
||||
assertEquals("filtering hello", responseEntity.getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFilterAndServlet_whenPostEcho_thenEchoFiltered() {
|
||||
ResponseEntity<String> responseEntity = this.restTemplate.postForEntity("/echo", "echo", String.class);
|
||||
assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
|
||||
assertEquals("filtering echo", responseEntity.getBody());
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
package com.baeldung.annotation.servletcomponentscan;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
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.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import javax.servlet.FilterRegistration;
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = SpringBootPlainApp.class)
|
||||
@AutoConfigureMockMvc
|
||||
@TestPropertySource(properties = { "security.basic.enabled=false", "server.tomcat.additional-tld-skip-patterns=tomee-*.jar,tomcat-*.jar,openejb-*.jar,cxf-*.jar,activemq-*.jar" })
|
||||
public class SpringBootWithoutServletComponentTest {
|
||||
|
||||
@Autowired private ServletContext servletContext;
|
||||
|
||||
@Autowired private TestRestTemplate restTemplate;
|
||||
|
||||
@Test
|
||||
public void givenServletContext_whenAccessAttrs_thenNotFound() {
|
||||
assertNull(servletContext.getAttribute("servlet-context-attr"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenServletFilter_whenGetHello_thenEndpointNotFound() {
|
||||
ResponseEntity<String> responseEntity = this.restTemplate.getForEntity("/hello", String.class);
|
||||
assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenServletContext_whenCheckFilterMappings_thenEmpty() {
|
||||
assertNotNull(servletContext);
|
||||
FilterRegistration filterRegistration = servletContext.getFilterRegistration("hello filter");
|
||||
|
||||
assertNull(filterRegistration);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -6,6 +6,7 @@ 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.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
|
@ -17,6 +18,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
|||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
@AutoConfigureMockMvc
|
||||
@TestPropertySource(properties = { "security.basic.enabled=false" })
|
||||
public class AppLiveTest {
|
||||
|
||||
@Autowired
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
package com.baeldung;
|
||||
package com.baeldung.webjar;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = WebjarsdemoApplication.class)
|
||||
@SpringBootTest(classes = WebjarsdemoApplication.class)
|
||||
@WebAppConfiguration
|
||||
public class WebjarsdemoApplicationIntegrationTest {
|
||||
|
|
@ -9,7 +9,7 @@ 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.SpringApplicationConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
|
@ -22,7 +22,7 @@ import org.springframework.web.context.WebApplicationContext;
|
|||
import java.nio.charset.Charset;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = Application.class)
|
||||
@SpringBootTest(classes = Application.class)
|
||||
@WebAppConfiguration
|
||||
public class SpringBootApplicationIntegrationTest {
|
||||
@Autowired
|
||||
|
|
|
@ -5,14 +5,14 @@ import org.baeldung.repository.GenericEntityRepository;
|
|||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = Application.class)
|
||||
@SpringBootTest(classes = Application.class)
|
||||
public class SpringBootJPAIntegrationTest {
|
||||
@Autowired
|
||||
private GenericEntityRepository genericEntityRepository;
|
||||
|
|
|
@ -5,7 +5,7 @@ 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.SpringApplicationConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
@ -23,7 +23,7 @@ import static org.junit.Assert.assertEquals;
|
|||
import static org.junit.Assert.assertThat;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = Application.class)
|
||||
@SpringBootTest(classes = Application.class)
|
||||
public class SpringBootMailIntegrationTest {
|
||||
@Autowired
|
||||
private JavaMailSender javaMailSender;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
spring.application.name=resource
|
||||
spring.application.name=book-service
|
||||
server.port=8083
|
||||
|
||||
resource.returnString=hello cloud
|
|
@ -6,9 +6,13 @@ eureka.client.registryFetchIntervalSeconds = 5
|
|||
|
||||
management.security.sessions=always
|
||||
|
||||
zuul.routes.resource.path=/resource/**
|
||||
zuul.routes.resource.sensitive-headers=Set-Cookie,Authorization
|
||||
hystrix.command.resource.execution.isolation.thread.timeoutInMilliseconds=600000
|
||||
zuul.routes.book-service.path=/book-service/**
|
||||
zuul.routes.book-service.sensitive-headers=Set-Cookie,Authorization
|
||||
hystrix.command.book-service.execution.isolation.thread.timeoutInMilliseconds=600000
|
||||
|
||||
zuul.routes.rating-service.path=/rating-service/**
|
||||
zuul.routes.rating-service.sensitive-headers=Set-Cookie,Authorization
|
||||
hystrix.command.rating-service.execution.isolation.thread.timeoutInMilliseconds=600000
|
||||
|
||||
zuul.routes.discovery.path=/discovery/**
|
||||
zuul.routes.discovery.sensitive-headers=Set-Cookie,Authorization
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
spring.application.name=rating-service
|
||||
server.port=8084
|
||||
|
||||
resource.returnString=hello cloud
|
||||
resource.user.returnString=hello cloud user
|
||||
resource.admin.returnString=hello cloud admin
|
||||
|
||||
eureka.client.region = default
|
||||
eureka.client.registryFetchIntervalSeconds = 5
|
||||
|
||||
management.security.sessions=never
|
||||
|
||||
logging.level.org.springframework.web.=debug
|
||||
logging.level.org.springframework.security=debug
|
||||
|
||||
spring.redis.host=localhost
|
||||
spring.redis.port=6379
|
|
@ -22,14 +22,14 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
|||
@Override
|
||||
protected void configure(HttpSecurity http) throws Exception {
|
||||
http.authorizeRequests()
|
||||
.antMatchers("/resource/hello/cloud").permitAll()
|
||||
.antMatchers("/book-service/books").permitAll()
|
||||
.antMatchers("/eureka/**").hasRole("ADMIN")
|
||||
.anyRequest().authenticated()
|
||||
.and()
|
||||
.formLogin()
|
||||
.and()
|
||||
.logout().permitAll()
|
||||
.logoutSuccessUrl("/resource/hello/cloud").permitAll()
|
||||
.logoutSuccessUrl("/book-service/books").permitAll()
|
||||
.and()
|
||||
.csrf()
|
||||
.disable();
|
||||
|
|
|
@ -14,12 +14,12 @@ public class GatewayApplicationLiveTest {
|
|||
TestRestTemplate testRestTemplate = new TestRestTemplate();
|
||||
String testUrl = "http://localhost:8080";
|
||||
|
||||
ResponseEntity<String> response = testRestTemplate.getForEntity(testUrl + "/resource/hello/cloud", String.class);
|
||||
ResponseEntity<String> response = testRestTemplate.getForEntity(testUrl + "/book-service/books", String.class);
|
||||
Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
Assert.assertEquals("hello cloud", response.getBody());
|
||||
Assert.assertNotNull(response.getBody());
|
||||
|
||||
//try the protected resource and confirm the redirect to login
|
||||
response = testRestTemplate.getForEntity(testUrl + "/resource/hello/user", String.class);
|
||||
response = testRestTemplate.getForEntity(testUrl + "/book-service/books/1", String.class);
|
||||
Assert.assertEquals(HttpStatus.FOUND, response.getStatusCode());
|
||||
Assert.assertEquals("http://localhost:8080/login", response.getHeaders().get("Location").get(0));
|
||||
|
||||
|
@ -36,12 +36,12 @@ public class GatewayApplicationLiveTest {
|
|||
HttpEntity<String> httpEntity = new HttpEntity<>(headers);
|
||||
|
||||
//request the protected resource
|
||||
response = testRestTemplate.exchange(testUrl + "/resource/hello/user", HttpMethod.GET, httpEntity, String.class);
|
||||
response = testRestTemplate.exchange(testUrl + "/book-service/books/1", HttpMethod.GET, httpEntity, String.class);
|
||||
Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
Assert.assertEquals("hello cloud user", response.getBody());
|
||||
Assert.assertNotNull(response.getBody());
|
||||
|
||||
//request the admin protected resource to determine it is still protected
|
||||
response = testRestTemplate.exchange(testUrl + "/resource/hello/admin", HttpMethod.GET, httpEntity, String.class);
|
||||
response = testRestTemplate.exchange(testUrl + "/rating-service/ratings/all", HttpMethod.GET, httpEntity, String.class);
|
||||
Assert.assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode());
|
||||
|
||||
//login as the admin
|
||||
|
@ -57,9 +57,9 @@ public class GatewayApplicationLiveTest {
|
|||
httpEntity = new HttpEntity<>(headers);
|
||||
|
||||
//request the protected resource
|
||||
response = testRestTemplate.exchange(testUrl + "/resource/hello/admin", HttpMethod.GET, httpEntity, String.class);
|
||||
response = testRestTemplate.exchange(testUrl + "/rating-service/ratings/all", HttpMethod.GET, httpEntity, String.class);
|
||||
Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
Assert.assertEquals("hello cloud admin", response.getBody());
|
||||
Assert.assertNotNull(response.getBody());
|
||||
|
||||
//request the discovery resources as the admin
|
||||
response = testRestTemplate.exchange(testUrl + "/discovery", HttpMethod.GET, httpEntity, String.class);
|
||||
|
|
|
@ -14,7 +14,8 @@
|
|||
<module>config</module>
|
||||
<module>discovery</module>
|
||||
<module>gateway</module>
|
||||
<module>resource</module>
|
||||
<module>svc-book</module>
|
||||
<module>svc-rating</module>
|
||||
</modules>
|
||||
|
||||
|
||||
|
|
|
@ -1,41 +0,0 @@
|
|||
package com.baeldung.spring.cloud.bootstrap.resource;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableEurekaClient
|
||||
@RestController
|
||||
public class ResourceApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ResourceApplication.class, args);
|
||||
}
|
||||
|
||||
@Value("${resource.returnString}")
|
||||
private String returnString;
|
||||
|
||||
@Value("${resource.user.returnString}")
|
||||
private String userReturnString;
|
||||
|
||||
@Value("${resource.admin.returnString}")
|
||||
private String adminReturnString;
|
||||
|
||||
@RequestMapping("/hello/cloud")
|
||||
public String getString() {
|
||||
return returnString;
|
||||
}
|
||||
|
||||
@RequestMapping("/hello/user")
|
||||
public String getUserString() {
|
||||
return userReturnString;
|
||||
}
|
||||
|
||||
@RequestMapping("/hello/admin")
|
||||
public String getAdminString() {
|
||||
return adminReturnString;
|
||||
}
|
||||
}
|
|
@ -4,7 +4,8 @@
|
|||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>resource</artifactId>
|
||||
<groupId>com.baeldung.spring.cloud</groupId>
|
||||
<artifactId>svc-book</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
|
@ -0,0 +1,40 @@
|
|||
package com.baeldung.spring.cloud.bootstrap.svcbook;
|
||||
|
||||
public class Book {
|
||||
private Long id;
|
||||
private String author;
|
||||
private String title;
|
||||
|
||||
public Book(Long id, String title, String author) {
|
||||
this.id = id;
|
||||
this.author = author;
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public Book() {
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package com.baeldung.spring.cloud.bootstrap.svcbook;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableEurekaClient
|
||||
@RestController
|
||||
@RequestMapping("/books")
|
||||
public class BookServiceApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(BookServiceApplication.class, args);
|
||||
}
|
||||
|
||||
private List<Book> bookList = Arrays.asList(
|
||||
new Book(1L, "Baeldung goes to the market", "Tim Schimandle"),
|
||||
new Book(2L, "Baeldung goes to the park", "Slavisa")
|
||||
);
|
||||
|
||||
@GetMapping("")
|
||||
public List<Book> findAllBooks() {
|
||||
return bookList;
|
||||
}
|
||||
|
||||
@GetMapping("/{bookId}")
|
||||
public Book findBook(@PathVariable Long bookId) {
|
||||
return bookList.stream().filter(b -> b.getId().equals(bookId)).findFirst().orElse(null);
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.spring.cloud.bootstrap.resource;
|
||||
package com.baeldung.spring.cloud.bootstrap.svcbook;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
@ -22,9 +22,8 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
|||
http.httpBasic()
|
||||
.disable()
|
||||
.authorizeRequests()
|
||||
.antMatchers("/hello/cloud").permitAll()
|
||||
.antMatchers("/hello/user").hasAnyRole("USER", "ADMIN")
|
||||
.antMatchers("/hello/admin").hasRole("ADMIN")
|
||||
.antMatchers("/books").permitAll()
|
||||
.antMatchers("/books/*").hasAnyRole("USER", "ADMIN")
|
||||
.anyRequest().authenticated()
|
||||
.and()
|
||||
.csrf()
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.spring.cloud.bootstrap.resource;
|
||||
package com.baeldung.spring.cloud.bootstrap.svcbook;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
|
|
@ -1,4 +1,4 @@
|
|||
spring.cloud.config.name=resource
|
||||
spring.cloud.config.name=book-service
|
||||
spring.cloud.config.discovery.service-id=config
|
||||
spring.cloud.config.discovery.enabled=true
|
||||
spring.cloud.config.username=configUser
|
|
@ -0,0 +1,85 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.baeldung.spring.cloud</groupId>
|
||||
<artifactId>svc-rating</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.4.2.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-config</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-eureka</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-security</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.session</groupId>
|
||||
<artifactId>spring-session</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring-cloud-dependencies.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<properties>
|
||||
<spring-cloud-dependencies.version>Brixton.SR7</spring-cloud-dependencies.version>
|
||||
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
|
||||
</properties>
|
||||
</project>
|
|
@ -0,0 +1,40 @@
|
|||
package com.baeldung.spring.cloud.bootstrap.svcrating;
|
||||
|
||||
public class Rating {
|
||||
private Long id;
|
||||
private Long bookId;
|
||||
private int stars;
|
||||
|
||||
public Rating() {
|
||||
}
|
||||
|
||||
public Rating(Long id, Long bookId, int stars) {
|
||||
this.id = id;
|
||||
this.bookId = bookId;
|
||||
this.stars = stars;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getBookId() {
|
||||
return bookId;
|
||||
}
|
||||
|
||||
public void setBookId(Long bookId) {
|
||||
this.bookId = bookId;
|
||||
}
|
||||
|
||||
public int getStars() {
|
||||
return stars;
|
||||
}
|
||||
|
||||
public void setStars(int stars) {
|
||||
this.stars = stars;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package com.baeldung.spring.cloud.bootstrap.svcrating;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableEurekaClient
|
||||
@RestController
|
||||
@RequestMapping("/ratings")
|
||||
public class RatingServiceApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(RatingServiceApplication.class, args);
|
||||
}
|
||||
|
||||
private List<Rating> ratingList = Arrays.asList(
|
||||
new Rating(1L, 1L, 2),
|
||||
new Rating(2L, 1L, 3),
|
||||
new Rating(3L, 2L, 4),
|
||||
new Rating(4L, 2L, 5)
|
||||
);
|
||||
|
||||
@GetMapping("")
|
||||
public List<Rating> findRatingsByBookId(@RequestParam Long bookId) {
|
||||
return bookId == null || bookId.equals(0L) ? Collections.EMPTY_LIST : ratingList.stream().filter(r -> r.getBookId().equals(bookId)).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@GetMapping("/all")
|
||||
public List<Rating> findAllRatings() {
|
||||
return ratingList;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.baeldung.spring.cloud.bootstrap.svcrating;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||
|
||||
@EnableWebSecurity
|
||||
@Configuration
|
||||
public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
|
||||
@Autowired
|
||||
public void configureGlobal1(AuthenticationManagerBuilder auth) throws Exception {
|
||||
//try in memory auth with no users to support the case that this will allow for users that are logged in to go anywhere
|
||||
auth.inMemoryAuthentication();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configure(HttpSecurity http) throws Exception {
|
||||
http.httpBasic()
|
||||
.disable()
|
||||
.authorizeRequests()
|
||||
.antMatchers("/ratings").hasRole("USER")
|
||||
.antMatchers("/ratings/all").hasRole("ADMIN")
|
||||
.anyRequest().authenticated()
|
||||
.and()
|
||||
.csrf()
|
||||
.disable();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.spring.cloud.bootstrap.svcrating;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
|
||||
import org.springframework.session.web.context.AbstractHttpSessionApplicationInitializer;
|
||||
|
||||
@Configuration
|
||||
@EnableRedisHttpSession
|
||||
public class SessionConfig extends AbstractHttpSessionApplicationInitializer {
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
spring.cloud.config.name=rating-service
|
||||
spring.cloud.config.discovery.service-id=config
|
||||
spring.cloud.config.discovery.enabled=true
|
||||
spring.cloud.config.username=configUser
|
||||
spring.cloud.config.password=configPassword
|
||||
|
||||
eureka.client.serviceUrl.defaultZone=http://discUser:discPassword@localhost:8082/eureka/
|
|
@ -0,0 +1,25 @@
|
|||
package org.baeldung.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
|
||||
|
||||
import com.mongodb.Mongo;
|
||||
import com.mongodb.MongoClient;
|
||||
|
||||
@Configuration
|
||||
@EnableMongoRepositories(basePackages = "org.baeldung.repository")
|
||||
public class SimpleMongoConfig {
|
||||
|
||||
@Bean
|
||||
public Mongo mongo() throws Exception {
|
||||
return new MongoClient("localhost");
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MongoTemplate mongoTemplate() throws Exception {
|
||||
return new MongoTemplate(mongo(), "test");
|
||||
}
|
||||
|
||||
}
|
|
@ -1,8 +1,10 @@
|
|||
package org.baeldung.mongotemplate;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.baeldung.config.MongoConfig;
|
||||
import org.baeldung.config.SimpleMongoConfig;
|
||||
import org.baeldung.model.User;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
|
@ -15,7 +17,7 @@ import org.springframework.test.context.ContextConfiguration;
|
|||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = MongoConfig.class)
|
||||
@ContextConfiguration(classes = SimpleMongoConfig.class)
|
||||
public class MongoTemplateProjectionLiveTest {
|
||||
|
||||
@Autowired
|
||||
|
@ -38,7 +40,7 @@ public class MongoTemplateProjectionLiveTest {
|
|||
mongoTemplate.insert(new User("John", 30));
|
||||
mongoTemplate.insert(new User("Ringo", 35));
|
||||
|
||||
Query query = new Query();
|
||||
final Query query = new Query();
|
||||
query.fields()
|
||||
.include("name");
|
||||
|
||||
|
@ -55,7 +57,7 @@ public class MongoTemplateProjectionLiveTest {
|
|||
mongoTemplate.insert(new User("John", 30));
|
||||
mongoTemplate.insert(new User("Ringo", 35));
|
||||
|
||||
Query query = new Query();
|
||||
final Query query = new Query();
|
||||
query.fields()
|
||||
.exclude("_id");
|
||||
|
||||
|
@ -64,7 +66,7 @@ public class MongoTemplateProjectionLiveTest {
|
|||
assertNull(user.getId());
|
||||
assertNotNull(user.getAge());
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -16,16 +16,15 @@ public class HibernateOneToManyAnnotationMain {
|
|||
public static void main(String[] args) {
|
||||
|
||||
Cart cart = new Cart();
|
||||
cart.setName("MyCart");
|
||||
|
||||
Items item1 = new Items("I10", 10, 1, cart);
|
||||
Items item2 = new Items("I20", 20, 2, cart);
|
||||
Items item1 = new Items(cart);
|
||||
Items item2 = new Items(cart);
|
||||
Set<Items> itemsSet = new HashSet<Items>();
|
||||
itemsSet.add(item1);
|
||||
itemsSet.add(item2);
|
||||
|
||||
cart.setItems(itemsSet);
|
||||
cart.setTotal(10 * 1 + 20 * 2);
|
||||
|
||||
|
||||
SessionFactory sessionFactory = null;
|
||||
Session session = null;
|
||||
|
|
|
@ -19,11 +19,6 @@ public class Cart {
|
|||
@Column(name = "cart_id")
|
||||
private long id;
|
||||
|
||||
@Column(name = "total")
|
||||
private double total;
|
||||
|
||||
@Column(name = "name")
|
||||
private String name;
|
||||
|
||||
@OneToMany(mappedBy = "cart")
|
||||
private Set<Items> items;
|
||||
|
@ -36,21 +31,6 @@ public class Cart {
|
|||
this.id = id;
|
||||
}
|
||||
|
||||
public double getTotal() {
|
||||
return total;
|
||||
}
|
||||
|
||||
public void setTotal(double total) {
|
||||
this.total = total;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Set<Items> getItems() {
|
||||
return items;
|
||||
|
|
|
@ -18,14 +18,6 @@ public class Items {
|
|||
@Column(name = "id")
|
||||
private long id;
|
||||
|
||||
@Column(name = "item_id")
|
||||
private String itemId;
|
||||
|
||||
@Column(name = "item_total")
|
||||
private double itemTotal;
|
||||
|
||||
@Column(name = "quantity")
|
||||
private int quantity;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "cart_id", nullable = false)
|
||||
|
@ -35,37 +27,10 @@ public class Items {
|
|||
public Items() {
|
||||
}
|
||||
|
||||
public Items(String itemId, double total, int qty, Cart c) {
|
||||
this.itemId = itemId;
|
||||
this.itemTotal = total;
|
||||
this.quantity = qty;
|
||||
public Items(Cart c) {
|
||||
this.cart = c;
|
||||
}
|
||||
|
||||
public String getItemId() {
|
||||
return itemId;
|
||||
}
|
||||
|
||||
public void setItemId(String itemId) {
|
||||
this.itemId = itemId;
|
||||
}
|
||||
|
||||
public double getItemTotal() {
|
||||
return itemTotal;
|
||||
}
|
||||
|
||||
public void setItemTotal(double itemTotal) {
|
||||
this.itemTotal = itemTotal;
|
||||
}
|
||||
|
||||
public int getQuantity() {
|
||||
return quantity;
|
||||
}
|
||||
|
||||
public void setQuantity(int quantity) {
|
||||
this.quantity = quantity;
|
||||
}
|
||||
|
||||
public Cart getCart() {
|
||||
return cart;
|
||||
}
|
||||
|
|
|
@ -1,16 +1,11 @@
|
|||
CREATE TABLE `Cart` (
|
||||
`cart_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
|
||||
`total` decimal(10,0) NOT NULL,
|
||||
`name` varchar(10) DEFAULT NULL,
|
||||
PRIMARY KEY (`cart_id`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
|
||||
|
||||
CREATE TABLE `Items` (
|
||||
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
|
||||
`cart_id` int(11) unsigned NOT NULL,
|
||||
`item_id` varchar(10) NOT NULL,
|
||||
`item_total` decimal(10,0) NOT NULL,
|
||||
`quantity` int(3) NOT NULL,
|
||||
PRIMARY KEY (`id`),
|
||||
KEY `cart_id` (`cart_id`),
|
||||
CONSTRAINT `items_ibfk_1` FOREIGN KEY (`cart_id`) REFERENCES `Cart` (`cart_id`)
|
||||
|
|
|
@ -63,9 +63,6 @@ public class HibernateOneToManyAnnotationMainTest {
|
|||
cartItems = cart.getItems();
|
||||
Assert.assertNull(cartItems);
|
||||
Items item1 = new Items();
|
||||
item1.setItemId("I10");
|
||||
item1.setItemTotal(10);
|
||||
item1.setQuantity(1);
|
||||
item1.setCart(cart);
|
||||
assertNotNull(item1);
|
||||
Set<Items> itemsSet = new HashSet<Items>();
|
||||
|
|
Loading…
Reference in New Issue