-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7258 from ita-social-projects/backup-feature
- Loading branch information
Showing
16 changed files
with
540 additions
and
3 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
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
80 changes: 80 additions & 0 deletions
80
core/src/main/java/greencity/controller/DataBaseBackUpController.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,80 @@ | ||
package greencity.controller; | ||
|
||
import greencity.constant.HttpStatuses; | ||
import greencity.service.DataBaseBackUpService; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.media.Content; | ||
import io.swagger.v3.oas.annotations.media.ExampleObject; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.format.annotation.DateTimeFormat; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/database") | ||
public class DataBaseBackUpController { | ||
private final DataBaseBackUpService dataBaseBackUpService; | ||
|
||
/** | ||
* Endpoint to initiate the database backup process. | ||
* | ||
* @return ResponseEntity with a success message if the backup is completed | ||
* successfully. | ||
*/ | ||
@Operation(summary = "Backup Database") | ||
@ResponseStatus(value = HttpStatus.CREATED) | ||
@ApiResponses(value = { | ||
@ApiResponse(responseCode = "200", description = HttpStatuses.OK, | ||
content = @Content(examples = @ExampleObject(HttpStatuses.OK))), | ||
@ApiResponse(responseCode = "400", description = HttpStatuses.BAD_REQUEST, | ||
content = @Content(examples = @ExampleObject(HttpStatuses.BAD_REQUEST))), | ||
@ApiResponse(responseCode = "401", description = HttpStatuses.UNAUTHORIZED, | ||
content = @Content(examples = @ExampleObject(HttpStatuses.UNAUTHORIZED))), | ||
@ApiResponse(responseCode = "403", description = HttpStatuses.FORBIDDEN, | ||
content = @Content(examples = @ExampleObject(HttpStatuses.FORBIDDEN))), | ||
@ApiResponse(responseCode = "404", description = HttpStatuses.NOT_FOUND, | ||
content = @Content(examples = @ExampleObject(HttpStatuses.NOT_FOUND))) | ||
}) | ||
@GetMapping("/backup") | ||
public ResponseEntity<String> backupDatabase() { | ||
dataBaseBackUpService.backupDatabase(); | ||
return ResponseEntity.ok("Database backup completed successfully!"); | ||
} | ||
|
||
/** | ||
* Get URLs of backup files from DB for the specified time range. | ||
* | ||
* @param start Start time of the range in ISO date-time format. | ||
* @param end End time of the range in ISO date-time format. | ||
* @return ResponseEntity containing a list of backup URLs. | ||
*/ | ||
@Operation(summary = "Get urls of backup files from DB for the specified time range") | ||
@ApiResponses(value = { | ||
@ApiResponse(responseCode = "200", description = HttpStatuses.OK, | ||
content = @Content(examples = @ExampleObject(HttpStatuses.OK))), | ||
@ApiResponse(responseCode = "400", description = HttpStatuses.BAD_REQUEST, | ||
content = @Content(examples = @ExampleObject(HttpStatuses.BAD_REQUEST))), | ||
@ApiResponse(responseCode = "401", description = HttpStatuses.UNAUTHORIZED, | ||
content = @Content(examples = @ExampleObject(HttpStatuses.UNAUTHORIZED))), | ||
@ApiResponse(responseCode = "403", description = HttpStatuses.FORBIDDEN, | ||
content = @Content(examples = @ExampleObject(HttpStatuses.FORBIDDEN))), | ||
@ApiResponse(responseCode = "404", description = HttpStatuses.NOT_FOUND, | ||
content = @Content(examples = @ExampleObject(HttpStatuses.NOT_FOUND))) | ||
}) | ||
@GetMapping("/backupFiles") | ||
public ResponseEntity<List<String>> getBackupFiles( | ||
@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime start, | ||
@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime end) { | ||
return ResponseEntity.ok(dataBaseBackUpService.getBackUpDBUrls(start, end)); | ||
} | ||
} |
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
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
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
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
64 changes: 64 additions & 0 deletions
64
core/src/test/java/greencity/controller/DataBaseBackUpControllerTest.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,64 @@ | ||
package greencity.controller; | ||
|
||
import greencity.service.DataBaseBackUpService; | ||
import java.time.LocalDateTime; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; | ||
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.doNothing; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class DataBaseBackUpControllerTest { | ||
|
||
private MockMvc mockMvc; | ||
|
||
@Mock | ||
private DataBaseBackUpService dataBaseBackUpService; | ||
|
||
@InjectMocks | ||
private DataBaseBackUpController controller; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
this.mockMvc = MockMvcBuilders.standaloneSetup(controller) | ||
.build(); | ||
} | ||
|
||
@Test | ||
void testBackupDatabase() throws Exception { | ||
doNothing().when(dataBaseBackUpService).backupDatabase(); | ||
|
||
mockMvc.perform(MockMvcRequestBuilders.get("/database/backup") | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(MockMvcResultMatchers.status().isOk()) | ||
.andExpect(MockMvcResultMatchers.content().string("Database backup completed successfully!")); | ||
} | ||
|
||
@Test | ||
void testGetBackupFiles() throws Exception { | ||
LocalDateTime start = LocalDateTime.now().minusHours(1); | ||
LocalDateTime end = LocalDateTime.now(); | ||
List<String> urls = Arrays.asList("http://backup1.com", "http://backup2.com"); | ||
when(dataBaseBackUpService.getBackUpDBUrls(any(LocalDateTime.class), any(LocalDateTime.class))) | ||
.thenReturn(urls); | ||
|
||
mockMvc.perform(MockMvcRequestBuilders.get("/database/backupFiles") | ||
.param("start", start.toString()) | ||
.param("end", end.toString()) | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(MockMvcResultMatchers.status().isOk()); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
dao/src/main/java/greencity/entity/DataBaseBackUpFiles.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,30 @@ | ||
package greencity.entity; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.Table; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Column; | ||
import java.time.LocalDateTime; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import lombok.Builder; | ||
|
||
@Entity | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Getter | ||
@Setter | ||
@Builder | ||
@Table(name = "db_backups") | ||
public class DataBaseBackUpFiles { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
private String url; | ||
@Column(name = "created_at") | ||
private LocalDateTime createdAt; | ||
} |
17 changes: 17 additions & 0 deletions
17
dao/src/main/java/greencity/repository/DataBaseBackUpFilesRepo.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,17 @@ | ||
package greencity.repository; | ||
|
||
import greencity.entity.DataBaseBackUpFiles; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface DataBaseBackUpFilesRepo extends JpaRepository<DataBaseBackUpFiles, Long> { | ||
/** | ||
* Finds all backup files created between the specified start and end times. | ||
* | ||
* @param start the start time of the range, inclusive | ||
* @param end the end time of the range, inclusive | ||
* @return a list of backup files created within the specified time range | ||
*/ | ||
List<DataBaseBackUpFiles> findAllByCreatedAtBetween(LocalDateTime start, LocalDateTime end); | ||
} |
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
20 changes: 20 additions & 0 deletions
20
dao/src/main/resources/db/changelog/logs/ch-add-table-db_backups-kizerov.xml
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,20 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog | ||
https://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd"> | ||
|
||
<changeSet id="1" author="Kizerov"> | ||
<createTable tableName="db_backups"> | ||
<column name="id" type="BIGINT" autoIncrement="true"> | ||
<constraints primaryKey="true" nullable="false"/> | ||
</column> | ||
<column name="url" type="VARCHAR(255)"> | ||
<constraints nullable="false"/> | ||
</column> | ||
<column name="created_at" type="TIMESTAMP"> | ||
<constraints nullable="false"/> | ||
</column> | ||
</createTable> | ||
</changeSet> | ||
</databaseChangeLog> |
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
26 changes: 26 additions & 0 deletions
26
service-api/src/main/java/greencity/service/DataBaseBackUpService.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,26 @@ | ||
package greencity.service; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
/** | ||
* Interface for the service that handles database backup operations. | ||
*/ | ||
public interface DataBaseBackUpService { | ||
/** | ||
* Creates a backup of the database. This method should initiate the process of | ||
* backing up the entire database. | ||
*/ | ||
void backupDatabase(); | ||
|
||
/** | ||
* Retrieves URLs of backup files created between the specified start and end | ||
* times. | ||
* | ||
* @param start the start time of the range, inclusive | ||
* @param end the end time of the range, inclusive | ||
* @return a list of URLs of backup files created within the specified time | ||
* range | ||
*/ | ||
List<String> getBackUpDBUrls(LocalDateTime start, LocalDateTime end); | ||
} |
Oops, something went wrong.