Skip to content

Commit

Permalink
Change elide to return response on update requests
Browse files Browse the repository at this point in the history
  • Loading branch information
rpoet-jh committed Sep 25, 2023
1 parent d702d14 commit 53ab4b8
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.HashMap;
import java.util.Objects;
import java.util.Set;
import java.util.TimeZone;
import javax.jms.ConnectionFactory;
import javax.jms.TextMessage;
import javax.json.Json;
Expand All @@ -30,16 +31,27 @@
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.sqs.AmazonSQSClientBuilder;
import com.yahoo.elide.Elide;
import com.yahoo.elide.ElideSettingsBuilder;
import com.yahoo.elide.RefreshableElide;
import com.yahoo.elide.annotation.LifeCycleHookBinding.Operation;
import com.yahoo.elide.annotation.LifeCycleHookBinding.TransactionPhase;
import com.yahoo.elide.core.RequestScope;
import com.yahoo.elide.core.TransactionRegistry;
import com.yahoo.elide.core.audit.Slf4jLogger;
import com.yahoo.elide.core.datastore.DataStore;
import com.yahoo.elide.core.dictionary.EntityDictionary;
import com.yahoo.elide.core.dictionary.Injector;
import com.yahoo.elide.core.exceptions.ErrorMapper;
import com.yahoo.elide.core.filter.dialect.RSQLFilterDialect;
import com.yahoo.elide.core.lifecycle.LifeCycleHook;
import com.yahoo.elide.core.type.Type;
import com.yahoo.elide.core.utils.ClassScanner;
import com.yahoo.elide.core.utils.coerce.CoerceUtil;
import com.yahoo.elide.jsonapi.JsonApiMapper;
import com.yahoo.elide.jsonapi.links.DefaultJSONApiLinks;
import com.yahoo.elide.spring.config.ElideConfigProperties;
import com.yahoo.elide.utils.HeaderUtils;
import org.apache.activemq.broker.BrokerService;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.pass.main.repository.DepositRepository;
Expand All @@ -56,7 +68,9 @@
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.core.JmsTemplate;
Expand Down Expand Up @@ -169,6 +183,57 @@ public BrokerService brokerService(@Value("${spring.activemq.broker-url}") Strin
return brokerService;
}

/**
* This Bean override for RefreshableElide is needed because of the `withUpdate200Status` setting.
* If Elide adds this to the config props, then it can be set application.yml, but until then,
* this is the only way of changing this setting.
* The other setting were pulled from the ElideAutoConfiguration.getRefreshableElide method.
* @param dictionary the elide dictionary
* @param dataStore the elide datastore
* @param headerProcessor the elide header processor
* @param transactionRegistry the elide transaction reg
* @param settings the elide settings
* @param mapper the elide mapper
* @param errorMapper the elide error mapper
* @return the refreshable elide
*/
@Bean
@RefreshScope
@ConditionalOnMissingBean
public RefreshableElide getRefreshableElide(EntityDictionary dictionary, DataStore dataStore,
HeaderUtils.HeaderProcessor headerProcessor,
TransactionRegistry transactionRegistry,
ElideConfigProperties settings, JsonApiMapper mapper,
ErrorMapper errorMapper) {
ElideSettingsBuilder builder = new ElideSettingsBuilder(dataStore)
.withEntityDictionary(dictionary)
.withErrorMapper(errorMapper)
.withJsonApiMapper(mapper)
.withDefaultMaxPageSize(settings.getMaxPageSize())
.withDefaultPageSize(settings.getPageSize())
.withJoinFilterDialect(RSQLFilterDialect.builder().dictionary(dictionary).build())
.withSubqueryFilterDialect(RSQLFilterDialect.builder().dictionary(dictionary).build())
.withAuditLogger(new Slf4jLogger()).withBaseUrl(settings.getBaseUrl())
.withISO8601Dates("yyyy-MM-dd'T'HH:mm'Z'", TimeZone.getTimeZone("UTC"))
.withJsonApiPath(settings.getJsonApi().getPath()).withHeaderProcessor(headerProcessor)
.withGraphQLApiPath(settings.getGraphql().getPath())
.withUpdate200Status();

if (settings.getJsonApi() != null && settings.getJsonApi().isEnabled()
&& settings.getJsonApi().isEnableLinks()) {
String baseUrl = settings.getBaseUrl();
if (StringUtils.isEmpty(baseUrl)) {
builder.withJSONApiLinks(new DefaultJSONApiLinks());
} else {
String jsonApiBaseUrl = baseUrl + settings.getJsonApi().getPath() + "/";
builder.withJSONApiLinks(new DefaultJSONApiLinks(jsonApiBaseUrl));
}
}

Elide elide = new Elide(builder.build(), transactionRegistry, dictionary.getScanner(), true);
return new RefreshableElide(elide);
}

