Merge branch 'master' into JAVA-3536-move-spring-rest-shell

This commit is contained in:
Loredana Crusoveanu 2021-01-02 15:15:35 +02:00 committed by GitHub
commit 5cf91b556c
285 changed files with 1199 additions and 912 deletions

View File

@ -28,6 +28,29 @@
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-junit-jupiter</artifactId>
<version>${mockserver.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
@ -48,7 +71,9 @@
<maven.compiler.source.version>11</maven.compiler.source.version>
<maven.compiler.target.version>11</maven.compiler.target.version>
<guava.version>29.0-jre</guava.version>
<junit.jupiter.version>5.7.0</junit.jupiter.version>
<assertj.version>3.17.2</assertj.version>
<mockserver.version>5.11.1</mockserver.version>
</properties>
</project>

View File

@ -0,0 +1,17 @@
package com.baeldung.features;
public class MainClass {
private static boolean mainPrivateMethod() {
return true;
}
public static class NestedClass {
boolean nestedPublicMethod() {
return mainPrivateMethod();
}
}
}

View File

@ -0,0 +1,54 @@
package com.baeldung.features;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockserver.integration.ClientAndServer;
import org.mockserver.model.HttpStatusCode;
import org.mockserver.socket.PortFactory;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockserver.integration.ClientAndServer.startClientAndServer;
class HttpClientIntegrationTest {
private static ClientAndServer mockServer;
private static int port;
@BeforeAll
static void startServer() {
port = PortFactory.findFreePort();
mockServer = startClientAndServer(port);
mockServer.when(new org.mockserver.model.HttpRequest().withMethod("GET"))
.respond(new org.mockserver.model.HttpResponse()
.withStatusCode(HttpStatusCode.OK_200.code())
.withBody("Hello from the server!"));
}
@AfterAll
static void stopServer() {
mockServer.stop();
}
@Test
void givenSampleHttpRequest_whenRequestIsSent_thenServerResponseIsReceived() throws IOException, InterruptedException {
HttpClient httpClient = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_2)
.connectTimeout(Duration.ofSeconds(20))
.build();
HttpRequest httpRequest = HttpRequest.newBuilder()
.GET()
.uri(URI.create("http://localhost:" + port))
.build();
HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
assertThat(httpResponse.body()).isEqualTo("Hello from the server!");
}
}

View File

@ -0,0 +1,61 @@
package com.baeldung.features;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
class JavaElevenFeaturesUnitTest {
@Test
void givenMultilineString_whenExtractingNonBlankStrippedLines_thenLinesAreReturned() {
String multilineString = "Baeldung helps \n \n developers \n explore Java.";
List<String> lines = multilineString.lines()
.filter(line -> !line.isBlank())
.map(String::strip)
.collect(Collectors.toList());
assertThat(lines).containsExactly("Baeldung helps", "developers", "explore Java.");
}
@Test
void givenTemporaryFile_whenReadingStringContent_thenContentIsReturned(@TempDir Path tempDir) throws IOException {
Path filePath = Files.writeString(Files.createTempFile(tempDir, "demo", ".txt"), "Sample text");
String fileContent = Files.readString(filePath);
assertThat(fileContent).isEqualTo("Sample text");
}
@Test
void givenSampleList_whenConvertingToArray_thenItemsRemainUnchanged() {
List<String> sampleList = Arrays.asList("Java", "Kotlin");
String[] sampleArray = sampleList.toArray(String[]::new);
assertThat(sampleArray).containsExactly("Java", "Kotlin");
}
@Test
void givenSampleList_whenConvertingToUppercaseString_thenUppercaseIsReturned() {
List<String> sampleList = Arrays.asList("Java", "Kotlin");
String resultString = sampleList.stream()
.map((@Nonnull var x) -> x.toUpperCase())
.collect(Collectors.joining(", "));
assertThat(resultString).isEqualTo("JAVA, KOTLIN");
}
@Test
void givenSampleList_whenExtractingNonBlankValues_thenOnlyNonBlanksAreReturned() {
List<String> sampleList = Arrays.asList("Java", "\n \n", "Kotlin", " ");
List<String> withoutBlanks = sampleList.stream()
.filter(Predicate.not(String::isBlank))
.collect(Collectors.toList());
assertThat(withoutBlanks).containsExactly("Java", "Kotlin");
}
}

View File

