Optimize and refactor (#2302)

This commit is contained in:
Grzegorz Piwowarek 2017-07-21 13:37:49 +03:00 committed by GitHub
parent 4e95722017
commit f8bf4038f1
32 changed files with 220 additions and 270 deletions

View File

@ -14,7 +14,9 @@ import org.springframework.test.context.junit4.SpringRunner;
import javax.servlet.FilterRegistration; import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import static org.junit.Assert.*; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = SpringBootAnnotatedApp.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = SpringBootAnnotatedApp.class)
@ -22,7 +24,8 @@ import static org.junit.Assert.*;
@TestPropertySource(properties = {"security.basic.enabled=false"}) @TestPropertySource(properties = {"security.basic.enabled=false"})
public class SpringBootWithServletComponentIntegrationTest { public class SpringBootWithServletComponentIntegrationTest {
@Autowired private ServletContext servletContext; @Autowired
private ServletContext servletContext;
@Test @Test
public void givenServletContext_whenAccessAttrs_thenFoundAttrsPutInServletListner() { public void givenServletContext_whenAccessAttrs_thenFoundAttrsPutInServletListner() {
@ -42,7 +45,8 @@ public class SpringBootWithServletComponentIntegrationTest {
.contains("echo servlet")); .contains("echo servlet"));
} }
@Autowired private TestRestTemplate restTemplate; @Autowired
private TestRestTemplate restTemplate;
@Test @Test
public void givenServletFilter_whenGetHello_thenRequestFiltered() { public void givenServletFilter_whenGetHello_thenRequestFiltered() {
@ -59,7 +63,6 @@ public class SpringBootWithServletComponentIntegrationTest {
} }
} }

View File

