Skip to content

Commit

Permalink
testcontainers changes are implemented, and more tests are added
Browse files Browse the repository at this point in the history
  • Loading branch information
musab.bozkurt committed Feb 4, 2024
1 parent d56c0eb commit 908d3e9
Show file tree
Hide file tree
Showing 21 changed files with 1,179 additions and 9 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
- Centralize exception handling by `ControllerAdvice`
- `Mapstruct` to map different type of objects to each other
- `Micrometer` dependencies were added to track the logs easily
- `Testcontainers` dependencies were added for integration tests
- `docker-compose.yml` contains `Grafana`, `Prometheus` and `Zipkin` to track metrics, `Kafka` for event-driven
architecture
- `Actuator`: http://localhost:8080/actuator
Expand All @@ -62,6 +63,12 @@
- `Prometheus`: http://localhost:9090/graph
- `Zipkin UI`: http://localhost:9411

- Test via `Postman` (OPTIONAL):
1. Import [Postman Collection](docs%2Funit_test_service.postman_collection.json)
2. Right-click the imported Postman Collection and click _**Run Collection**_ section.
3. On the right panel choose _**Functional**_ or _**Performance**_ section, edit _**Run configuration**_ and click
_**run**_ to test the application.

-------

### How_To_Run_And_Test_Application
Expand Down
170 changes: 170 additions & 0 deletions docs/unit_test_service.postman_collection.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
{
"info": {
"_postman_id": "ebe84f96-4ce4-4dcf-98b5-ebc2a6120236",
"name": "Unit Test Service",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "31512047"
},
"item": [
{
"name": "Create Tutorial",
"event": [
{
"listen": "test",
"script": {
"exec": [
"var response = JSON.parse(responseBody);",
"",
"pm.test(\"Status code is 201\", function () {",
" pm.response.to.have.status(201);",
" postman.setEnvironmentVariable(\"tutorialId\", response.id);",
"});"
],
"type": "text/javascript"
}
}
],
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\n \"title\": \"title\",\n \"description\": \"description\",\n \"published\": true\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "http://localhost:8080/api/tutorials",
"protocol": "http",
"host": [
"localhost"
],
"port": "8080",
"path": [
"api",
"tutorials"
]
}
},
"response": []
},
{
"name": "Get Tutorial by Id",
"event": [
{
"listen": "test",
"script": {
"exec": [
"var response = JSON.parse(responseBody);",
"",
"pm.test(\"Status code is 200\", function () {",
" pm.response.to.have.status(200);",
" postman.setEnvironmentVariable(\"tutorialId\", response.id);",
"});"
],
"type": "text/javascript"
}
}
],
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "http://localhost:8080/api/tutorials/:tutorialId",
"protocol": "http",
"host": [
"localhost"
],
"port": "8080",
"path": [
"api",
"tutorials",
":tutorialId"
],
"variable": [
{
"key": "tutorialId",
"value": "{{tutorialId}}"
}
]
}
},
"response": []
},
{
"name": "Update Tutorial by Id",
"event": [
{
"listen": "test",
"script": {
"exec": [
"var response = JSON.parse(responseBody);",
"",
"pm.test(\"Status code is 200\", function () {",
" pm.response.to.have.status(200);",
" postman.setEnvironmentVariable(\"tutorialId\", response.id);",
"});"
],
"type": "text/javascript"
}
}
],
"request": {
"method": "PUT",
"header": [],
"body": {
"mode": "raw",
"raw": "{\n \"title\": \"updatedTitle\",\n \"description\": \"updateddescription\",\n \"published\": true\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "http://localhost:8080/api/tutorials/:tutorialId",
"protocol": "http",
"host": [
"localhost"
],
"port": "8080",
"path": [
"api",
"tutorials",
":tutorialId"
],
"variable": [
{
"key": "tutorialId",
"value": "{{tutorialId}}"
}
]
}
},
"response": []
},
{
"name": "Get All Tutorials",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "http://localhost:8080/api/tutorials",
"protocol": "http",
"host": [
"localhost"
],
"port": "8080",
"path": [
"api",
"tutorials"
]
}
},
"response": []
}
]
}
35 changes: 29 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,6 @@
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
Expand Down Expand Up @@ -119,6 +113,35 @@
<artifactId>spring-boot-docker-compose</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-testcontainers</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.mb.livedataservice.api.controller;

import com.mb.livedataservice.api.request.ApiTutorialRequest;
import com.mb.livedataservice.api.request.ApiTutorialUpdateRequest;
import com.mb.livedataservice.api.response.ApiTutorialResponse;
import com.mb.livedataservice.mapper.TutorialMapper;
import com.mb.livedataservice.service.TutorialService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api")
@CrossOrigin(origins = "http://localhost:8081")
public class TutorialController {

private final TutorialService tutorialService;
private final TutorialMapper tutorialMapper;

@PostMapping("/tutorials")
public ResponseEntity<ApiTutorialResponse> createTutorial(@RequestBody ApiTutorialRequest apiTutorialRequest) {
return new ResponseEntity<>(tutorialMapper.map(tutorialService.save(tutorialMapper.map(apiTutorialRequest))), HttpStatus.CREATED);
}

@GetMapping("/tutorials/{id}")
public ResponseEntity<ApiTutorialResponse> getTutorialById(@PathVariable("id") long id) {
return ResponseEntity.ok(tutorialMapper.map(tutorialService.findById(id)));
}

@GetMapping("/tutorials")
public ResponseEntity<List<ApiTutorialResponse>> getAllTutorials(@RequestParam(required = false) String title) {
return ResponseEntity.ok(tutorialMapper.map(tutorialService.findByTitleContaining(title)));
}

@PutMapping("/tutorials/{id}")
public ResponseEntity<ApiTutorialResponse> updateTutorial(@PathVariable("id") long id, @RequestBody ApiTutorialUpdateRequest apiTutorialUpdateRequest) {
return new ResponseEntity<>(tutorialMapper.map(tutorialService.update(id, tutorialMapper.map(apiTutorialUpdateRequest))), HttpStatus.OK);
}

@DeleteMapping("/tutorials/{id}")
public ResponseEntity<HttpStatus> deleteTutorial(@PathVariable("id") long id) {
tutorialService.deleteById(id);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

@DeleteMapping("/tutorials")
public ResponseEntity<HttpStatus> deleteAllTutorials() {
tutorialService.deleteAll();
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

@GetMapping("/tutorials/published")
public ResponseEntity<List<ApiTutorialResponse>> findByPublished() {
return new ResponseEntity<>(tutorialMapper.map(tutorialService.findByPublished(true)), HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.mb.livedataservice.api.request;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class ApiTutorialRequest {

private String title;

private String description;

private boolean published;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.mb.livedataservice.api.request;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class ApiTutorialUpdateRequest {

private String title;

private String description;

private boolean published;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.mb.livedataservice.api.response;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class ApiTutorialResponse {

private long id;

private String title;

private String description;

private boolean published;
}
Loading

0 comments on commit 908d3e9

Please sign in to comment.