Commit 77eab59f authored by Yauheni Lesnikau's avatar Yauheni Lesnikau
Browse files

Merge branch 'change-kind-version' into 'master'

fix issue with impossibility of retrieving version with changed kind

See merge request !264
parents 53dc295b 1ee52de6
Pipeline #69967 passed with stages
in 17 minutes and 59 seconds
......@@ -28,6 +28,7 @@ import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.core.common.model.storage.*;
import org.opengroup.osdu.core.common.util.Crc32c;
import org.opengroup.osdu.storage.provider.azure.repository.GroupsInfoRepository;
import org.opengroup.osdu.storage.provider.azure.util.RecordUtil;
import org.opengroup.osdu.storage.provider.interfaces.ICloudStorage;
import org.opengroup.osdu.storage.provider.interfaces.IRecordsMetadataRepository;
import org.slf4j.MDC;
......@@ -67,6 +68,9 @@ public class CloudStorageImpl implements ICloudStorage {
@Autowired
private GroupsInfoRepository groupsInfoRepository;
@Autowired
private RecordUtil recordUtil;
@Autowired
@Named("STORAGE_CONTAINER_NAME")
private String containerName;
......@@ -313,9 +317,9 @@ public class CloudStorageImpl implements ICloudStorage {
return path;
}
private String buildPath(RecordMetadata record, String version)
{
String path = record.getKind() + "/" + record.getId() + "/" + version;
private String buildPath(RecordMetadata record, String version) {
String kind = recordUtil.getKindForVersion(record, version);
String path = kind + "/" + record.getId() + "/" + version;
return path;
}
}
\ No newline at end of file
package org.opengroup.osdu.storage.provider.azure.service;
import org.opengroup.osdu.core.common.model.storage.RecordBulkUpdateParam;
import org.opengroup.osdu.storage.provider.azure.util.RecordIdValidator;
import org.opengroup.osdu.storage.provider.azure.util.RecordUtil;
import org.opengroup.osdu.storage.response.BulkUpdateRecordsResponse;
import org.opengroup.osdu.storage.service.BulkUpdateRecordServiceImpl;
import org.springframework.context.annotation.Primary;
......@@ -11,14 +11,14 @@ import org.springframework.stereotype.Service;
@Primary
public class BulkUpdateRecordServiceAzureImpl extends BulkUpdateRecordServiceImpl {
private final RecordIdValidator recordIdValidator;
private final RecordUtil recordUtil;
public BulkUpdateRecordServiceAzureImpl(RecordIdValidator recordIdValidator) {
this.recordIdValidator = recordIdValidator;
public BulkUpdateRecordServiceAzureImpl(RecordUtil recordUtil) {
this.recordUtil = recordUtil;
}
public BulkUpdateRecordsResponse bulkUpdateRecords(RecordBulkUpdateParam recordBulkUpdateParam, String user) {
recordIdValidator.validateIds(recordBulkUpdateParam.getQuery().getIds());
recordUtil.validateIds(recordBulkUpdateParam.getQuery().getIds());
return super.bulkUpdateRecords(recordBulkUpdateParam, user);
}
......
......@@ -2,7 +2,7 @@ package org.opengroup.osdu.storage.provider.azure.service;
import org.opengroup.osdu.core.common.model.storage.Record;
import org.opengroup.osdu.core.common.model.storage.TransferInfo;
import org.opengroup.osdu.storage.provider.azure.util.RecordIdValidator;
import org.opengroup.osdu.storage.provider.azure.util.RecordUtil;
import org.opengroup.osdu.storage.service.IngestionServiceImpl;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
......@@ -15,15 +15,15 @@ import static java.util.stream.Collectors.toList;
@Primary
public class IngestionServiceAzureImpl extends IngestionServiceImpl {
private final RecordIdValidator recordIdValidator;
private final RecordUtil recordUtil;
public IngestionServiceAzureImpl(RecordIdValidator recordIdValidator) {
this.recordIdValidator = recordIdValidator;
public IngestionServiceAzureImpl(RecordUtil recordUtil) {
this.recordUtil = recordUtil;
}
@Override
public TransferInfo createUpdateRecords(boolean skipDupes, List<Record> inputRecords, String user) {
recordIdValidator.validateIds(inputRecords.stream().map(Record::getId).collect(toList()));
recordUtil.validateIds(inputRecords.stream().map(Record::getId).collect(toList()));
return super.createUpdateRecords(skipDupes, inputRecords, user);
}
}
\ No newline at end of file
package org.opengroup.osdu.storage.provider.azure.util;
import static org.apache.commons.lang3.StringUtils.isNoneBlank;
import org.apache.http.HttpStatus;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.model.storage.RecordMetadata;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
......@@ -9,13 +12,10 @@ import java.util.List;
import java.util.Objects;
@Component
public class RecordIdValidator {
private final int recordIdMaxLength;
public class RecordUtil {
public RecordIdValidator(@Value("${record-id.max.length}") int recordIdMaxLength) {
this.recordIdMaxLength = recordIdMaxLength;
}
@Value("${record-id.max.length}")
private Integer recordIdMaxLength;
public void validateIds(List<String> inputRecords) {
if (inputRecords.stream().filter(Objects::nonNull)
......@@ -24,4 +24,20 @@ public class RecordIdValidator {
throw new AppException(HttpStatus.SC_BAD_REQUEST, "Invalid id", msg);
}
}
public String getKindForVersion(RecordMetadata record, String version) {
String versionPath =
record.getGcsVersionPaths()
.stream()
.filter(path -> isNoneBlank(path) && path.contains(version))
.findFirst()
.orElseThrow(() -> throwVersionNotFound(record.getId(), version));
return versionPath.split("/")[0];
}
private AppException throwVersionNotFound(String id, String version) {
String errorMessage = String.format("The version %s can't be found for record %s", version, id);
throw new AppException(500, "Version not found", errorMessage);
}
}
\ No newline at end of file
......@@ -2,6 +2,7 @@ package org.opengroup.osdu.storage.provider.azure.util;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
......@@ -9,47 +10,94 @@ import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import org.opengroup.osdu.core.common.model.http.AppError;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.model.storage.RecordMetadata;
import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Field;
import java.util.Arrays;
import static java.util.Collections.singletonList;
import static org.junit.Assert.assertEquals;
import static org.springframework.util.ReflectionUtils.findField;
@RunWith(MockitoJUnitRunner.class)
public class RecordIdValidatorTest {
private static final String RECORD_ID_WITH_101_SYMBOLS = "onetwothree";
public class RecordUtilTest {
private static final String RECORD_ID_WITH_11_SYMBOLS = "onetwothree";
private static final String ERROR_REASON = "Invalid id";
private static final String ERROR_MESSAGE = "RecordId values which are exceeded 100 symbols temporarily not allowed";
private static final Long VERSION = 10000L;
private static final String WRONG_VERSION = "11111";
private static final String KIND = "kind";
private static final int RECORD_ID_MAX_LENGTH = 10;
@Rule
public ExpectedException exceptionRule = ExpectedException.none();
private RecordIdValidator recordIdValidator = new RecordIdValidator(RECORD_ID_MAX_LENGTH);
private RecordUtil recordUtil = new RecordUtil();
@Before
public void setup() {
Field recordIdMaxLength = findField(RecordUtil.class, "recordIdMaxLength");
recordIdMaxLength.setAccessible(true);
ReflectionUtils.setField(recordIdMaxLength, recordUtil, RECORD_ID_MAX_LENGTH);
}
@Test
public void shouldFail_CreateUpdateRecords_ifTooLOngRecordIdPresented() {
assertEquals(11, RECORD_ID_WITH_101_SYMBOLS.length());
assertEquals(11, RECORD_ID_WITH_11_SYMBOLS.length());
exceptionRule.expect(AppException.class);
exceptionRule.expect(buildAppExceptionMatcher(ERROR_MESSAGE, ERROR_REASON));
exceptionRule.expect(buildAppExceptionMatcher(ERROR_MESSAGE, ERROR_REASON, 400));
recordIdValidator.validateIds(singletonList(RECORD_ID_WITH_101_SYMBOLS));
recordUtil.validateIds(Arrays.asList(RECORD_ID_WITH_11_SYMBOLS, RECORD_ID_WITH_11_SYMBOLS));
}
@Test
public void shouldDoNothing_ifNullRecordId_passed() {
recordIdValidator.validateIds(singletonList(null));
recordUtil.validateIds(singletonList(null));
}
@Test
public void shouldGetKindForVersion_successFully() {
RecordMetadata record = buildRecordMetadata();
String actualKind = recordUtil.getKindForVersion(record, VERSION.toString());
assertEquals(KIND, actualKind);
}
@Test
public void shouldFailGetKindForVersion_whenVersionNotFound() {
String errorMessage = String.format("The version %s can't be found for record %s",
WRONG_VERSION, RECORD_ID_WITH_11_SYMBOLS);
String errorReason = "Version not found";
RecordMetadata record = buildRecordMetadata();
exceptionRule.expect(AppException.class);
exceptionRule.expect(buildAppExceptionMatcher(errorMessage, errorReason, 500));
recordUtil.getKindForVersion(record, WRONG_VERSION);
}
private RecordMetadata buildRecordMetadata() {
RecordMetadata recordMetadata = new RecordMetadata();
recordMetadata.setId(RECORD_ID_WITH_11_SYMBOLS);
recordMetadata.setKind(KIND);
recordMetadata.addGcsPath(VERSION);
recordMetadata.getGcsVersionPaths().add(null);
return recordMetadata;
}
private Matcher<AppException> buildAppExceptionMatcher(String message, String reason) {
private Matcher<AppException> buildAppExceptionMatcher(String message, String reason, int errorCode) {
return new Matcher<AppException>() {
@Override
public boolean matches(Object o) {
AppException appException = (AppException) o;
AppError error = appException.getError();
return error.getCode() == 400 &&
return error.getCode() == errorCode &&
error.getMessage().equals(message) &&
error.getReason().equals(reason);
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment