Skip to content

Commit

Permalink
docs: added more readme examples
Browse files Browse the repository at this point in the history
  • Loading branch information
1grzyb1 committed Mar 21, 2024
1 parent a121d95 commit 41b9db9
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Currently supported annotations are:

### Basic Usage

#### Simple GET request
Create a proxy instance of your controller and invoke methods directly

Underneath it uses `MockMvc` to perform the request and map the response to the return type of the method.
Expand All @@ -66,4 +67,72 @@ Underneath it uses `MockMvc` to perform the request and map the response to the
}
```

Without controller client it would look like this

``` java
@Autowired private MockMvc mockMvc;
@Autowired private ObjectMapper objectMapper;

@Test
void basicGetMockMvc() throws Exception {
String responseContent = mockMvc.perform(get("/example"))
.andExpect(status().isOk())
.andReturn()
.getResponse()
.getContentAsString();

ExampleResponse response = objectMapper.readValue(responseContent, ExampleResponse.class);

assertThat(response.message()).isEqualTo("Hello world!");
}
```

#### POST request with request body

For following POST request

``` java
@PostMapping("/example/body")
public ExampleResponse examplePostMethod(@RequestBody ExampleRequest request) {
return new ExampleResponse(request.getMessage());
}
```

Controller client usage would look like this

``` java
@Test
void postWithBody() {
ExampleController exampleController = controllerClientBuilderFactory.builder(ExampleController.class).build();
var request = new ExampleRequest("Test message");
var response = exampleController.bodyExample(request);
assertThat(response.message()).isEqualTo("Received: Test message");
}
```

Without controller client it would look like this

```java
@Test
void testBodyExample() throws Exception {
var exampleRequest = new ExampleRequest("Test message");
var requestJson = objectMapper.writeValueAsString(exampleRequest);

var requestBuilder =
MockMvcRequestBuilders.request(HttpMethod.POST, "/example/body")
.content(requestJson)
.contentType(MediaType.APPLICATION_JSON);
var responseContent = mockMvc.perform(requestBuilder)
.andExpect(status().isOk())
.andReturn()
.getResponse()
.getContentAsString();

var response = objectMapper.readValue(responseContent, ExampleResponse.class);
assertThat(response.message()).isEqualTo("Received: Test message");
}

```


You can check more examples in the [example package](example/src/test/java/ovh/snet/grzybek/controller/client/example).

0 comments on commit 41b9db9

Please sign in to comment.