@ -0,0 +1,37 @@
package com.baeldung.features;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
class NestedClassesUnitTest {
@Test
public void giveNestedClass_whenCallingMainClassPrivateMethod_thenNoExceptionIsThrown() {
MainClass.NestedClass nestedInstance = new MainClass.NestedClass();
assertThat(nestedInstance.nestedPublicMethod()).isTrue();
}
@Test
public void giveNestedClass_whenCheckingNestmate_thenNestedClassIsReturned() {
assertThat(MainClass.class.isNestmateOf(MainClass.NestedClass.class)).isTrue();
}
@Test
public void giveNestedClass_whenCheckingNestHost_thenMainClassIsReturned() {
assertThat(MainClass.NestedClass.class.getNestHost()).isEqualTo(MainClass.class);
}
@Test
public void giveNestedClass_whenCheckingNestMembers_thenNestMembersAreReturned() {
Set<String> nestedMembers = Arrays.stream(MainClass.NestedClass.class.getNestMembers())
.map(Class::getName)
.collect(Collectors.toSet());
assertThat(nestedMembers).contains(MainClass.class.getName(), MainClass.NestedClass.class.getName());
}
}

View File

@ -6,8 +6,7 @@ import java.util.logging.Logger;
public class OutputStreamExample {
public static void main(String[] args) {
Logger log = Logger.getLogger(OutputStreamExample.class.getName());
log.log(Level.INFO, Integer.toString(sum(1,2)));
System.out.println(sum(1,2));
}
public static int sum(int a, int b) {

View File

@ -5,10 +5,10 @@ import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.String;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.lang.Integer;
@ -88,4 +88,21 @@ class ProcessUnderstandingUnitTest {
.filter(ph -> (ph.pid() > 10000 && ph.pid() < 50000))
.count()) > 0);
}
@Test
public void givenSourceProgram_whenReadingInputStream_thenFirstLineEquals3() throws IOException {
Runtime.getRuntime()
.exec("javac -cp src src/main/java/com/baeldung/java9/process/OutputStreamExample.java"
.replace("/", File.separator));
Process process = Runtime.getRuntime()
.exec("java -cp src/main/java com.baeldung.java9.process.OutputStreamExample"
.replace("/", File.separator));
BufferedReader output = new BufferedReader(new InputStreamReader(process.getInputStream()));
int value = Integer.parseInt(output.readLine());
assertEquals(3, value);
}
}

View File