@ -14,7 +14,9 @@ import org.springframework.test.context.junit4.SpringRunner;
import javax.servlet.FilterRegistration; import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import static org.junit.Assert.*; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = SpringBootPlainApp.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = SpringBootPlainApp.class)
@ -22,9 +24,11 @@ import static org.junit.Assert.*;
@TestPropertySource(properties = {"security.basic.enabled=false"}) @TestPropertySource(properties = {"security.basic.enabled=false"})
public class SpringBootWithoutServletComponentIntegrationTest { public class SpringBootWithoutServletComponentIntegrationTest {
@Autowired private ServletContext servletContext; @Autowired
private ServletContext servletContext;
@Autowired private TestRestTemplate restTemplate; @Autowired
private TestRestTemplate restTemplate;
@Test @Test
public void givenServletContext_whenAccessAttrs_thenNotFound() { public void givenServletContext_whenAccessAttrs_thenNotFound() {

View File

@ -1,5 +1,8 @@
package com.baeldung.autoconfiguration; package com.baeldung.autoconfiguration;
import com.baeldung.autoconfiguration.example.AutoconfigurationApplication;
import com.baeldung.autoconfiguration.example.MyUser;
import com.baeldung.autoconfiguration.example.MyUserRepository;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -7,10 +10,6 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.baeldung.autoconfiguration.example.AutoconfigurationApplication;
import com.baeldung.autoconfiguration.example.MyUser;
import com.baeldung.autoconfiguration.example.MyUserRepository;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = AutoconfigurationApplication.class) @SpringBootTest(classes = AutoconfigurationApplication.class)
@EnableJpaRepositories(basePackages = {"com.baeldung.autoconfiguration.example"}) @EnableJpaRepositories(basePackages = {"com.baeldung.autoconfiguration.example"})

View File

@ -1,15 +1,5 @@
package com.baeldung.displayallbeans; package com.baeldung.displayallbeans;
import static org.assertj.core.api.BDDAssertions.then;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -23,7 +13,15 @@ import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.WebApplicationContext;
import com.baeldung.displayallbeans.Application; import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static org.assertj.core.api.BDDAssertions.then;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

View File

@ -1,9 +1,5 @@
package com.baeldung.toggle; package com.baeldung.toggle;
import static org.junit.Assert.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -16,6 +12,10 @@ import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.WebApplicationContext;
import static org.junit.Assert.assertEquals;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = ToggleApplication.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = ToggleApplication.class)
@AutoConfigureMockMvc @AutoConfigureMockMvc

View File

@ -1,5 +1,6 @@
package com.baeldung.utils; package com.baeldung.utils;
import com.baeldung.utils.controller.UtilsController;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
@ -10,9 +11,6 @@ import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import com.baeldung.utils.controller.UtilsController;
public class UtilsControllerIntegrationTest { public class UtilsControllerIntegrationTest {
@InjectMocks @InjectMocks

View File

@ -6,11 +6,6 @@ import org.mockito.Mockito;
import org.springframework.messaging.simp.stomp.StompHeaders; import org.springframework.messaging.simp.stomp.StompHeaders;
import org.springframework.messaging.simp.stomp.StompSession; import org.springframework.messaging.simp.stomp.StompSession;
/**
* Test class for MyStompSessionHandler
* @author Kalyan
*
*/
public class MyStompSessionHandlerIntegrationTest { public class MyStompSessionHandlerIntegrationTest {
@Test @Test

View File

@ -1,9 +1,5 @@
package org.baeldung.boot.boottest; package org.baeldung.boot.boottest;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.List;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -11,6 +7,10 @@ import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@DataJpaTest @DataJpaTest
public class EmployeeRepositoryIntegrationTest { public class EmployeeRepositoryIntegrationTest {

View File

@ -1,19 +1,5 @@
package org.baeldung.boot.boottest; package org.baeldung.boot.boottest;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.hasSize;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.io.IOException;
import java.util.List;
import org.baeldung.boot.DemoApplication; import org.baeldung.boot.DemoApplication;
import org.junit.After; import org.junit.After;
import org.junit.Test; import org.junit.Test;
@ -27,6 +13,20 @@ import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import java.io.IOException;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.hasSize;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = DemoApplication.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = DemoApplication.class)
@AutoConfigureMockMvc @AutoConfigureMockMvc

View File

@ -1,10 +1,5 @@
package org.baeldung.boot.boottest; package org.baeldung.boot.boottest;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Arrays;
import java.util.List;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -16,6 +11,11 @@ import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import java.util.Arrays;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
public class EmployeeServiceImplIntegrationTest { public class EmployeeServiceImplIntegrationTest {

View File

@ -1,10 +1,10 @@
package org.baeldung.boot.boottest; package org.baeldung.boot.boottest;
import java.io.IOException;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
class JsonUtil { class JsonUtil {
static byte[] toJson(Object object) throws IOException { static byte[] toJson(Object object) throws IOException {
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();

View File

@ -28,9 +28,17 @@ import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.li
import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.links; import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.links;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration; import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.*; import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.delete;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.*; import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
import static org.springframework.restdocs.payload.PayloadDocumentation.*; import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.patch;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.put;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessResponse;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.restdocs.payload.PayloadDocumentation.requestFields;
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;
import static org.springframework.restdocs.snippet.Attributes.key; import static org.springframework.restdocs.snippet.Attributes.key;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.util.StringUtils.collectionToDelimitedString; import static org.springframework.util.StringUtils.collectionToDelimitedString;

View File

@ -1,8 +1,6 @@
package com.example; package com.example;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.jayway.jsonpath.JsonPath;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
@ -14,21 +12,19 @@ import org.springframework.restdocs.RestDocumentation;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.WebApplicationContext;
import java.io.UnsupportedEncodingException; import static org.hamcrest.Matchers.is;
import java.util.Arrays; import static org.hamcrest.Matchers.notNullValue;
import java.util.HashMap;
import java.util.Map;
import static org.hamcrest.Matchers.*;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration; import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.*; import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.*; import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessResponse;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SpringRestDocsApplication.class) @SpringApplicationConfiguration(classes = SpringRestDocsApplication.class)

View File

@ -37,7 +37,7 @@ public abstract class AbstractDiscoverabilityLiveTest<T extends Serializable> ex
// Then // Then
final String allowHeader = res.getHeader(HttpHeaders.ALLOW); final String allowHeader = res.getHeader(HttpHeaders.ALLOW);
assertThat(allowHeader, AnyOf.<String> anyOf(containsString("GET"), containsString("PUT"), containsString("DELETE"))); assertThat(allowHeader, AnyOf.anyOf(containsString("GET"), containsString("PUT"), containsString("DELETE")));
} }
@Test @Test

View File

@ -31,7 +31,7 @@ public abstract class CsrfAbstractIntegrationTest {
@Autowired @Autowired
private Filter springSecurityFilterChain; private Filter springSecurityFilterChain;
protected MockMvc mvc; MockMvc mvc;
// //
@ -40,15 +40,15 @@ public abstract class CsrfAbstractIntegrationTest {
mvc = MockMvcBuilders.webAppContextSetup(context).addFilters(springSecurityFilterChain).build(); mvc = MockMvcBuilders.webAppContextSetup(context).addFilters(springSecurityFilterChain).build();
} }
protected RequestPostProcessor testUser() { RequestPostProcessor testUser() {
return user("user1").password("user1Pass").roles("USER"); return user("user1").password("user1Pass").roles("USER");
} }
protected RequestPostProcessor testAdmin() { RequestPostProcessor testAdmin() {
return user("admin").password("adminPass").roles("USER", "ADMIN"); return user("admin").password("adminPass").roles("USER", "ADMIN");
} }
protected String createFoo() throws JsonProcessingException { String createFoo() throws JsonProcessingException {
return new ObjectMapper().writeValueAsString(new Foo(randomAlphabetic(6))); return new ObjectMapper().writeValueAsString(new Foo(randomAlphabetic(6)));
} }
} }

View File

@ -33,10 +33,6 @@ public final class JacksonMarshaller implements IMarshaller {
String entityAsJSON = null; String entityAsJSON = null;
try { try {
entityAsJSON = objectMapper.writeValueAsString(resource); entityAsJSON = objectMapper.writeValueAsString(resource);
} catch (final JsonParseException parseEx) {
logger.error("", parseEx);
} catch (final JsonMappingException mappingEx) {
logger.error("", mappingEx);
} catch (final IOException ioEx) { } catch (final IOException ioEx) {
logger.error("", ioEx); logger.error("", ioEx);
} }
@ -51,10 +47,6 @@ public final class JacksonMarshaller implements IMarshaller {
T entity = null; T entity = null;
try { try {
entity = objectMapper.readValue(resourceAsString, clazz); entity = objectMapper.readValue(resourceAsString, clazz);
} catch (final JsonParseException parseEx) {
logger.error("", parseEx);
} catch (final JsonMappingException mappingEx) {
logger.error("", mappingEx);
} catch (final IOException ioEx) { } catch (final IOException ioEx) {
logger.error("", ioEx); logger.error("", ioEx);
} }
@ -76,10 +68,6 @@ public final class JacksonMarshaller implements IMarshaller {
} else { } else {
entities = objectMapper.readValue(resourcesAsString, List.class); entities = objectMapper.readValue(resourcesAsString, List.class);
} }
} catch (final JsonParseException parseEx) {
logger.error("", parseEx);
} catch (final JsonMappingException mappingEx) {
logger.error("", mappingEx);
} catch (final IOException ioEx) { } catch (final IOException ioEx) {
logger.error("", ioEx); logger.error("", ioEx);
} }

View File

@ -10,21 +10,21 @@ public final class IDUtil {
// API // API
public final static String randomPositiveLongAsString() { public static String randomPositiveLongAsString() {
return Long.toString(randomPositiveLong()); return Long.toString(randomPositiveLong());
} }
public final static String randomNegativeLongAsString() { public static String randomNegativeLongAsString() {
return Long.toString(randomNegativeLong()); return Long.toString(randomNegativeLong());
} }
public final static long randomPositiveLong() { public static long randomPositiveLong() {
long id = new Random().nextLong() * 10000; long id = new Random().nextLong() * 10000;
id = (id < 0) ? (-1 * id) : id; id = (id < 0) ? (-1 * id) : id;
return id; return id;
} }
public final static long randomNegativeLong() { private static long randomNegativeLong() {
long id = new Random().nextLong() * 10000; long id = new Random().nextLong() * 10000;
id = (id > 0) ? (-1 * id) : id; id = (id > 0) ? (-1 * id) : id;
return id; return id;

View File

@ -27,6 +27,7 @@ public class LoggerInterceptorIntegrationTest {
@Autowired @Autowired
WebApplicationContext wac; WebApplicationContext wac;
@Autowired @Autowired
MockHttpSession session; MockHttpSession session;

View File

@ -1,10 +1,5 @@
package org.baeldung.web.util; package org.baeldung.web.util;
import java.util.List;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
public final class HTTPLinkHeaderUtil { public final class HTTPLinkHeaderUtil {
private HTTPLinkHeaderUtil() { private HTTPLinkHeaderUtil() {
@ -13,23 +8,6 @@ public final class HTTPLinkHeaderUtil {
// //
/**
* ex. <br>
* https://api.github.com/users/steveklabnik/gists?page=2>; rel="next", <https://api.github.com/users/steveklabnik/gists?page=3>; rel="last"
*/
public static List<String> extractAllURIs(final String linkHeader) {
Preconditions.checkNotNull(linkHeader);
final List<String> linkHeaders = Lists.newArrayList();
final String[] links = linkHeader.split(", ");
for (final String link : links) {
final int positionOfSeparator = link.indexOf(';');
linkHeaders.add(link.substring(1, positionOfSeparator - 1));
}
return linkHeaders;
}
public static String extractURIByRel(final String linkHeader, final String rel) { public static String extractURIByRel(final String linkHeader, final String rel) {
if (linkHeader == null) { if (linkHeader == null) {
return null; return null;
@ -37,7 +15,7 @@ public final class HTTPLinkHeaderUtil {
String uriWithSpecifiedRel = null; String uriWithSpecifiedRel = null;
final String[] links = linkHeader.split(", "); final String[] links = linkHeader.split(", ");
String linkRelation = null; String linkRelation;
for (final String link : links) { for (final String link : links) {
final int positionOfSeparator = link.indexOf(';'); final int positionOfSeparator = link.indexOf(';');
linkRelation = link.substring(positionOfSeparator + 1, link.length()).trim(); linkRelation = link.substring(positionOfSeparator + 1, link.length()).trim();
@ -50,20 +28,9 @@ public final class HTTPLinkHeaderUtil {
return uriWithSpecifiedRel; return uriWithSpecifiedRel;
} }
static Object extractTypeOfRelation(final String linkRelation) { private static Object extractTypeOfRelation(final String linkRelation) {
final int positionOfEquals = linkRelation.indexOf('='); final int positionOfEquals = linkRelation.indexOf('=');
return linkRelation.substring(positionOfEquals + 2, linkRelation.length() - 1).trim(); return linkRelation.substring(positionOfEquals + 2, linkRelation.length() - 1).trim();
} }
/**
* ex. <br>
* https://api.github.com/users/steveklabnik/gists?page=2>; rel="next"
*/
public static String extractSingleURI(final String linkHeader) {
Preconditions.checkNotNull(linkHeader);
final int positionOfSeparator = linkHeader.indexOf(';');
return linkHeader.substring(1, positionOfSeparator - 1);
}
} }

View File

@ -41,7 +41,7 @@ public class ExpressionUtilityObjectsControllerIntegrationTest {
@Autowired @Autowired
private Filter springSecurityFilterChain; private Filter springSecurityFilterChain;
protected RequestPostProcessor testUser() { private RequestPostProcessor testUser() {
return user("user1").password("user1Pass").roles("USER"); return user("user1").password("user1Pass").roles("USER");
} }

View File

@ -41,7 +41,7 @@ public class LayoutDialectControllerIntegrationTest {
@Autowired @Autowired
private Filter springSecurityFilterChain; private Filter springSecurityFilterChain;
protected RequestPostProcessor testUser() { private RequestPostProcessor testUser() {
return user("user1").password("user1Pass").roles("USER"); return user("user1").password("user1Pass").roles("USER");
} }

View File

@ -41,7 +41,7 @@ public class CsrfEnabledIntegrationTest {
@Autowired @Autowired
private Filter springSecurityFilterChain; private Filter springSecurityFilterChain;
protected RequestPostProcessor testUser() { private RequestPostProcessor testUser() {
return user("user1").password("user1Pass").roles("USER"); return user("user1").password("user1Pass").roles("USER");
} }

View File

@ -1,13 +1,13 @@
package com.baeldung.junitparams; package com.baeldung.junitparams;
import static org.junit.Assert.assertEquals;
import junitparams.FileParameters; import junitparams.FileParameters;
import junitparams.JUnitParamsRunner; import junitparams.JUnitParamsRunner;
import junitparams.Parameters; import junitparams.Parameters;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import static org.junit.Assert.assertEquals;
@RunWith(JUnitParamsRunner.class) @RunWith(JUnitParamsRunner.class)
public class SafeAdditionUtilTest { public class SafeAdditionUtilTest {

View File

@ -1,9 +1,8 @@
package com.baeldung.testing.calculator; package com.baeldung.testing.calculator;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions; import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber; import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class) @RunWith(Cucumber.class)
@CucumberOptions( @CucumberOptions(

View File

@ -1,16 +1,5 @@
package com.baeldung.vavr.collections; package com.baeldung.vavr.collections;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import java.util.Comparator;
import org.junit.Test;
import io.vavr.Tuple;
import io.vavr.Tuple2; import io.vavr.Tuple2;
import io.vavr.collection.Array; import io.vavr.collection.Array;
import io.vavr.collection.CharSeq; import io.vavr.collection.CharSeq;
@ -26,6 +15,16 @@ import io.vavr.collection.Stream;
import io.vavr.collection.TreeMap; import io.vavr.collection.TreeMap;
import io.vavr.collection.TreeSet; import io.vavr.collection.TreeSet;
import io.vavr.collection.Vector; import io.vavr.collection.Vector;
import org.junit.Test;
import java.util.Comparator;
import java.util.stream.Collectors;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
public class CollectionAPIUnitTest { public class CollectionAPIUnitTest {
@ -170,7 +169,7 @@ public class CollectionAPIUnitTest {
@Test @Test
public void givenSortedSet_whenReversed_thenCorrect() { public void givenSortedSet_whenReversed_thenCorrect() {
Comparator<String> reverseCompare = (a, b) -> b.compareTo(a); Comparator<String> reverseCompare = Comparator.reverseOrder();
SortedSet<String> set = TreeSet.of(reverseCompare, "Green", "Red", "Blue"); SortedSet<String> set = TreeSet.of(reverseCompare, "Green", "Red", "Blue");
Iterator<String> iterator = set.iterator(); Iterator<String> iterator = set.iterator();
@ -223,13 +222,8 @@ public class CollectionAPIUnitTest {
@Test @Test
public void givenVavrList_whenConverted_thenCorrect() { public void givenVavrList_whenConverted_thenCorrect() {
Integer[] array = List.of(1, 2, 3) java.util.Set<Integer> collect = List.of(1, 2, 3)
.toJavaArray(Integer.class); .collect(Collectors.toSet());
assertEquals(3, array.length);
java.util.Map<String, Integer> map = List.of("1", "2", "3")
.toJavaMap(i -> Tuple.of(i, Integer.valueOf(i)));
assertEquals(new Integer(2), map.get("2"));
} }
@Test @Test
@ -256,6 +250,6 @@ public class CollectionAPIUnitTest {
Number sum = vavrList.map(i -> i * i) Number sum = vavrList.map(i -> i * i)
.sum(); .sum();
assertEquals(new Long(14), sum); assertEquals(14L, sum);
} }
} }