Commit 1b37d11c authored by Riabokon Stanislav(EPAM)[GCP]'s avatar Riabokon Stanislav(EPAM)[GCP]
Browse files

Merge branch 'feature/GONRG-1610-rewrite_integration_tests' into 'integration-master'

GONRG-1610 Rewrite integration tests for BackupService

Closes GONRG-1610

See merge request go3-nrg/platform/deployment-and-operations/backup-service!16
parents 61b3f418 c9b053c4
......@@ -47,7 +47,7 @@ import org.springframework.web.context.annotation.RequestScope;
@RequestMapping(value = "/collection")
@RequestScope
@RequiredArgsConstructor
@PreAuthorize("@authorizationFilter.hasPermission('" + BackupRole.BACKUP + "')")
@PreAuthorize("@authorizationFilter.hasPermission('" + BackupRole.ADMIN + "')")
@Validated
public class BackupCollectionApi {
......@@ -65,7 +65,7 @@ public class BackupCollectionApi {
@PathVariable String collectionName,
@Validated(UpdateCollection.class) @RequestBody BackupCollection collection) {
BackupCollection backupCollection = collectionService.update(collection, collectionName);
return new ResponseEntity<>(backupCollection, HttpStatus.CREATED);
return new ResponseEntity<>(backupCollection, HttpStatus.ACCEPTED);
}
@GetMapping("/{collectionName}")
......@@ -96,7 +96,7 @@ public class BackupCollectionApi {
@PatchMapping("/restore/{stateId}")
public ResponseEntity<CollectionState> restoreCollection(@PathVariable String stateId) {
CollectionState collectionState = collectionService.restoreCollection(stateId);
return new ResponseEntity<>(collectionState, HttpStatus.OK);
return new ResponseEntity<>(collectionState, HttpStatus.ACCEPTED);
}
......
......@@ -38,9 +38,8 @@ public class AppExceptionHandler {
return this.getErrorResponse(e);
}
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MethodArgumentNotValidException.class)
public Map<String, String> handleValidationExceptions(
public ResponseEntity<Map<String, String>> handleValidationExceptions(
MethodArgumentNotValidException ex) {
Map<String, String> errors = new HashMap<>();
ex.getBindingResult().getAllErrors().forEach((error) -> {
......@@ -48,7 +47,7 @@ public class AppExceptionHandler {
String errorMessage = error.getDefaultMessage();
errors.put(fieldName, errorMessage);
});
return errors;
return new ResponseEntity<>(errors, HttpStatus.BAD_REQUEST);
}
private ResponseEntity<Object> getErrorResponse(AppException e) {
......
......@@ -18,7 +18,7 @@ package org.opengroup.osdu.backup.model;
public class BackupRole {
public static final String BACKUP = "service.backup";
public static final String ADMIN = "service.backup.admin";
private BackupRole() {
}
......
......@@ -19,6 +19,7 @@ gcp.project-id=osdu-cicd-epam
LOG_PREFIX=${log.prefix}
spring.cloud.gcp.datastore.namespace=opendes
spring.cloud.gcp.sql.databaseName=postgres
spring.cloud.gcp.sql.instanceConnectionName=osdu-cicd-epam:us-central1:test-postgre
......
......@@ -35,12 +35,6 @@
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.5.2</version>
</dependency>
</dependencies>
</project>
......@@ -2,14 +2,14 @@ package org.opengroup.osdu.backup;
public class Config {
public static final String BACKUP_SERVICE_HOST = "";
public static final String BACKUP_SERVICE_BASE_URL = "";
public static final String INTEGRATION_TESTER = "";
private static final String NO_DATA_ACCESS_TESTER = "";
public static final String DATA_PARTITION_ID = "";
public static String getBackupServiceHost() {
return getEnvironmentVariableOrDefaultValue("BACKUP_SERVICE_HOST", BACKUP_SERVICE_HOST);
public static String getBackupServiceBaseUrl() {
return getEnvironmentVariableOrDefaultValue("BACKUP_SERVICE_BASE_URL", BACKUP_SERVICE_BASE_URL);
}
public static String getIntegrationTester() {
......
......@@ -19,6 +19,7 @@ import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
......@@ -39,13 +40,24 @@ public abstract class HttpClient {
public abstract String getNoDataAccessToken() throws IOException;
public ClientResponse send(String path, String httpMethod, Map<String, String> headers)
throws Exception {
return send(path, httpMethod, headers, "");
}
public ClientResponse send(String httpMethod, Map<String, String> headers,
String requestBody)
throws Exception {
return send("", httpMethod, headers, requestBody);
}
public ClientResponse send(String path, String httpMethod, Map<String, String> headers,
String requestBody)
throws Exception {
Client client = this.getClient();
String mergedURL = new URL(Config.getBackupServiceHost() + path).toString();
String mergedURL = URI.create(Config.getBackupServiceBaseUrl() + path).normalize().toString();
System.out.println(String.format("calling %s API:%s", httpMethod, mergedURL));
System.out.println(String.format("request body:%s", requestBody));
......
......@@ -12,7 +12,7 @@ public class TestBase {
protected static AssetUtil assetUtil;
public static Map<String, String> getCommonHeader() throws IOException {
public static Map<String, String> getValidAuthorizationHeaders() throws IOException {
return getHeaders(Config.getDataPartitionId(), client.getAccessToken());
}
......
package org.opengroup.osdu.backup.apitest;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.jersey.api.client.ClientResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import javax.ws.rs.HttpMethod;
import org.apache.http.HttpStatus;
import org.junit.jupiter.api.Test;
import org.opengroup.osdu.backup.Config;
import org.opengroup.osdu.backup.TestBase;
import org.opengroup.osdu.backup.locator.Asset;
import org.opengroup.osdu.backup.model.BackupImportRequest;
import org.opengroup.osdu.backup.model.BackupSchedule;
public class Backup extends TestBase {
static final String SCHEDULE = "/schedule";
static final String SCHEDULE_LIST = "/list_schedules";
static final String BACKUP_IMPORT = "/backup_import";
static final String BACKUP_LIST = "/list_backups";
static final String GET_ID = "?id=";
protected static ObjectMapper mapper = new ObjectMapper();
protected static List<String> scheduleIds = new ArrayList<>();
@Test
public void shouldCreateBackupSchedule() throws Exception {
BackupSchedule schedule = submitBackupSchedule();
cancelSchedule(schedule);
}
@Test
public void shouldUpdateSchedule() throws Exception {
BackupSchedule schedule = submitBackupSchedule();
schedule.setActive(false);
schedule.setBackupPeriod(10);
schedule.setLifetime(10);
String updateScheduleRequest = mapper.writerFor(BackupSchedule.class).writeValueAsString(schedule);
ClientResponse updateScheduleResponse = client
.send(SCHEDULE, HttpMethod.PUT, getCommonHeader(), updateScheduleRequest);
assertEquals(HttpStatus.SC_ACCEPTED, updateScheduleResponse.getStatus());
BackupSchedule updatedSchedule = mapper
.readValue(updateScheduleResponse.getEntity(String.class), BackupSchedule.class);
assertFalse(updatedSchedule.isActive());
assertTrue(
schedule.getBackupPeriod() == updatedSchedule.getBackupPeriod() && schedule.getLifetime() == updatedSchedule
.getLifetime());
}
@Test
public void shouldReturnCreatedScheduleById() throws Exception {
BackupSchedule createdBackupSchedule = submitBackupSchedule();
ClientResponse scheduleByIdResponse = client
.send(SCHEDULE + GET_ID + createdBackupSchedule.getScheduleId(), HttpMethod.GET, getCommonHeader(), "");
BackupSchedule scheduleById = mapper
.readValue(scheduleByIdResponse.getEntity(String.class), BackupSchedule.class);
assertEquals(HttpStatus.SC_OK, scheduleByIdResponse.getStatus());
assertEquals(createdBackupSchedule, scheduleById);
cancelSchedule(scheduleById);
}
@Test
public void getSchedulesShouldReturnSchedulesWithCreatedSchedule() throws Exception {
BackupSchedule createdBackupSchedule = submitBackupSchedule();
ClientResponse listSchedulesResponse = client.send(SCHEDULE_LIST, HttpMethod.GET, getCommonHeader(), "");
List<BackupSchedule> scheduleList = Arrays.asList(mapper
.readValue(listSchedulesResponse.getEntity(String.class), BackupSchedule[].class));
assertEquals(HttpStatus.SC_OK, listSchedulesResponse.getStatus());
assertTrue(scheduleList.contains(createdBackupSchedule));
cancelSchedule(createdBackupSchedule);
}
@Test
public void shouldReturnConflictWhenCreateScheduleWithExistingAssetContext() throws Exception {
BackupSchedule createdBackupSchedule = submitBackupSchedule();
ClientResponse conflictResponse = client.send(SCHEDULE, HttpMethod.POST, getCommonHeader(),
mapper.writerFor(BackupSchedule.class).writeValueAsString(createdBackupSchedule));
assertEquals(HttpStatus.SC_CONFLICT, conflictResponse.getStatus());
cancelSchedule(createdBackupSchedule);
}
@Test
public void shouldReturnNotFoundForNotExistingSchedule() throws Exception {
String randomString = UUID.randomUUID().toString();
ClientResponse scheduleByIdResponse = client
.send(SCHEDULE + GET_ID + randomString, HttpMethod.GET, getCommonHeader(), "");
assertEquals(HttpStatus.SC_NOT_FOUND, scheduleByIdResponse.getStatus());
}
@Test
public void shouldReturnNotFoundWhenImportNotExistingBackup() throws Exception {
BackupImportRequest backupImportRequest = BackupImportRequest.builder()
.backupStampId(UUID.randomUUID().toString())
.asset(Asset.DATASTORE).build();
String request = mapper.writerFor(BackupImportRequest.class).writeValueAsString(backupImportRequest);
ClientResponse send = client.send(BACKUP_IMPORT, HttpMethod.POST, getCommonHeader(), request);
assertEquals(HttpStatus.SC_NOT_FOUND, send.getStatus());
}
@Test
public void shouldReturnOkWhenListAvailableBackups() throws Exception {
ClientResponse send = client.send(BACKUP_LIST, HttpMethod.GET, getCommonHeader(), "");
assertEquals(HttpStatus.SC_OK, send.getStatus());
}
@Test
public void shouldReturnNotAuthorizedWhenGivenAnonymous() throws Exception {
ClientResponse send = client.send(BACKUP_LIST, HttpMethod.GET, getHeaders(Config.getDataPartitionId(), ""), "");
assertEquals(HttpStatus.SC_UNAUTHORIZED, send.getStatus());
}
@Test
public void shouldReturnNotAuthorizedWhenGivenNoAccess() throws Exception {
ClientResponse send = client
.send(BACKUP_LIST, HttpMethod.GET, getHeaders(Config.getDataPartitionId(), client.getNoDataAccessToken()),
"");
assertEquals(HttpStatus.SC_UNAUTHORIZED, send.getStatus());
}
private BackupSchedule submitBackupSchedule() throws Exception {
ClientResponse createBackupScheduleResponse = client
.send(SCHEDULE, HttpMethod.POST, getCommonHeader(), assetUtil.getBackupScheduleRequest());
BackupSchedule createdBackupSchedule = mapper
.readValue(createBackupScheduleResponse.getEntity(String.class), BackupSchedule.class);
assertEquals(HttpStatus.SC_CREATED, createBackupScheduleResponse.getStatus());
assertFalse(
Objects.isNull(createdBackupSchedule.getScheduleId()) || createdBackupSchedule.getScheduleId().isEmpty());
scheduleIds.add(createdBackupSchedule.getScheduleId());
return createdBackupSchedule;
}
private void cancelSchedule(BackupSchedule schedule) throws Exception {
schedule.setActive(false);
String canceledSchedule = mapper.writerFor(BackupSchedule.class).writeValueAsString(schedule);
ClientResponse send = client
.send(SCHEDULE, HttpMethod.PUT, getCommonHeader(), canceledSchedule);
BackupSchedule schedule1 = mapper
.readValue(send.getEntity(String.class), BackupSchedule.class);
assertEquals(HttpStatus.SC_ACCEPTED, send.getStatus());
assertFalse(schedule1.isActive());
}
}
package org.opengroup.osdu.backup.apitest;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.jersey.api.client.ClientResponse;
import java.util.Arrays;
import java.util.List;
import javax.ws.rs.HttpMethod;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.junit.Test;
import org.opengroup.osdu.backup.Config;
import org.opengroup.osdu.backup.TestBase;
import org.opengroup.osdu.backup.model.BackupCollection;
public class BackupCollectionTest extends TestBase {
private final String API = "/api/backup/v1/collection";
protected static ObjectMapper mapper = new ObjectMapper();
@Test
public void testShouldCreateBackupCollection() throws Exception {
BackupCollection collection = assetUtil.getValidCreateBackupCollection();
BackupCollection submittedCollection = submitBackupCollection(collection);
deleteCollection(submittedCollection);
}
@Test
public void testShouldGetByNameCreatedCollection() throws Exception {
BackupCollection collection = assetUtil.getValidCreateBackupCollection();
BackupCollection submittedCollection = submitBackupCollection(collection);
ClientResponse getCollectionResponse = client
.send(
API + "/" + submittedCollection.getCollectionName(),
HttpMethod.GET,
getValidAuthorizationHeaders()
);
assertEquals(HttpStatus.SC_OK, getCollectionResponse.getStatus());
BackupCollection getCollection = mapper
.readValue(getCollectionResponse.getEntity(String.class), BackupCollection.class);
assertEquals(submittedCollection, getCollection);
deleteCollection(submittedCollection);
}
@Test
public void testShouldUpdateCreatedCollection() throws Exception {
BackupCollection collection = assetUtil.getValidCreateBackupCollection();
BackupCollection submittedCollection = submitBackupCollection(collection);
BackupCollection collectionToUpdate = assetUtil.getValidUpdateBackupCollection(submittedCollection);
ClientResponse updateResponce = client
.send(
API + "/" + collection.getCollectionName(),
HttpMethod.PUT,
getValidAuthorizationHeaders(),
mapper.writeValueAsString(collectionToUpdate)
);
assertEquals(HttpStatus.SC_ACCEPTED, updateResponce.getStatus());
BackupCollection updatedCollection = mapper
.readValue(updateResponce.getEntity(String.class), BackupCollection.class);
assertEquals(collectionToUpdate.getCollectionName(), updatedCollection.getCollectionName());
assertEquals(collectionToUpdate.getInterval(), updatedCollection.getInterval());
assertEquals(collectionToUpdate.getLifetime(), updatedCollection.getLifetime());
assertEquals(collectionToUpdate.isActive(), updatedCollection.isActive());
deleteCollection(updatedCollection);
}
@Test
public void testShouldRespondBadRequestWhenSubmittingMalformedCollection() throws Exception {
BackupCollection malformedCollection = assetUtil.getMalformedBackupCollection();
ClientResponse malformedCollectionResponse = client
.send(
API,
HttpMethod.POST,
getValidAuthorizationHeaders(),
mapper.writeValueAsString(malformedCollection)
);
assertEquals(HttpStatus.SC_BAD_REQUEST, malformedCollectionResponse.getStatus());
}
@Test
public void testShouldRespondBadRequestWhenSubmittingMalformedUpdateCollection() throws Exception {
BackupCollection collection = assetUtil.getValidCreateBackupCollection();
BackupCollection submittedCollection = submitBackupCollection(collection);
BackupCollection collectionToUpdate = assetUtil.getNotValidUpdateBackupCollection(submittedCollection);
ClientResponse updateResponse = client
.send(
API + "/" + collection.getCollectionName(),
HttpMethod.PUT,
getValidAuthorizationHeaders(),
mapper.writeValueAsString(collectionToUpdate)
);
assertEquals(HttpStatus.SC_BAD_REQUEST, updateResponse.getStatus());
deleteCollection(submittedCollection);
}
@Test
public void testShouldReturnListCollectionsWithSubmittedCollection() throws Exception {
BackupCollection collection = assetUtil.getValidCreateBackupCollection();
BackupCollection submittedCollection = submitBackupCollection(collection);
ClientResponse listResponse = client
.send(
API + "/list",
HttpMethod.GET,
getValidAuthorizationHeaders()
);
assertEquals(HttpStatus.SC_OK, listResponse.getStatus());
List<BackupCollection> collectionList = Arrays.asList(mapper
.readValue(listResponse.getEntity(String.class), BackupCollection[].class));
assertTrue(collectionList.contains(submittedCollection));
deleteCollection(submittedCollection);
}
@Test
public void testShouldReturnNotAuthorizedWhenNoTokenProvided() throws Exception {
ClientResponse listResponse = client
.send(
API + "/list",
HttpMethod.GET,
getHeaders(Config.getDataPartitionId(),"")
);
assertEquals(HttpStatus.SC_UNAUTHORIZED, listResponse.getStatus());
}
@Test
public void testShouldReturnNotAuthorizedWhenNoPartitionProvided() throws Exception {
ClientResponse listResponse = client
.send(
API + "/list",
HttpMethod.GET,
getHeaders("",client.getAccessToken())
);
assertEquals(HttpStatus.SC_UNAUTHORIZED, listResponse.getStatus());
}
protected BackupCollection submitBackupCollection(BackupCollection collection) throws Exception {
ClientResponse createBackupCollectionResponse = client
.send(
API,
HttpMethod.POST,
getValidAuthorizationHeaders(),
mapper.writeValueAsString(collection)
);
BackupCollection createdCollection = mapper
.readValue(createBackupCollectionResponse.getEntity(String.class), BackupCollection.class);
assertEquals(HttpStatus.SC_CREATED, createBackupCollectionResponse.getStatus());
assertTrue(StringUtils.isNotBlank(createdCollection.getCollectionId()));
return createdCollection;
}
private void deleteCollection(BackupCollection collection) throws Exception {
ClientResponse response = client
.send(
API + "/" + collection.getCollectionName(),
HttpMethod.DELETE,
getValidAuthorizationHeaders()
);
assertEquals(HttpStatus.SC_NO_CONTENT, response.getStatus());
}
}
package org.opengroup.osdu.backup.util;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.opengroup.osdu.backup.model.BackupCollection;
public abstract class AssetUtil {
public abstract String getBackupScheduleRequest() throws JsonProcessingException;
public abstract BackupCollection getValidCreateBackupCollection();
public abstract BackupCollection getValidUpdateBackupCollection(BackupCollection collection);
public abstract BackupCollection getNotValidUpdateBackupCollection(BackupCollection collection);
public abstract BackupCollection getMalformedBackupCollection();
}
......@@ -28,16 +28,6 @@
<artifactId>backup-test-core</artifactId>
<version>0.0.1</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-datastore</artifactId>
<version>1.105.0</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
<version>1.108.0</version>
</dependency>
<dependency>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-oauth2-http</artifactId>
......
package org.opengroup.osdu.backup;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.opengroup.osdu.backup.apitest.Backup;
import org.opengroup.osdu.backup.util.AssetUtilGCP;
import org.opengroup.osdu.backup.util.DatastoreUtil;
public class TestBackup extends Backup {
private static DatastoreUtil datastoreUtil;
@BeforeAll
static void setUp() {
client = new HttpClientGCP();
assetUtil = new AssetUtilGCP();
datastoreUtil = new DatastoreUtil();
}
@AfterAll
static void tearDown() throws Exception {
DatastoreUtil datastoreUtil1 = datastoreUtil;
for (String scheduleId : scheduleIds) {
datastoreUtil1.deleteSchedule(scheduleId);
}
}
}
package org.opengroup.osdu.backup.util;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableMap;
import java.util.UUID;
import org.opengroup.osdu.backup.Config;
import org.opengroup.osdu.backup.locator.Asset;
import org.opengroup.osdu.backup.model.BackupSchedule;
public class AssetUtilGCP extends AssetUtil {