-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
examples/gcp-web-example/src/main/java/org/entur/example/web/rest/AsyncDocumentEndpoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package org.entur.example.web.rest; | ||
|
||
import com.fasterxml.jackson.core.JsonFactory; | ||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.io.CharArrayWriter; | ||
import java.io.IOException; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
@RestController | ||
@RequestMapping("/api/async-document") | ||
public class AsyncDocumentEndpoint { | ||
|
||
private final static Logger logger = LoggerFactory.getLogger(AsyncDocumentEndpoint.class); | ||
|
||
@PostMapping("/some/method") | ||
public CompletableFuture<MyEntity> someMessage(@RequestBody MyEntity entity) { | ||
logger.trace("Hello entity with secret / trace"); | ||
logger.debug("Hello entity with secret / debug"); | ||
logger.info("Hello entity with secret / info"); | ||
logger.warn("Hello entity with secret / warn"); | ||
logger.error("Hello entity with secret / error"); | ||
|
||
entity.setName("Entur response"); | ||
return CompletableFuture.supplyAsync(() -> { | ||
try { | ||
Thread.sleep(1000); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
return entity; | ||
}); | ||
} | ||
|
||
@PostMapping("/some/error") | ||
public CompletableFuture<ResponseEntity> errorMethod(@RequestBody MyEntity entity) throws InterruptedException { | ||
System.out.flush(); | ||
System.out.println("System out before endpoint logging on thread " + Thread.currentThread().getName()); | ||
|
||
logger.trace("This message should be ignored / trace"); | ||
logger.debug("This message should be ignored / debug"); | ||
logger.info("This message should be delayed / info"); | ||
logger.warn("This message should be logged / warn"); | ||
|
||
Thread.sleep(1000); | ||
System.out.println("System out after endpoint logging + 1000ms"); | ||
|
||
return CompletableFuture.supplyAsync(() -> { | ||
System.out.println("Complete future on thread " + Thread.currentThread().getName()); | ||
try { | ||
Thread.sleep(1000); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
return new ResponseEntity(HttpStatus.NOT_FOUND); | ||
}); | ||
} | ||
|
||
|
||
} |
72 changes: 72 additions & 0 deletions
72
...example/src/test/java/org/entur/example/web/OndemandWebLoggingHttpNotFound1AsyncTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package org.entur.example.web; | ||
|
||
import no.entur.logging.cloud.logback.logstash.test.CompositeConsoleOutputControl; | ||
import no.entur.logging.cloud.logback.logstash.test.CompositeConsoleOutputControlClosable; | ||
import org.entur.example.web.rest.MyEntity; | ||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; | ||
import org.springframework.boot.test.web.client.TestRestTemplate; | ||
import org.springframework.boot.test.web.server.LocalServerPort; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.test.context.TestPropertySource; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* | ||
* Note: Async does not currently work with on-demand logging. | ||
* | ||
*/ | ||
|
||
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) | ||
@TestPropertySource(properties = {"entur.logging.http.ondemand.enabled=true", "entur.logging.http.ondemand.failure.http.statusCode.equalOrHigherThan=400"}) | ||
public class OndemandWebLoggingHttpNotFound1AsyncTest { | ||
|
||
@LocalServerPort | ||
private int randomServerPort; | ||
|
||
@Autowired | ||
private TestRestTemplate restTemplate; | ||
|
||
@Test | ||
@Disabled | ||
public void useHumanReadablePlainEncoderExpectFullLogging() { | ||
MyEntity entity = new MyEntity(); | ||
entity.setName("Entur"); | ||
entity.setSecret("mySecret"); | ||
|
||
ResponseEntity<MyEntity> response = restTemplate.postForEntity("/api/async-document/some/error", entity, MyEntity.class); | ||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); | ||
} | ||
|
||
@Test | ||
@Disabled | ||
public void useHumanReadableJsonEncoderExpectFullLogging() { | ||
try (CompositeConsoleOutputControlClosable c = CompositeConsoleOutputControl.useHumanReadableJsonEncoder()) { | ||
MyEntity entity = new MyEntity(); | ||
entity.setName("Entur"); | ||
entity.setSecret("mySecret"); | ||
|
||
ResponseEntity<MyEntity> response = restTemplate.postForEntity("/api/async-document/some/error", entity, MyEntity.class); | ||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); | ||
} | ||
} | ||
|
||
@Test | ||
@Disabled | ||
public void useMachineReadableJsonEncoderExpectFullLogging() { | ||
try (CompositeConsoleOutputControlClosable c = CompositeConsoleOutputControl.useMachineReadableJsonEncoder()) { | ||
MyEntity entity = new MyEntity(); | ||
entity.setName("Entur"); | ||
entity.setSecret("mySecret"); | ||
|
||
ResponseEntity<MyEntity> response = restTemplate.postForEntity("/api/async-document/some/error", entity, MyEntity.class); | ||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); | ||
} | ||
} | ||
|
||
} |