CST-12178 notifyservice entity score attribute Patch + tests

This commit is contained in:
frabacche
2023-10-17 14:37:44 +02:00
parent 8b67c77ce8
commit 06611e9158
4 changed files with 35 additions and 29 deletions

View File

@@ -38,6 +38,7 @@ public class NotifyServiceConverter implements DSpaceConverter<NotifyServiceEnti
notifyServiceRest.setUrl(obj.getUrl());
notifyServiceRest.setLdnUrl(obj.getLdnUrl());
notifyServiceRest.setEnabled(obj.isEnabled());
notifyServiceRest.setScore(obj.getScore());
if (obj.getInboundPatterns() != null) {
notifyServiceRest.setNotifyServiceInboundPatterns(

View File

@@ -46,25 +46,25 @@ public class NotifyServiceScoreAddOperation extends PatchOperation<NotifyService
@Override
public NotifyServiceEntity perform(Context context, NotifyServiceEntity notifyServiceEntity, Operation operation)
throws SQLException {
checkNonExistingScoreValue(notifyServiceEntity);
checkOperationValue(operation.getValue());
Object score = operation.getValue();
if (score == null) {
throw new DSpaceBadRequestException("The /score value must be a decimal number");
}
BigDecimal scoreBigDecimal = null;
try {
BigDecimal scoreBigDecimal = new BigDecimal((String)score);
if(scoreBigDecimal.compareTo(java.math.BigDecimal.ZERO) == -1 ||
scoreBigDecimal.compareTo(java.math.BigDecimal.ONE) == 1) {
throw new UnprocessableEntityException(format("Score out of range [0, 1] %s",
scoreBigDecimal.setScale(4).toPlainString()));
}
scoreBigDecimal = new BigDecimal(score.toString());
}catch(Exception e) {
throw new DSpaceBadRequestException(format("Score out of range [0, 1] %s", (String)score));
throw new DSpaceBadRequestException(format("Score out of range [0, 1] %s", score.toString()));
}
checkNonExistingScoreValue(notifyServiceEntity);
notifyServiceEntity.setScore((BigDecimal) score);
if(scoreBigDecimal.compareTo(java.math.BigDecimal.ZERO) == -1 ||
scoreBigDecimal.compareTo(java.math.BigDecimal.ONE) == 1) {
throw new UnprocessableEntityException(format("Score out of range [0, 1] %s",
scoreBigDecimal.setScale(4).toPlainString()));
}
notifyServiceEntity.setScore(scoreBigDecimal);
notifyService.update(context, notifyServiceEntity);
return notifyServiceEntity;
}

View File

@@ -52,14 +52,15 @@ public class NotifyServiceScoreReplaceOperation extends PatchOperation<NotifySer
if (score == null) {
throw new DSpaceBadRequestException("The /score value must be a decimal number");
}
BigDecimal scoreBigDecimal = null;
try {
BigDecimal scoreBigDecimal = new BigDecimal((String)score);
if(scoreBigDecimal.compareTo(java.math.BigDecimal.ZERO) == -1 ||
scoreBigDecimal.compareTo(java.math.BigDecimal.ONE) == 1) {
throw new UnprocessableEntityException(format("Score out of range [0, 1]", (String)score));
}
scoreBigDecimal = new BigDecimal(score.toString());
}catch(Exception e) {
throw new DSpaceBadRequestException(format("Score out of range [0, 1] %s", (String)score));
throw new DSpaceBadRequestException(format("Score out of range [0, 1] %s", score.toString()));
}
if(scoreBigDecimal.compareTo(java.math.BigDecimal.ZERO) == -1 ||
scoreBigDecimal.compareTo(java.math.BigDecimal.ONE) == 1) {
throw new UnprocessableEntityException(format("Score out of range [0, 1]", (String)score));
}
checkModelForExistingValue(notifyServiceEntity);

View File

@@ -12,11 +12,13 @@ import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasJsonPath;
import static org.dspace.app.rest.matcher.NotifyServiceMatcher.matchNotifyService;
import static org.dspace.app.rest.matcher.NotifyServiceMatcher.matchNotifyServicePattern;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.closeTo;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.notNullValue;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
@@ -28,9 +30,8 @@ import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import javax.ws.rs.core.MediaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import javax.ws.rs.core.MediaType;
import org.apache.commons.lang3.RandomUtils;
import org.dspace.app.ldn.NotifyServiceEntity;
@@ -46,6 +47,8 @@ import org.dspace.app.rest.test.AbstractControllerIntegrationTest;
import org.dspace.builder.NotifyServiceBuilder;
import org.junit.Test;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* Integration test class for {@link NotifyServiceRestRepository}.
*
@@ -3287,7 +3290,6 @@ public class NotifyServiceRestRepositoryIT extends AbstractControllerIntegration
@Test
public void NotifyServiceScoreReplaceOperationTest() throws Exception {
context.turnOffAuthorisationSystem();
NotifyServiceEntity notifyServiceEntity =
NotifyServiceBuilder.createNotifyServiceBuilder(context)
@@ -3300,20 +3302,20 @@ public class NotifyServiceRestRepositoryIT extends AbstractControllerIntegration
context.restoreAuthSystemState();
List<Operation> ops = new ArrayList<Operation>();
ReplaceOperation inboundReplaceOperation = new ReplaceOperation("/score", "0.5");
ReplaceOperation inboundReplaceOperation = new ReplaceOperation("/score", "0.522");
ops.add(inboundReplaceOperation);
String patchBody = getPatchContent(ops);
String authToken = getAuthToken(admin.getEmail(), password);
// patch not boolean value
getClient(authToken)
.perform(patch("/api/ldn/ldnservices/" + notifyServiceEntity.getID())
.content(patchBody)
.contentType(MediaType.APPLICATION_JSON_PATCH_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$", matchNotifyService(notifyServiceEntity.getID(), "service name",
"add service description", "service url", "service ldn url", false))
);
"service description", "service url", "service ldn url", false)))
.andExpect(jsonPath("$.score", notNullValue()))
.andExpect(jsonPath("$.score", closeTo(0.522d, 0.001d)));
}
@Test
@@ -3331,12 +3333,11 @@ public class NotifyServiceRestRepositoryIT extends AbstractControllerIntegration
context.restoreAuthSystemState();
List<Operation> ops = new ArrayList<Operation>();
ReplaceOperation inboundReplaceOperation = new ReplaceOperation("/score", BigDecimal.TEN);
ReplaceOperation inboundReplaceOperation = new ReplaceOperation("/score", "10");
ops.add(inboundReplaceOperation);
String patchBody = getPatchContent(ops);
String authToken = getAuthToken(admin.getEmail(), password);
// patch not boolean value
getClient(authToken)
.perform(patch("/api/ldn/ldnservices/" + notifyServiceEntity.getID())
.content(patchBody)
@@ -3353,6 +3354,7 @@ public class NotifyServiceRestRepositoryIT extends AbstractControllerIntegration
NotifyServiceEntity notifyServiceEntity =
NotifyServiceBuilder.createNotifyServiceBuilder(context)
.withName("service name")
.withDescription("service description")
.withUrl("service url")
.withLdnUrl("service ldn url")
.isEnabled(false)
@@ -3360,7 +3362,7 @@ public class NotifyServiceRestRepositoryIT extends AbstractControllerIntegration
context.restoreAuthSystemState();
List<Operation> ops = new ArrayList<Operation>();
AddOperation operation = new AddOperation("/score", BigDecimal.ONE);
AddOperation operation = new AddOperation("/score", "1");
ops.add(operation);
String patchBody = getPatchContent(ops);
@@ -3372,8 +3374,10 @@ public class NotifyServiceRestRepositoryIT extends AbstractControllerIntegration
.contentType(MediaType.APPLICATION_JSON_PATCH_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$", matchNotifyService(notifyServiceEntity.getID(), "service name",
"add service description", "service url", "service ldn url", false))
);
"service description", "service url", "service ldn url", false)))
.andExpect(jsonPath("$.score", notNullValue()))
.andExpect(jsonPath("$.score", closeTo(1d, 0.001d)))
;
}