/**
* Override the EnitityDictionary bean in order to add hooks.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public void testCreateUpdateDeleteSubmissionAsShibUser() throws IOException, JSO

Response response = client.newCall(request).execute();

check(response, 204);
check(response, 200);
}

{
Expand Down Expand Up @@ -314,7 +314,7 @@ public void testCreateUpdateDeletePublicationAsShibUser() throws IOException, JS

Response response = client.newCall(request).execute();

check(response, 204);
check(response, 200);
}

{
Expand Down Expand Up @@ -375,7 +375,7 @@ public void testCreateUpdateDeleteFileAsShibUserOwningSubmission() throws IOExce

Response response = client.newCall(request).execute();

check(response, 204);
check(response, 200);
}

{
Expand Down Expand Up @@ -571,7 +571,7 @@ public void testCreateUpdateDeleteGrantAsBackend() throws IOException, JSONExcep

Response response = client.newCall(request).execute();

check(response, 204);
check(response, 200);
}

// Delete the grant
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ public void testUpdateSubmission_OptimisticLocking() throws IOException {
client.createObject(submission);

Submission updateSub1 = client.getObject(submission.getClass(), submission.getId());
Submission updateSub2 = client.getObject(submission.getClass(), submission.getId());
// This is needed to simulate second client get because client.getObject returns same instance of Submission
// in this test.
Submission updateSub2 = new Submission(updateSub1);

updateSub1.setSource(Source.OTHER);
updateSub1.setSubmissionStatus(SubmissionStatus.SUBMITTED);

client.updateObject(updateSub1);

IOException ioException = assertThrows(IOException.class, () -> {
Expand Down Expand Up @@ -102,7 +103,9 @@ public void testUpdateDeposit_OptimisticLocking() throws IOException {
client.createObject(deposit);

Deposit updateDep1 = client.getObject(deposit.getClass(), deposit.getId());
Deposit updateDep2 = client.getObject(deposit.getClass(), deposit.getId());
// This is needed to simulate second client get because client.getObject returns same instance of Deposit
// in this test.
Deposit updateDep2 = new Deposit(updateDep1);

updateDep1.setDepositStatus(DepositStatus.FAILED);
client.updateObject(updateDep1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,7 @@ public <T extends PassEntity> void createObject(T obj) throws IOException {

String id = elide.getMapper().readJsonApiDocument(response.getBody()).getData().getSingleValue().getId();
settings.getDictionary().setId(obj, id);
Object version = elide.getMapper().readJsonApiDocument(response.getBody()).getData().getSingleValue()
.getAttributes().get("version");
if (Objects.nonNull(version)) {
settings.getDictionary().setValue(obj, "version", version);
}
setVersionIfNeeded(response, obj);
}

@Override
Expand All @@ -198,6 +194,16 @@ public <T extends PassEntity> void updateObject(T obj) throws IOException {
if (code < 200 || code > 204) {
throw new IOException("Failed to update object: " + code + " " + response.getBody());
}

setVersionIfNeeded(response, obj);
}

private <T extends PassEntity> void setVersionIfNeeded(ElideResponse response, T obj) throws IOException {
Object version = elide.getMapper().readJsonApiDocument(response.getBody()).getData().getSingleValue()
.getAttributes().get("version");
if (Objects.nonNull(version)) {
settings.getDictionary().setValue(obj, "version", version);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public Deposit(Deposit deposit) {
this.submission = deposit.submission;
this.repository = deposit.repository;
this.repositoryCopy = deposit.repositoryCopy;
this.version = deposit.version;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ public Submission(Submission submission) {
this.preparers = new ArrayList<User>(submission.preparers);
this.grants = new ArrayList<Grant>(submission.grants);
this.effectivePolicies = new ArrayList<>(submission.effectivePolicies);
this.version = submission.version;
}

/**
Expand Down

0 comments on commit 53ab4b8

Please sign in to comment.