@ -9,11 +9,13 @@ import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.jupiter.api.Disabled;
import static org.junit.Assert.assertTrue;
@Ignore
public class ScreenshotUnitTest {
@Test
@ -43,7 +45,6 @@ public class ScreenshotUnitTest {
// This methods needs a component as a parameter and can only be run from an application with a GUI
@Test
@Disabled
public void givenComponent_whenTakeScreenshot_thenSaveToFile(Component component) throws Exception {
Rectangle componentRect = component.getBounds();
BufferedImage bufferedImage = new BufferedImage(componentRect.width, componentRect.height, BufferedImage.TYPE_INT_ARGB);

View File

@ -18,15 +18,6 @@ import static org.powermock.api.mockito.PowerMockito.mockStatic;
@PrepareForTest({ LocalDateTime.class })
public class LocalDateTimeUnitTest {
@Test
public void givenLocalDateTimeMock_whenNow_thenGetFixedLocalDateTime() {
Clock clock = Clock.fixed(Instant.parse("2014-12-22T10:15:30.00Z"), ZoneId.of("UTC"));
String dateTimeExpected = "2014-12-22T10:15:30";
LocalDateTime now = LocalDateTime.now(clock);
assertThat(now).isEqualTo(dateTimeExpected);
}
@Test
public void givenFixedClock_whenNow_thenGetFixedLocalDateTime() {
Clock clock = Clock.fixed(Instant.parse("2014-12-22T10:15:30.00Z"), ZoneId.of("UTC"));

18
pom.xml
View File

@ -658,21 +658,11 @@
<module>spring-mobile</module>
<module>spring-mockito</module>
<module>spring-mvc-forms-thymeleaf</module>
<module>spring-mvc-java</module>
<module>spring-mvc-java-2</module>
<module>spring-mvc-velocity</module>
<module>spring-mvc-xml</module>
<module>spring-protobuf</module>
<module>spring-quartz</module>
<module>spring-reactor</module>
<module>spring-remoting</module>
<module>spring-rest-http-2</module>
<module>spring-rest-query-language</module>
<module>spring-rest-simple</module>
<module>spring-resttemplate</module>
<module>spring-rest-testing</module>
@ -1124,19 +1114,11 @@
<module>spring-mobile</module>
<module>spring-mockito</module>
<module>spring-mvc-forms-thymeleaf</module>
<module>spring-mvc-java</module>
<module>spring-mvc-java-2</module>
<module>spring-mvc-velocity</module>
<module>spring-mvc-xml</module>
<module>spring-protobuf</module>
<module>spring-quartz</module>
<module>spring-reactor</module>
<module>spring-remoting</module>
<module>spring-rest-query-language</module>
<module>spring-rest-simple</module>
<module>spring-resttemplate</module>
<module>spring-rest-testing</module>

View File

@ -24,6 +24,7 @@
<module>spring-boot-angular</module>
<module>spring-boot-annotations</module>
<module>spring-boot-artifacts</module>
<module>spring-boot-artifacts-2</module>
<module>spring-boot-autoconfiguration</module>
<module>spring-boot-basic-customization</module>
<module>spring-boot-basic-customization-2</module>

View File

@ -0,0 +1,7 @@
## Spring Boot Artifacts 2
This module contains articles about configuring the Spring Boot build process 2.
### Relevant Articles:
TBD

View File

@ -0,0 +1,48 @@
<?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>
<parent>
<groupId>com.baeldung.spring-boot-modules</groupId>
<artifactId>spring-boot-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>
<artifactId>spring-boot-artifacts-2</artifactId>
<packaging>jar</packaging>
<name>spring-boot-artifacts-2</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<!-- The main class to start by executing java -jar -->
<start-class>com.baeldung.demo.DemoApplication</start-class>
</properties>
</project>

View File

@ -0,0 +1,11 @@
package com.baeldung.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

View File

@ -0,0 +1,15 @@
package com.baeldung.demo;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoRestController {
@GetMapping(value = "/welcome")
public ResponseEntity<String> welcomeEndpoint() {
return ResponseEntity.ok("Welcome to Baeldung Spring Boot Demo!");
}
}

View File

@ -0,0 +1,3 @@
spring:
application:
name: Baeldung_SpringBoot_Demo

View File

@ -5,14 +5,21 @@ import static com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.support.RateL
import static com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.support.RateLimitConstants.HEADER_REMAINING;
import static com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.support.RateLimitConstants.HEADER_REMAINING_QUOTA;
import static com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.support.RateLimitConstants.HEADER_RESET;
import static java.lang.Integer.parseInt;
import static org.hamcrest.Matchers.both;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.springframework.http.HttpStatus.OK;
import static org.springframework.http.HttpStatus.TOO_MANY_REQUESTS;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
@ -26,7 +33,7 @@ import org.springframework.test.context.junit4.SpringRunner;
@AutoConfigureTestDatabase
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class GreetingControllerManualTest {
public class GreetingControllerIntegrationTest {
private static final String SIMPLE_GREETING = "/greeting/simple";
private static final String ADVANCED_GREETING = "/greeting/advanced";
@ -44,9 +51,13 @@ public class GreetingControllerManualTest {
String remaining = headers.getFirst(HEADER_REMAINING + key);
String reset = headers.getFirst(HEADER_RESET + key);
assertEquals(limit, "5");
assertEquals(remaining, "4");
assertEquals(reset, "60000");
assertEquals("5", limit);
assertEquals(remaining, "4", remaining);
assertNotNull(reset);
assertThat(
parseInt(reset),
is(both(greaterThanOrEqualTo(0)).and(lessThanOrEqualTo(60000)))
);
assertEquals(OK, response.getStatusCode());
}

View File

@ -20,10 +20,17 @@
<module>spring-mvc-basics-4</module>
<module>spring-mvc-crash</module>
<module>spring-mvc-forms-jsp</module>
<module>spring-mvc-forms-thymeleaf</module>
<module>spring-mvc-java</module>
<module>spring-mvc-java-2</module>
<module>spring-mvc-velocity</module>
<module>spring-mvc-views</module>
<module>spring-mvc-webflow</module>
<module>spring-mvc-xml</module>
<module>spring-rest-angular</module>
<module>spring-rest-http</module>
<module>spring-rest-http-2</module>
<module>spring-rest-query-language</module>
<module>spring-rest-shell</module>
<module>spring-resttemplate-2</module>
<module>spring-thymeleaf</module>

View File

@ -11,7 +11,7 @@
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-boot-2</relativePath>
<relativePath>../../parent-boot-2</relativePath>
</parent>
<dependencies>

View File

@ -12,7 +12,7 @@
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-boot-2</relativePath>
<relativePath>../../parent-boot-2</relativePath>
</parent>
<dependencies>

View File

@ -4,7 +4,7 @@ This module contains articles about Spring MVC with Java configuration
### The Course
The "REST With Spring" Classes: http://bit.ly/restwithspring
The "REST With Spring" Classes: https://bit.ly/restwithspring
### Relevant Articles:
- [Integration Testing in Spring](https://www.baeldung.com/integration-testing-in-spring)

View File

@ -11,7 +11,7 @@
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-boot-2</relativePath>
<relativePath>../../parent-boot-2</relativePath>
</parent>
<dependencies>

Some files were not shown because too many files have changed in this diff Show More