Commit 81151760 authored by Aman Verma's avatar Aman Verma
Browse files

implementing method to upsert system schemas

parent 3b7a0530
Pipeline #61710 passed with stages
in 31 minutes and 29 seconds
......@@ -10,6 +10,8 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.sun.net.httpserver.HttpsConfigurator;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONException;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
......@@ -108,7 +110,7 @@ public class SchemaService implements ISchemaService {
try {
schema = schemaStore.getSchema(dataPartitionId, schemaId);
} catch (NotFoundException e) {
schema = schemaStore.getSchema(sharedTenant, schemaId);
schema = schemaStore.getSystemSchema(schemaId);
}
auditLogger.schemaRetrievedSuccess(Collections.singletonList(schema.toString()));
......@@ -136,7 +138,7 @@ public class SchemaService implements ISchemaService {
String dataPartitionId = headers.getPartitionId();
String schemaId = createAndSetSchemaId(schemaRequest);
if (schemaInfoStore.isUnique(schemaId, dataPartitionId)) {
setScope(schemaRequest, dataPartitionId);
setScope(schemaRequest,false);
String schema = resolveAndCheckBreakingChanges(schemaRequest);
......@@ -205,7 +207,7 @@ public class SchemaService implements ISchemaService {
if (SchemaStatus.DEVELOPMENT.equals(schemaInfo.getStatus())) {
log.info(MessageFormat.format(SchemaConstants.SCHEMA_UPDATION_STARTED, createdSchemaId));
setScope(schemaRequest, dataPartitionId);
setScope(schemaRequest, false);
String schema = resolveAndCheckBreakingChanges(schemaRequest);
SchemaInfo schInfo = schemaInfoStore.updateSchemaInfo(schemaRequest);
auditLogger.schemaUpdatedSuccess(Collections.singletonList(schemaRequest.toString()));
......@@ -221,6 +223,41 @@ public class SchemaService implements ISchemaService {
}
private SchemaInfo updateSystemSchema(SchemaRequest schemaRequest) throws ApplicationException, BadRequestException {
String dataPartitionId = headers.getPartitionId();
String createdSchemaId = createAndSetSchemaId(schemaRequest);
SchemaInfo schemaInfo = null;
try {
schemaInfo = schemaInfoStore.getSystemSchemaInfo(createdSchemaId);
} catch (NotFoundException e) {
log.error(SchemaConstants.INVALID_SCHEMA_UPDATE);
if (!SchemaStatus.DEVELOPMENT.equals(schemaRequest.getSchemaInfo().getStatus()))
throw new BadRequestException(SchemaConstants.SCHEMA_PUT_CREATE_EXCEPTION);
throw new NoSchemaFoundException(SchemaConstants.INVALID_SCHEMA_UPDATE);
}
if (SchemaStatus.DEVELOPMENT.equals(schemaInfo.getStatus())) {
log.info(MessageFormat.format(SchemaConstants.SCHEMA_UPDATION_STARTED, createdSchemaId));
setScope(schemaRequest, true);
String schema = resolveAndCheckBreakingChanges(schemaRequest);
SchemaInfo schInfo = schemaInfoStore.updateSystemSchemaInfo(schemaRequest);
auditLogger.schemaUpdatedSuccess(Collections.singletonList(schemaRequest.toString()));
schemaStore.createSystemSchema(schemaRequest.getSchemaInfo().getSchemaIdentity().getId(), schema);
messageBus.publishMessage(createdSchemaId, SchemaConstants.SCHEMA_UPDATE_EVENT_TYPE);
log.info(SchemaConstants.SCHEMA_UPDATED);
return schInfo;
} else {
auditLogger.schemaUpdatedFailure(Collections.singletonList(schemaRequest.toString()));
log.error(SchemaConstants.SCHEMA_UPDATE_ERROR);
throw new BadRequestException(SchemaConstants.SCHEMA_UPDATE_EXCEPTION);
}
}
private String createSchemaId(SchemaRequest schemaRequest) {
SchemaIdentity schemaIdentity = schemaRequest.getSchemaInfo().getSchemaIdentity();
return new StringBuilder().append(schemaIdentity.getAuthority()).append(":").append(schemaIdentity.getSource())
......@@ -315,9 +352,31 @@ public class SchemaService implements ISchemaService {
return upsertBuilder.schemaInfo(response).httpCode(httpCode).build();
}
private SchemaUpsertResponse upsertSystemSchemaInternal(SchemaRequest schemaRequest) throws ApplicationException, BadRequestException {
SchemaInfo response = null;
HttpStatus httpCode = HttpStatus.BAD_REQUEST;
SchemaUpsertResponse.SchemaUpsertResponseBuilder upsertBuilder = SchemaUpsertResponse.builder();
try {
response = updateSystemSchema(schemaRequest);
httpCode = HttpStatus.OK;
} catch (NoSchemaFoundException noSchemaFound) {
try {
response = createSchema(schemaRequest);
httpCode = HttpStatus.CREATED;
}catch (BadRequestException badreqEx) {
//If there is same schema-id for other tenant then throw different error message
if(SchemaConstants.SCHEMA_ID_EXISTS.equals(badreqEx.getMessage()))
throw new BadRequestException(SchemaConstants.INVALID_UPDATE_OPERATION);
throw badreqEx;
}
}
return upsertBuilder.schemaInfo(response).httpCode(httpCode).build();
}
public SchemaUpsertResponse upsertSystemSchema(SchemaRequest schemaRequest) throws ApplicationException, BadRequestException {
updateDataPartitionId();
return this.upsertSchema(schemaRequest);
return this.upsertSystemSchemaInternal(schemaRequest);
}
private void updateDataPartitionId() {
......@@ -348,8 +407,8 @@ public class SchemaService implements ISchemaService {
* @param schemaRequest
* @param dataPartitionId
*/
private void setScope(SchemaRequest schemaRequest, String dataPartitionId) {
if (dataPartitionId.equalsIgnoreCase(sharedTenant)) {
private void setScope(SchemaRequest schemaRequest, Boolean isPublicSchema) {
if (isPublicSchema) {
schemaRequest.getSchemaInfo().setScope(SchemaScope.SHARED);
} else {
schemaRequest.getSchemaInfo().setScope(SchemaScope.INTERNAL);
......
......@@ -152,7 +152,7 @@ public class SchemaServiceTest {
Mockito.when(headers.getPartitionId()).thenReturn(dataPartitionId);
String schemaId = "os..wks..well.1.1";
Mockito.when(schemaStore.getSchema(dataPartitionId, schemaId)).thenThrow(NotFoundException.class);
Mockito.when(schemaStore.getSchema(sharedTenant, schemaId)).thenReturn("{}");
Mockito.when(schemaStore.getSystemSchema(schemaId)).thenReturn("{}");
assertNotNull(schemaService.getSchema(schemaId));
}
......@@ -165,8 +165,8 @@ public class SchemaServiceTest {
Mockito.when(headers.getPartitionId()).thenReturn(dataPartitionId);
String schemaId = "os..wks..well.1.1";
Mockito.when(schemaStore.getSchema(dataPartitionId, schemaId)).thenThrow(NotFoundException.class);
Mockito.when(schemaStore.getSchema(sharedTenant, schemaId))
.thenThrow(new NotFoundException(SchemaConstants.SCHEMA_NOT_PRESENT));
Mockito.when(schemaStore.getSystemSchema(schemaId))
.thenThrow(new NotFoundException(SchemaConstants.SCHEMA_NOT_PRESENT));
schemaService.getSchema(schemaId);
}
......
Markdown is supported
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