[DSC-183] Fix checkstyle and refactor

This commit is contained in:
Mykhaylo
2022-12-01 18:51:24 +01:00
parent bdb4bd53dc
commit 141cde9899
19 changed files with 295 additions and 406 deletions

View File

@@ -2,11 +2,14 @@
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
* <p>
*
* http://www.dspace.org/license/
*/
package org.dspace.eperson;
import java.sql.SQLException;
import java.util.List;
import org.apache.logging.log4j.Logger;
import org.dspace.authorize.AuthorizeException;
import org.dspace.core.Context;
@@ -14,8 +17,6 @@ import org.dspace.eperson.dao.SubscriptionParameterDAO;
import org.dspace.eperson.service.SubscriptionParameterService;
import org.springframework.beans.factory.annotation.Autowired;
import java.sql.SQLException;
import java.util.List;
/**
* Class implemennting method for service layer of SubscriptionParameter entity
@@ -42,16 +43,20 @@ public class SubscribeParameterServiceImpl implements SubscriptionParameterServi
}
@Override
public SubscriptionParameter add(Context context, String name, String value, Subscription subscription) throws SQLException, AuthorizeException {
SubscriptionParameter subscriptionParameter = subscriptionParameterDAO.create(context, new SubscriptionParameter());
public SubscriptionParameter add(Context context, String name, String value,
Subscription subscription) throws SQLException, AuthorizeException {
SubscriptionParameter subscriptionParameter =
subscriptionParameterDAO.create(context, new SubscriptionParameter());
subscriptionParameter.setName(name);
subscriptionParameter.setSubscription(subscription);
subscriptionParameter.setValue(value);
return subscriptionParameter;
}
@Override
public SubscriptionParameter edit(Context context,Integer id,String value, String name, Subscription subscription) throws SQLException, AuthorizeException {
SubscriptionParameter subscriptionParameter = subscriptionParameterDAO.findByID(context, SubscriptionParameter.class, id);
public SubscriptionParameter edit(Context context,Integer id,String value,
String name, Subscription subscription) throws SQLException, AuthorizeException {
SubscriptionParameter subscriptionParameter =
subscriptionParameterDAO.findByID(context, SubscriptionParameter.class, id);
subscriptionParameter.setId(id);
subscriptionParameter.setName(name);
subscriptionParameter.setSubscription(subscription);
@@ -67,7 +72,8 @@ public class SubscribeParameterServiceImpl implements SubscriptionParameterServi
@Override
public void deleteSubscriptionParameter(Context context, Integer id) throws SQLException, AuthorizeException {
SubscriptionParameter subscriptionParameter = subscriptionParameterDAO.findByID(context, SubscriptionParameter.class, id);
SubscriptionParameter subscriptionParameter =
subscriptionParameterDAO.findByID(context, SubscriptionParameter.class, id);
if (subscriptionParameter != null) {
subscriptionParameter.setSubscription(null);
subscriptionParameterDAO.delete(context, subscriptionParameter);

View File

@@ -2,7 +2,6 @@
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
* <p>
* http://www.dspace.org/license/
*/
package org.dspace.eperson;
@@ -61,8 +60,8 @@ public class SubscribeServiceImpl implements SubscribeService {
|| ((context.getCurrentUser() != null) && (context
.getCurrentUser().getID().equals(eperson.getID())))) {
Subscription new_subscription = subscriptionDAO.create(context, new Subscription());
subscriptionParameterList.forEach(subscriptionParameter -> new_subscription.addParameter(subscriptionParameter));
// new_subscription.setSubscriptionParameterList(subscriptionParameterList);
subscriptionParameterList.forEach(subscriptionParameter ->
new_subscription.addParameter(subscriptionParameter));
new_subscription.setePerson(eperson);
new_subscription.setdSpaceObject(dSpaceObject);
new_subscription.setType(type);
@@ -75,20 +74,20 @@ public class SubscribeServiceImpl implements SubscribeService {
@Override
public void unsubscribe(Context context, EPerson eperson,
Collection collection) throws SQLException, AuthorizeException {
DSpaceObject dSpaceObject) throws SQLException, AuthorizeException {
// Check authorisation. Must be administrator, or the eperson.
if (authorizeService.isAdmin(context)
|| ((context.getCurrentUser() != null) && (context
.getCurrentUser().getID().equals(eperson.getID())))) {
if (collection == null) {
if (dSpaceObject == null) {
// Unsubscribe from all
subscriptionDAO.deleteByEPerson(context, eperson);
} else {
subscriptionDAO.deleteByCollectionAndEPerson(context, collection, eperson);
subscriptionDAO.deleteByDSOAndEPerson(context, dSpaceObject, eperson);
log.info(LogHelper.getHeader(context, "unsubscribe",
log.info(LogManager.getHeader(context, "unsubscribe",
"eperson_id=" + eperson.getID() + ",collection_id="
+ collection.getID()));
+ dSpaceObject.getID()));
}
} else {
throw new AuthorizeException(
@@ -103,7 +102,8 @@ public class SubscribeServiceImpl implements SubscribeService {
}
@Override
public List<Subscription> getSubscriptionsByEPersonAndDso(Context context, EPerson eperson, DSpaceObject dSpaceObject)
public List<Subscription> getSubscriptionsByEPersonAndDso(Context context,
EPerson eperson, DSpaceObject dSpaceObject)
throws SQLException {
return subscriptionDAO.findByEPerson(context, eperson);
}
@@ -159,7 +159,8 @@ public class SubscribeServiceImpl implements SubscribeService {
subscriptionDB.removeParameterList();
subscriptionDB.setType(type);
subscriptionDB.setdSpaceObject(dSpaceObject);
subscriptionParameterList.forEach(subscriptionParameter -> subscriptionDB.addParameter(subscriptionParameter));
subscriptionParameterList.forEach(subscriptionParameter ->
subscriptionDB.addParameter(subscriptionParameter));
subscriptionDB.setePerson(eperson);
subscriptionDAO.save(context, subscriptionDB);
return subscriptionDB;
@@ -169,10 +170,12 @@ public class SubscribeServiceImpl implements SubscribeService {
}
@Override
public Subscription addSubscriptionParameter(Context context, Integer id, SubscriptionParameter subscriptionParameter) throws SQLException, AuthorizeException {
public Subscription addSubscriptionParameter(Context context, Integer id,
SubscriptionParameter subscriptionParameter) throws SQLException, AuthorizeException {
// must be admin or the subscriber of the subscription
Subscription subscriptionDB = subscriptionDAO.findByID(context, Subscription.class, id);
if (authorizeService.isAdmin(context, context.getCurrentUser()) || subscriptionDB.getePerson().equals(context.getCurrentUser())) {
if (authorizeService.isAdmin(context, context.getCurrentUser())
|| subscriptionDB.getePerson().equals(context.getCurrentUser())) {
subscriptionDB.addParameter(subscriptionParameter);
subscriptionDAO.save(context, subscriptionDB);
return subscriptionDB;
@@ -182,10 +185,12 @@ public class SubscribeServiceImpl implements SubscribeService {
}
@Override
public Subscription removeSubscriptionParameter(Context context, Integer id, SubscriptionParameter subscriptionParameter) throws SQLException, AuthorizeException {
public Subscription removeSubscriptionParameter(Context context, Integer id,
SubscriptionParameter subscriptionParameter) throws SQLException, AuthorizeException {
// must be admin or the subscriber of the subscription
Subscription subscriptionDB = subscriptionDAO.findByID(context, Subscription.class, id);
if (authorizeService.isAdmin(context, context.getCurrentUser()) || subscriptionDB.getePerson().equals(context.getCurrentUser())) {
if (authorizeService.isAdmin(context, context.getCurrentUser())
|| subscriptionDB.getePerson().equals(context.getCurrentUser())) {
subscriptionDB.removeParameter(subscriptionParameter);
subscriptionDAO.save(context, subscriptionDB);
return subscriptionDB;
@@ -200,8 +205,14 @@ public class SubscribeServiceImpl implements SubscribeService {
Subscription subscription = subscriptionDAO.findByID(context, Subscription.class, id);
if (subscription != null) {
// must be admin or the subscriber of the subscription
if (authorizeService.isAdmin(context, context.getCurrentUser()) || subscription.getePerson().equals(context.getCurrentUser())) {
if (authorizeService.isAdmin(context, context.getCurrentUser())
|| subscription.getePerson().equals(context.getCurrentUser())) {
try {
subscriptionDAO.delete(context, subscription);
} catch (SQLException sqlException) {
throw new SQLException(sqlException);
}
} else {
throw new AuthorizeException("Only admin or e-person themselves can delete the subscription");
}

View File

@@ -7,6 +7,8 @@
*/
package org.dspace.eperson;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
@@ -17,17 +19,14 @@ import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import org.dspace.content.Collection;
import org.dspace.content.DSpaceObject;
import org.dspace.core.Context;
import org.dspace.core.ReloadableEntity;
import java.util.ArrayList;
import java.util.List;
/**
* Database entity representation of the subscription table

View File

@@ -9,7 +9,6 @@ package org.dspace.eperson;
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
@@ -31,8 +30,9 @@ import javax.persistence.Table;
public class SubscriptionParameter {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "subscription_parameter_seq")
@SequenceGenerator(name = "subscription_parameter_seq", sequenceName = "subscription_parameter_seq", allocationSize = 1)
@Column(name = "subscription_parameter_id", unique = true, nullable = false, insertable = true)
@SequenceGenerator(name = "subscription_parameter_seq",
sequenceName = "subscription_parameter_seq", allocationSize = 1)
@Column(name = "subscription_parameter_id", unique = true)
private Integer id;
@ManyToOne
@JoinColumn(name = "subscription_id")

View File

@@ -2,7 +2,6 @@
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.eperson.dao;
@@ -10,7 +9,6 @@ package org.dspace.eperson.dao;
import java.sql.SQLException;
import java.util.List;
import org.dspace.content.Collection;
import org.dspace.content.DSpaceObject;
import org.dspace.core.Context;
import org.dspace.core.GenericDAO;
@@ -31,11 +29,12 @@ public interface SubscriptionDAO extends GenericDAO<Subscription> {
public List<Subscription> findByEPerson(Context context, EPerson eperson) throws SQLException;
public List<Subscription> findByEPersonAndDso(Context context, EPerson eperson, DSpaceObject dSpaceObject) throws SQLException;
public List<Subscription> findByEPersonAndDso(Context context,
EPerson eperson, DSpaceObject dSpaceObject) throws SQLException;
public void deleteByEPerson(Context context, EPerson eperson) throws SQLException;
public void deleteByCollectionAndEPerson(Context context, Collection collection, EPerson eperson)
public void deleteByDSOAndEPerson(Context context, DSpaceObject dSpaceObject, EPerson eperson)
throws SQLException;
public List<Subscription> findAllOrderedByEPerson(Context context) throws SQLException;

View File

@@ -6,10 +6,10 @@
* http://www.dspace.org/license/
*/
package org.dspace.eperson.dao;
import org.dspace.core.GenericDAO;
import org.dspace.eperson.SubscriptionParameter;
/**
* Database Access Object interface class for the SubscriptionParamter object.
* The implementation of this class is responsible for all database calls for the SubscriptionParameter object and is

View File

@@ -15,7 +15,6 @@ import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import org.dspace.content.Collection;
import org.dspace.content.DSpaceObject;
import org.dspace.core.AbstractHibernateDAO;
import org.dspace.core.Context;
@@ -47,14 +46,17 @@ public class SubscriptionDAOImpl extends AbstractHibernateDAO<Subscription> impl
}
@Override
public List<Subscription> findByEPersonAndDso(Context context, EPerson eperson, DSpaceObject dSpaceObject) throws SQLException {
public List<Subscription> findByEPersonAndDso(Context context, EPerson eperson,
DSpaceObject dSpaceObject) throws SQLException {
CriteriaBuilder criteriaBuilder = getCriteriaBuilder(context);
javax.persistence.criteria.CriteriaQuery criteriaQuery = getCriteriaQuery(criteriaBuilder, Subscription.class);
javax.persistence.criteria.CriteriaQuery criteriaQuery =
getCriteriaQuery(criteriaBuilder, Subscription.class);
Root<Subscription> subscriptionRoot = criteriaQuery.from(Subscription.class);
criteriaQuery.select(subscriptionRoot);
criteriaQuery.where(criteriaBuilder.equal(subscriptionRoot.get(Subscription_.ePerson), eperson));
criteriaQuery.where(criteriaBuilder.and(criteriaBuilder.equal(subscriptionRoot.get(Subscription_.ePerson), eperson),
criteriaQuery.where(criteriaBuilder.and(criteriaBuilder.equal(
subscriptionRoot.get(Subscription_.ePerson), eperson),
criteriaBuilder.equal(subscriptionRoot.get(Subscription_.dSpaceObject), dSpaceObject)
));
return list(context, criteriaQuery, false, Subscription.class, -1, -1);
@@ -78,11 +80,11 @@ public class SubscriptionDAOImpl extends AbstractHibernateDAO<Subscription> impl
}
@Override
public void deleteByCollectionAndEPerson(Context context, Collection collection, EPerson eperson)
public void deleteByDSOAndEPerson(Context context, DSpaceObject dSpaceObject, EPerson eperson)
throws SQLException {
String hqlQuery = "delete from Subscription where collection=:collection AND ePerson=:ePerson";
String hqlQuery = "delete from Subscription where dSpaceObject=:dSpaceObject AND ePerson=:ePerson";
Query query = createQuery(context, hqlQuery);
query.setParameter("collection", collection);
query.setParameter("dSpaceObject", dSpaceObject);
query.setParameter("ePerson", eperson);
query.executeUpdate();
}

View File

@@ -18,7 +18,8 @@ import org.dspace.eperson.dao.SubscriptionParameterDAO;
*
* @author Alba Aliu at atis.al
*/
public class SubscriptionParameterDAOImpl extends AbstractHibernateDAO<SubscriptionParameter> implements SubscriptionParameterDAO {
public class SubscriptionParameterDAOImpl extends AbstractHibernateDAO
<SubscriptionParameter> implements SubscriptionParameterDAO {
protected SubscriptionParameterDAOImpl() {
super();
}

View File

@@ -2,7 +2,7 @@
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
* <p>
*
* http://www.dspace.org/license/
*/
package org.dspace.eperson.service;
@@ -50,7 +50,7 @@ public interface SubscribeService {
public Subscription subscribe(Context context, EPerson eperson,
DSpaceObject dSpaceObject,
List<SubscriptionParameter> subscriptionParameterList,
String type ) throws SQLException, AuthorizeException;
String type) throws SQLException, AuthorizeException;
/**
* Unsubscribe an e-person to a collection. Passing in <code>null</code>
@@ -59,13 +59,13 @@ public interface SubscribeService {
*
* @param context DSpace context
* @param eperson EPerson to unsubscribe
* @param collection Collection to unsubscribe from
* @param dSpaceObject DSpaceObject to unsubscribe from
* @throws SQLException An exception that provides information on a database access error or other errors.
* @throws AuthorizeException Exception indicating the current user of the context does not have permission
* to perform a particular action.
*/
public void unsubscribe(Context context, EPerson eperson,
Collection collection) throws SQLException, AuthorizeException;
DSpaceObject dSpaceObject) throws SQLException, AuthorizeException;
/**
* Find out which collections an e-person is subscribed to
@@ -86,7 +86,10 @@ public interface SubscribeService {
* @return array of collections e-person is subscribed to and related with dso
* @throws SQLException An exception that provides information on a database access error or other errors.
*/
public List<Subscription> getSubscriptionsByEPersonAndDso(Context context, EPerson eperson, DSpaceObject dSpaceObject) throws SQLException;
public List<Subscription> getSubscriptionsByEPersonAndDso(Context context,
EPerson eperson,
DSpaceObject dSpaceObject) throws SQLException;
/**
* Find out which collections the currently logged in e-person can subscribe to
*
@@ -171,7 +174,10 @@ public interface SubscribeService {
* @param subscriptionParameter SubscriptionParameter subscriptionParameter
* @throws SQLException An exception that provides information on a database access error or other errors.
*/
public Subscription addSubscriptionParameter(Context context, Integer id, SubscriptionParameter subscriptionParameter) throws SQLException, AuthorizeException;
public Subscription addSubscriptionParameter(Context context, Integer id,
SubscriptionParameter subscriptionParameter)
throws SQLException, AuthorizeException;
/**
* Deletes a parameter from subscription
*
@@ -180,7 +186,8 @@ public interface SubscribeService {
* @param subscriptionParameter SubscriptionParameter subscriptionParameter
* @throws SQLException An exception that provides information on a database access error or other errors.
*/
public Subscription removeSubscriptionParameter(Context context, Integer id, SubscriptionParameter subscriptionParameter) throws SQLException, AuthorizeException;
public Subscription removeSubscriptionParameter(Context context, Integer id,
SubscriptionParameter subscriptionParameter) throws SQLException, AuthorizeException;
/**
* Deletes a subscription

View File

@@ -7,18 +7,18 @@
*/
package org.dspace.eperson.service;
import java.sql.SQLException;
import java.util.List;
import org.dspace.authorize.AuthorizeException;
import org.dspace.core.Context;
import org.dspace.eperson.Subscription;
import org.dspace.eperson.SubscriptionParameter;
import java.sql.SQLException;
import java.util.List;
/**
* Service interface class for the SubscriptionParameter object.
* The implementation of this class is responsible for all business logic calls for the SubscriptionParameter object and is
* autowired by spring
* The implementation of this class is responsible for all business logic calls for
* the SubscriptionParameter object and is autowired by spring
* Class defining methods for sending new item e-mail alerts to users
*
* @author Alba Aliu @atis.al

View File

@@ -7,6 +7,10 @@
*/
package org.dspace.app.rest.converter;
import java.util.ArrayList;
import java.util.List;
import org.dspace.app.rest.model.SubscriptionParameterRest;
import org.dspace.app.rest.model.SubscriptionRest;
import org.dspace.app.rest.projection.Projection;
import org.dspace.app.rest.utils.Utils;
@@ -24,12 +28,9 @@ import org.springframework.stereotype.Component;
public class SubscriptionConverter implements DSpaceConverter<Subscription, SubscriptionRest> {
@Autowired
protected Utils utils;
/* (non-Javadoc)
* @see org.dspace.app.rest.converter.DSpaceConverter#convert
* (java.lang.Object, org.dspace.app.rest.projection.Projection)
*/
@Autowired
private ConverterService converter;
@Override
public SubscriptionRest convert(Subscription subscription, Projection projection) {
SubscriptionRest rest = new SubscriptionRest();

View File

@@ -2,28 +2,23 @@
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
* <p>
*
* http://www.dspace.org/license/
*/
package org.dspace.app.rest.model;
import org.dspace.app.rest.RestResourceController;
import java.util.ArrayList;
import java.util.List;
@LinksRest(links = {
@LinkRest(
name = SubscriptionRest.DSPACE_OBJECT,
method = "getDSpaceObject"
),
@LinkRest(
import org.dspace.app.rest.RestResourceController;
@LinksRest(links = {@LinkRest(name = SubscriptionRest.DSPACE_OBJECT,
method = "getDSpaceObject"), @LinkRest(
name = SubscriptionRest.EPERSON,
method = "getEPerson"
)
method = "getEPerson")
})
public class SubscriptionRest extends BaseObjectRest<Integer>{
public class SubscriptionRest extends BaseObjectRest<Integer> {
public static final String NAME = "subscription";
public static final String NAME_PLURAL = "subscriptions";
public static final String CATEGORY = "core";
@@ -60,6 +55,7 @@ public class SubscriptionRest extends BaseObjectRest<Integer>{
public void setSubscriptionParameterList(List<SubscriptionParameter> subscriptionParameterList) {
this.subscriptionParameterList = subscriptionParameterList;
}
public String getSubscriptionType() {
return this.type;
}

View File

@@ -2,15 +2,18 @@
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
* <p>
*
* http://www.dspace.org/license/
*/
package org.dspace.app.rest.repository;
import java.sql.SQLException;
import javax.annotation.Nullable;
import javax.servlet.http.HttpServletRequest;
import org.dspace.app.rest.model.DSpaceObjectRest;
import org.dspace.app.rest.model.SubscriptionRest;
import org.dspace.app.rest.projection.Projection;
import org.dspace.content.service.ItemService;
import org.dspace.core.Context;
import org.dspace.eperson.Subscription;
import org.dspace.eperson.service.SubscribeService;
@@ -21,10 +24,6 @@ import org.springframework.data.domain.Pageable;
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
import org.springframework.stereotype.Component;
import javax.annotation.Nullable;
import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
/**
* Link repository for "dataSpaceObject" of subscription
*/

View File

@@ -7,9 +7,10 @@
*/
package org.dspace.app.rest.repository;
import java.sql.SQLException;
import javax.annotation.Nullable;
import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import org.dspace.app.rest.model.EPersonRest;
import org.dspace.app.rest.model.SubscriptionRest;
import org.dspace.app.rest.projection.Projection;
@@ -21,8 +22,6 @@ import org.springframework.data.domain.Pageable;
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
import org.springframework.stereotype.Component;
/**
* Link repository for "eperson" of subscription
*/

View File

@@ -2,11 +2,20 @@
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
* <p>
*
* http://www.dspace.org/license/
*/
package org.dspace.app.rest.repository;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.logging.log4j.LogManager;
@@ -15,6 +24,9 @@ import org.dspace.app.rest.SearchRestMethod;
import org.dspace.app.rest.converter.ConverterService;
import org.dspace.app.rest.exception.UnprocessableEntityException;
import org.dspace.app.rest.model.SubscriptionRest;
import org.dspace.app.rest.model.patch.Patch;
import org.dspace.app.rest.repository.patch.ResourcePatch;
import org.dspace.app.rest.utils.DSpaceObjectUtils;
import org.dspace.authorize.AuthorizeException;
import org.dspace.authorize.service.AuthorizeService;
import org.dspace.content.DSpaceObject;
@@ -43,7 +55,8 @@ import java.util.List;
*/
@Component(SubscriptionRest.CATEGORY + "." + SubscriptionRest.NAME)
public class SubscriptionRestRepository extends DSpaceRestRepository<SubscriptionRest, Integer> {
public class SubscriptionRestRepository extends DSpaceRestRepository
<SubscriptionRest, Integer> implements LinkRestRepository {
private static final Logger log = LogManager.getLogger();
@Autowired
AuthorizeService authorizeService;
@@ -60,7 +73,7 @@ public class SubscriptionRestRepository extends DSpaceRestRepository<Subscriptio
@Override
@PreAuthorize("hasAuthority('ADMIN')")
@PreAuthorize("isAuthenticated()")
public SubscriptionRest findOne(Context context, Integer id) {
try {
Subscription subscription = subscribeService.findById(context, id);
@@ -89,7 +102,8 @@ public class SubscriptionRestRepository extends DSpaceRestRepository<Subscriptio
try {
Context context = obtainContext();
EPerson ePerson = personService.findByIdOrLegacyId(context, id);
if (context.getCurrentUser().equals(ePerson) || authorizeService.isAdmin(context, context.getCurrentUser())) {
if (context.getCurrentUser().equals(ePerson)
|| authorizeService.isAdmin(context, context.getCurrentUser())) {
List<Subscription> subscriptionList = subscribeService.getSubscriptionsByEPerson(context, ePerson);
return converter.toRestPage(subscriptionList, pageable, utils.obtainProjection());
} else {
@@ -113,9 +127,12 @@ public class SubscriptionRestRepository extends DSpaceRestRepository<Subscriptio
if (dsoId == null || epersonId == null) {
throw new UnprocessableEntityException("error parsing the body");
}
if (context.getCurrentUser().equals(ePerson) || authorizeService.isAdmin(context, context.getCurrentUser())) {
List<Subscription> subscriptionList = subscribeService.getSubscriptionsByEPerson(context, ePerson);
return converter.toRestPage(subscriptionList, pageable, utils.obtainProjection());
if (context.getCurrentUser().equals(ePerson)
|| authorizeService.isAdmin(context, context.getCurrentUser())) {
List<Subscription> subscriptionList =
subscribeService.getSubscriptionsByEPersonAndDso(context, ePerson, dSpaceObject);
return converter.toRestPage(subscriptionList, pageable, subscriptionList.size(),
utils.obtainProjection());
} else {
throw new AuthorizeException("Only admin or e-person themselves can search for it's subscription");
}
@@ -184,9 +201,18 @@ public class SubscriptionRestRepository extends DSpaceRestRepository<Subscriptio
} catch (SQLException e) {
throw new ResourceNotFoundException(notFoundException);
}
if (id.equals(subscriptionRest.getId())) {
subscription = subscribeService.updateSubscription(context, subscriptionRest.getId(), null,
null, subscriptionRest.getSubscriptionParameterList(), subscriptionRest.getType());
if (id.equals(subscription.getID())) {
List<SubscriptionParameter> subscriptionParameterList = new ArrayList<>();
for (SubscriptionParameterRest subscriptionParameterRest :
subscriptionRest.getSubscriptionParameterList()) {
SubscriptionParameter subscriptionParameter = new SubscriptionParameter();
subscriptionParameter.setSubscription(subscription);
subscriptionParameter.setValue(subscriptionParameterRest.getValue());
subscriptionParameter.setName(subscriptionParameterRest.getName());
subscriptionParameterList.add(subscriptionParameter);
}
subscription = subscribeService.updateSubscription(context, id, ePerson,
dSpaceObject, subscriptionParameterList, subscriptionRest.getSubscriptionType());
return converter.toRest(subscription, utils.obtainProjection());
} else {
throw new IllegalArgumentException("The id in the Json and the id in the url do not match: "
@@ -197,8 +223,9 @@ public class SubscriptionRestRepository extends DSpaceRestRepository<Subscriptio
@Override
@PreAuthorize("isAuthenticated()")
public void patch(Context context, HttpServletRequest request, String apiCategory, String model, Integer id, Patch patch)
throws UnprocessableEntityException, DSpaceBadRequestException {
public void patch(Context context, HttpServletRequest request, String apiCategory,
String model, Integer id, Patch patch)
throws UnprocessableEntityException, DSpaceBadRequestException, AuthorizeException {
Subscription subscription = null;
try {
subscription = subscribeService.findById(context, id);

View File

@@ -2,11 +2,13 @@
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
* <p>
*
* http://www.dspace.org/license/
*/
package org.dspace.app.rest.repository.patch.operation;
import java.sql.SQLException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.dspace.app.rest.exception.DSpaceBadRequestException;
@@ -21,7 +23,7 @@ import org.dspace.eperson.service.SubscribeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.sql.SQLException;
/**
* Implementation for SubscriptionParameterAddOperation patches.
@@ -50,7 +52,8 @@ public class SubscriptionParameterAddOperation extends PatchOperation<Subscripti
} else {
value = objectMapper.readTree((String) operation.getValue());
}
SubscriptionParameterRest subscriptionParameterRest = objectMapper.readValue(value.toString(), SubscriptionParameterRest.class);
SubscriptionParameterRest subscriptionParameterRest =
objectMapper.readValue(value.toString(), SubscriptionParameterRest.class);
SubscriptionParameter subscriptionParameter = new SubscriptionParameter();
subscriptionParameter.setSubscription(subscription);
subscriptionParameter.setValue(subscriptionParameterRest.getValue());

View File

@@ -2,11 +2,12 @@
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
* <p>
*
* http://www.dspace.org/license/
*/
package org.dspace.app.rest.repository.patch.operation;
import java.sql.SQLException;
import org.dspace.app.rest.exception.DSpaceBadRequestException;
import org.dspace.app.rest.exception.RESTAuthorizationException;
@@ -20,8 +21,6 @@ import org.dspace.eperson.service.SubscriptionParameterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.sql.SQLException;
/**
* Implementation for SubscriptionParameterRemoveOperation patches.
* <p>

View File

@@ -2,11 +2,13 @@
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
* <p>
*
* http://www.dspace.org/license/
*/
package org.dspace.app.rest.repository.patch.operation;
import java.sql.SQLException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.dspace.app.rest.exception.DSpaceBadRequestException;
@@ -22,9 +24,6 @@ import org.dspace.eperson.service.SubscriptionParameterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.sql.SQLException;
/**
* Implementation for SubscriptionParameterReplaceOperation patches.
* <p>
@@ -54,9 +53,11 @@ public class SubscriptionParameterReplaceOperation extends PatchOperation<Subscr
} else {
value = objectMapper.readTree((String) operation.getValue());
}
SubscriptionParameterRest subscriptionParameterRest = objectMapper.readValue(value.toString(), SubscriptionParameterRest.class);
SubscriptionParameterRest subscriptionParameterRest = objectMapper.readValue(
value.toString(), SubscriptionParameterRest.class);
try {
SubscriptionParameter subscriptionParameter = subscriptionParameterService.edit(context, subscriptionParameterId, subscriptionParameterRest.getValue(),
SubscriptionParameter subscriptionParameter = subscriptionParameterService.edit(context,
subscriptionParameterId, subscriptionParameterRest.getValue(),
subscriptionParameterRest.getName(),
subscription);
} catch (SQLException | AuthorizeException exception) {

View File

@@ -2,7 +2,7 @@
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
* <p>
*
* http://www.dspace.org/license/
*/
package org.dspace.app.rest;
@@ -27,10 +27,10 @@ import org.dspace.content.service.SiteService;
import org.dspace.eperson.EPerson;
import org.dspace.eperson.Subscription;
import org.dspace.eperson.SubscriptionParameter;
import org.dspace.eperson.service.SubscribeService;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
import org.springframework.http.MediaType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
@@ -41,7 +41,6 @@ import java.util.List;
import java.util.Map;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
@@ -56,21 +55,29 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
* (Class has to start or end with IT to be picked up by the failsafe plugin)
*/
public class SubscriptionRestRepositoryIT extends AbstractControllerIntegrationTest {
@Autowired
SubscribeService subscribeService;
private Collection collection;
private Item publicItem;
@Override
@Before
public void setUp() throws Exception {
super.setUp();
// We turn off the authorization system in order to create the structure as
// defined below
// context.turnOffAuthorisationSystem();
// parentCommunity = CommunityBuilder.createCommunity(context).withName("Parent Community").build();
// colPeople = CollectionBuilder.createCollection(context, parentCommunity).withName("People")
// .withEntityType("Person").build();
// context.restoreAuthSystemState();
context.turnOffAuthorisationSystem();
parentCommunity = CommunityBuilder.createCommunity(context)
.withName("Parent Community")
.build();
Community community = CommunityBuilder.createSubCommunity(context, parentCommunity)
.withName("Sub Community")
.build();
collection = CollectionBuilder.createCollection(context, community).withName("Collection 1").build();
// creation of the item which will be the DSO related with a subscription
publicItem = ItemBuilder.createItem(context, collection)
.withTitle("Test")
.withIssueDate("2010-10-17")
.withAuthor("Smith, Donald")
.withSubject("ExtraEntry")
.build();
context.restoreAuthSystemState();
}
@Test
@@ -80,28 +87,13 @@ public class SubscriptionRestRepositoryIT extends AbstractControllerIntegrationT
getClient().perform(get("/api/core/subscriptions"))
//The status has to be 401 Not Authorized
.andExpect(status().isUnauthorized());
String token = getAuthToken(admin.getEmail(), password);
parentCommunity = CommunityBuilder.createCommunity(context)
.withName("Parent Community")
.build();
Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity)
.withName("Sub Community")
.build();
Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
// creation of the item which will be the DSO related with a subscription
Item publicItem1 = ItemBuilder.createItem(context, col1)
.withTitle("Test")
.withIssueDate("2010-10-17")
.withAuthor("Smith, Donald")
.withSubject("ExtraEntry")
.build();
List<SubscriptionParameter> subscriptionParameterList = new ArrayList<>();
SubscriptionParameter subscriptionParameter = new SubscriptionParameter();
subscriptionParameter.setName("Frequency");
subscriptionParameter.setValue("Daily");
subscriptionParameterList.add(subscriptionParameter);
Subscription subscription = subscribeService.subscribe(context, admin, publicItem1, subscriptionParameterList, "TypeTest");
Subscription subscription = SubscribeBuilder.subscribeBuilder(context, "TypeTest", publicItem, admin, subscriptionParameterList).build();
subscriptionParameter.setSubscription(subscription);
//When we call the root endpoint
context.restoreAuthSystemState();
@@ -127,33 +119,18 @@ public class SubscriptionRestRepositoryIT extends AbstractControllerIntegrationT
@Test
public void findByIdAsAdministrator() throws Exception {
context.turnOffAuthorisationSystem();
//When we call the root endpoint as anonymous user
getClient().perform(get("/api/core/subscriptions"))
//The status has to be 403 Not Authorized
.andExpect(status().isUnauthorized());
String token = getAuthToken(admin.getEmail(), password);
context.turnOffAuthorisationSystem();
parentCommunity = CommunityBuilder.createCommunity(context)
.withName("Parent Community")
.build();
Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity)
.withName("Sub Community")
.build();
Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
// creation of the item which will be the DSO related with a subscription
Item publicItem1 = ItemBuilder.createItem(context, col1)
.withTitle("Item")
.withIssueDate("2020-10-17")
.withAuthor("John, Doe")
.withSubject("Test")
.build();
List<SubscriptionParameter> subscriptionParameterList = new ArrayList<>();
SubscriptionParameter subscriptionParameter = new SubscriptionParameter();
subscriptionParameter.setName("Parameter");
subscriptionParameter.setValue("ValueParameter");
subscriptionParameterList.add(subscriptionParameter);
Subscription subscription = subscribeService.subscribe(context, admin, publicItem1, subscriptionParameterList, "TestType");
Subscription subscription = SubscribeBuilder.subscribeBuilder(context, "TestType", publicItem, admin, subscriptionParameterList).build();
context.restoreAuthSystemState();
//When we call the root endpoint
getClient(token).perform(get("/api/core/subscriptions/" + subscription.getID()))
@@ -175,34 +152,19 @@ public class SubscriptionRestRepositoryIT extends AbstractControllerIntegrationT
}
@Test
public void findByIdAsRandomUser() throws Exception {
String token = getAuthToken(admin.getEmail(), password);
public void findByIdAsAnonymous() throws Exception {
context.turnOffAuthorisationSystem();
parentCommunity = CommunityBuilder.createCommunity(context)
.withName("Parent Community")
.build();
Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity)
.withName("Sub Community")
.build();
Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
// creation of the item which will be the DSO related with a subscription
Item publicItem1 = ItemBuilder.createItem(context, col1)
.withTitle("Item")
.withIssueDate("2020-10-17")
.withAuthor("John, Doe")
.withSubject("Test")
.build();
List<SubscriptionParameter> subscriptionParameterList = new ArrayList<>();
SubscriptionParameter subscriptionParameter = new SubscriptionParameter();
subscriptionParameter.setName("Parameter");
subscriptionParameter.setValue("ValueParameter");
subscriptionParameterList.add(subscriptionParameter);
Subscription subscription = subscribeService.subscribe(context, admin, publicItem1, subscriptionParameterList, "TestType");
Subscription subscription = SubscribeBuilder.subscribeBuilder(context, "TestType", publicItem, admin, subscriptionParameterList).build();
context.restoreAuthSystemState();
//When we call the root endpoint
getClient(token).perform(get("/api/core/subscriptions/" + subscription.getID()))
//The status has to be 403
.andExpect(status().isForbidden());
getClient().perform(get("/api/core/subscriptions/" + subscription.getID()))
//The status has to be 401
.andExpect(status().isUnauthorized());
}
@Test
@@ -213,26 +175,16 @@ public class SubscriptionRestRepositoryIT extends AbstractControllerIntegrationT
// .andExpect(status().isUnauthorized());
String token = getAuthToken(eperson.getEmail(), password);
context.turnOffAuthorisationSystem();
parentCommunity = CommunityBuilder.createCommunity(context)
.withName("Parent Community")
.build();
Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity)
.withName("Sub Community")
.build();
Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
// creation of the item which will be the DSO related with a subscription
Item publicItem1 = ItemBuilder.createItem(context, col1)
.withTitle("Item")
.withIssueDate("2020-10-17")
.withAuthor("John, Doe")
.withSubject("Test")
EPerson user = EPersonBuilder.createEPerson(context)
.withEmail("user@test.it")
.withPassword(password)
.build();
List<SubscriptionParameter> subscriptionParameterList = new ArrayList<>();
SubscriptionParameter subscriptionParameter = new SubscriptionParameter();
subscriptionParameter.setName("Parameter1");
subscriptionParameter.setValue("ValueParameter1");
subscriptionParameterList.add(subscriptionParameter);
Subscription subscription = subscribeService.subscribe(context, eperson, publicItem1, subscriptionParameterList, "TestType");
Subscription subscription = SubscribeBuilder.subscribeBuilder(context, "TestType", publicItem, user, subscriptionParameterList).build();
context.restoreAuthSystemState();
//When we call the root endpoint
getClient(token).perform(get("/api/core/subscriptions/search/findByEPerson?id=" + eperson.getID()))
@@ -242,14 +194,13 @@ public class SubscriptionRestRepositoryIT extends AbstractControllerIntegrationT
.andExpect(jsonPath("$.page.totalElements", greaterThanOrEqualTo(1)))
.andExpect(jsonPath("$.page.totalPages", greaterThanOrEqualTo(1)))
.andExpect(jsonPath("$.page.number", is(0)))
.andExpect(jsonPath("$._embedded.subscriptions[0].subscriptionType", is("TypeTest")))
.andExpect(jsonPath("$._embedded.subscriptions[0].subscriptionType", is("TestType")))
.andExpect(jsonPath("$._embedded.subscriptions[0]._links.dSpaceObject.href", Matchers.startsWith(REST_SERVER_URL + "core/subscriptions")))
.andExpect(jsonPath("$._embedded.subscriptions[0]._links.dSpaceObject.href", Matchers.endsWith("dSpaceObject")))
.andExpect(jsonPath("$._embedded.subscriptions[0]._links.ePerson.href", Matchers.startsWith(REST_SERVER_URL + "core/subscriptions")))
.andExpect(jsonPath("$._embedded.subscriptions[0]._links.ePerson.href", Matchers.endsWith("ePerson")))
.andExpect(jsonPath("$._embedded.subscriptions[0].subscriptionParameterList[0].name", is("Parameter1")))
.andExpect(jsonPath("$._embedded.subscriptions[0].subscriptionParameterList[0].value", is("ValueParameter1")))
.andExpect(jsonPath("$._links.self.href", Matchers.is(REST_SERVER_URL + "core/subscriptions")));
.andExpect(jsonPath("$._embedded.subscriptions[0].subscriptionParameterList[0].value", is("ValueParameter1")));
EPerson epersonIT = EPersonBuilder.createEPerson(context)
@@ -257,30 +208,28 @@ public class SubscriptionRestRepositoryIT extends AbstractControllerIntegrationT
.withPassword(password)
.withLanguage("al")
.build();
String epersonITtoken = getAuthToken(epersonIT.getEmail(), password);
getClient(epersonITtoken).perform(get("/api/core/subscriptions/" + subscription.getID()))
List<SubscriptionParameter> subscriptionParameterList = new ArrayList<>();
SubscriptionParameter subscriptionParameter = new SubscriptionParameter();
subscriptionParameter.setName("Parameter1");
subscriptionParameter.setValue("ValueParameter1");
subscriptionParameterList.add(subscriptionParameter);
List<SubscriptionParameter> subscriptionParameterList1 = new ArrayList<>();
SubscriptionParameter subscriptionParameter1 = new SubscriptionParameter();
subscriptionParameter1.setName("Parameter1");
subscriptionParameter1.setValue("ValueParameter1");
subscriptionParameterList1.add(subscriptionParameter1);
Subscription subscription = SubscribeBuilder.subscribeBuilder(context, "TestType", collection, user, subscriptionParameterList).build();
Subscription subscription1 = SubscribeBuilder.subscribeBuilder(context, "Test", collection, user, subscriptionParameterList1).build();
context.restoreAuthSystemState();
//When we call the root endpoint
String token = getAuthToken(admin.getEmail(), password);
getClient(token).perform(get("/api/core/subscriptions/search/findByEPersonAndDso?dspace_object_id=" + collection.getID() + "&eperson_id=" + user.getID()))
//The status has to be 200 OK
.andExpect(status().isUnauthorized());
}
@Test
public void addSubscriptionNotLoggedIn() throws Exception {
context.turnOffAuthorisationSystem();
parentCommunity = CommunityBuilder.createCommunity(context)
.withName("Parent Community")
.build();
Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity)
.withName("Sub Community")
.build();
Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
// creation of the item which will be the DSO related with a subscription
Item publicItem1 = ItemBuilder.createItem(context, col1)
.withTitle("Item")
.withIssueDate("2020-10-17")
.withAuthor("John, Doe")
.withSubject("Test")
.build();
context.restoreAuthSystemState();
SubscriptionParameterRest subscriptionParameterRest = new SubscriptionParameterRest();
subscriptionParameterRest.setValue("nameTest");
subscriptionParameterRest.setName("valueTest");
@@ -289,10 +238,10 @@ public class SubscriptionRestRepositoryIT extends AbstractControllerIntegrationT
SubscriptionRest subscriptionRest = new SubscriptionRest();
subscriptionRest.setType("testType");
MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
params.add("dspace_object_id", publicItem1.getID().toString());
params.add("dspace_object_id", publicItem.getID().toString());
params.add("eperson_id", eperson.getID().toString());
ObjectMapper objectMapper = new ObjectMapper();
getClient().perform(post("/api/core/subscriptions?dspace_object_id="+publicItem1.getID()+"&eperson_id="+eperson.getID())
getClient().perform(post("/api/core/subscriptions?dspace_object_id=" + publicItem.getID() + "&eperson_id=" + eperson.getID())
.content(objectMapper.writeValueAsString(subscriptionRest))
.contentType(contentType))
//The status has to be 401 Not Authorized
@@ -301,22 +250,6 @@ public class SubscriptionRestRepositoryIT extends AbstractControllerIntegrationT
@Test
public void addSubscriptionAsAdmin() throws Exception {
context.turnOffAuthorisationSystem();
parentCommunity = CommunityBuilder.createCommunity(context)
.withName("Parent Community")
.build();
Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity)
.withName("Sub Community")
.build();
Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
// creation of the item which will be the DSO related with a subscription
Item publicItem1 = ItemBuilder.createItem(context, col1)
.withTitle("Item")
.withIssueDate("2020-10-17")
.withAuthor("John, Doe")
.withSubject("Test")
.build();
context.restoreAuthSystemState();
//When we call the root endpoint as anonymous user
SubscriptionParameterRest subscriptionParameterRest = new SubscriptionParameterRest();
subscriptionParameterRest.setValue("nameTest");
@@ -328,9 +261,17 @@ public class SubscriptionRestRepositoryIT extends AbstractControllerIntegrationT
// subscriptionRest.setSubscriptionParameterList(subscriptionParameterRestList);
ObjectMapper objectMapper = new ObjectMapper();
String token = getAuthToken(admin.getEmail(), password);
getClient(token).perform(post("/api/core/subscriptions?dspace_object_id="+publicItem1.getID()+"&eperson_id="+admin.getID())
.content(objectMapper.writeValueAsString(subscriptionRest))
.contentType(contentType))
Map<String, Object> map = new HashMap<>();
map.put("type", "test");
List<Map<String, Object>> list = new ArrayList<>();
Map<String, Object> sub_list = new HashMap<>();
sub_list.put("name", "frequency");
sub_list.put("value", "daily");
list.add(sub_list);
map.put("subscriptionParameterList", list);
getClient(token).perform(post("/api/core/subscriptions?dspace_object_id=" + publicItem.getID() + "&eperson_id=" + admin.getID())
.content(objectMapper.writeValueAsString(map))
.contentType(MediaType.APPLICATION_JSON_VALUE))
//The status has to be 200 OK
.andExpect(status().isOk())
//We expect the content type to be "application/hal+json;charset=UTF-8"
@@ -349,29 +290,27 @@ public class SubscriptionRestRepositoryIT extends AbstractControllerIntegrationT
@Test
public void editSubscriptionAnonymous() throws Exception {
context.turnOffAuthorisationSystem();
parentCommunity = CommunityBuilder.createCommunity(context)
.withName("Parent Community")
.build();
Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity)
.withName("Sub Community")
.build();
Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
// creation of the item which will be the DSO related with a subscription
Item publicItem1 = ItemBuilder.createItem(context, col1)
.withTitle("Item")
.withIssueDate("2020-10-17")
.withAuthor("John, Doe")
.withSubject("Test")
.build();
List<SubscriptionParameter> subscriptionParameterList = new ArrayList<>();
SubscriptionParameter subscriptionParameter = new SubscriptionParameter();
subscriptionParameter.setName("Parameter1");
subscriptionParameter.setValue("ValueParameter1");
subscriptionParameterList.add(subscriptionParameter);
Subscription subscription = subscribeService.subscribe(context, eperson, publicItem1, subscriptionParameterList, "TestType");
Subscription subscription = SubscribeBuilder.subscribeBuilder(context, "TestType", publicItem, admin, subscriptionParameterList).build();
ObjectMapper objectMapper = new ObjectMapper();
String token = getAuthToken(admin.getEmail(), password);
Map<String, Object> newSubscription = new HashMap<>();
newSubscription.put("type", "test");
List<Map<String, Object>> list = new ArrayList<>();
Map<String, Object> sub_list = new HashMap<>();
sub_list.put("name", "frequency");
sub_list.put("value", "daily");
list.add(sub_list);
newSubscription.put("subscriptionParameterList", list);
context.restoreAuthSystemState();
//When we call the root endpoint as anonymous user
getClient().perform(put("/api/core/subscriptions"))
getClient().perform(put("/api/core/subscriptions/" + subscription.getID() + "?dspace_object_id=" + publicItem.getID() + "&eperson_id=" + admin.getID())
.content(objectMapper.writeValueAsString(newSubscription))
.contentType(MediaType.APPLICATION_JSON_VALUE))
//The status has to be 403 Not Authorized
.andExpect(status().isUnauthorized());
}
@@ -384,30 +323,17 @@ public class SubscriptionRestRepositoryIT extends AbstractControllerIntegrationT
.withPassword(password)
.withLanguage("al")
.build();
String epersonITtoken = getAuthToken(epersonIT.getEmail(), password);
parentCommunity = CommunityBuilder.createCommunity(context)
.withName("Parent Community")
.build();
Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity)
.withName("Sub Community")
.build();
Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
// creation of the item which will be the DSO related with a subscription
Item publicItem1 = ItemBuilder.createItem(context, col1)
.withTitle("Item")
.withIssueDate("2020-10-17")
.withAuthor("John, Doe")
.withSubject("Test")
.build();
List<SubscriptionParameter> subscriptionParameterList = new ArrayList<>();
SubscriptionParameter subscriptionParameter = new SubscriptionParameter();
subscriptionParameter.setName("Parameter1");
subscriptionParameter.setValue("ValueParameter1");
subscriptionParameterList.add(subscriptionParameter);
Subscription subscription = subscribeService.subscribe(context, eperson, publicItem1, subscriptionParameterList, "TestType");
Subscription subscription = SubscribeBuilder.subscribeBuilder(context, "TestType", publicItem, eperson, subscriptionParameterList).build();
context.restoreAuthSystemState();
//When we call the root endpoint as anonymous user
getClient(epersonITtoken).perform(put("/api/core/subscriptions"))
getClient().perform(put("/api/core/subscriptions/" + subscription.getID() + "?dspace_object_id=" + publicItem.getID() + "&eperson_id=" + admin.getID())
.content(objectMapper.writeValueAsString(newSubscription))
.contentType(MediaType.APPLICATION_JSON_VALUE))
//The status has to be 403 Not Authorized
.andExpect(status().isUnauthorized());
}
@@ -417,46 +343,39 @@ public class SubscriptionRestRepositoryIT extends AbstractControllerIntegrationT
String tokenAdmin = getAuthToken(admin.getEmail(), password);
String tokenSubscriber = getAuthToken(eperson.getEmail(), password);
context.turnOffAuthorisationSystem();
parentCommunity = CommunityBuilder.createCommunity(context)
.withName("Parent Community")
.build();
Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity)
.withName("Sub Community")
.build();
Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
// creation of the item which will be the DSO related with a subscription
Item publicItem1 = ItemBuilder.createItem(context, col1)
.withTitle("Item")
.withIssueDate("2020-10-17")
.withAuthor("John, Doe")
.withSubject("Test")
.build();
List<SubscriptionParameter> subscriptionParameterList = new ArrayList<>();
SubscriptionParameter subscriptionParameter = new SubscriptionParameter();
subscriptionParameter.setName("Frequency");
subscriptionParameter.setValue("Daily");
subscriptionParameterList.add(subscriptionParameter);
Subscription subscription = subscribeService.subscribe(context, eperson, publicItem1, subscriptionParameterList, "Test");
Subscription subscription = SubscribeBuilder.subscribeBuilder(context, "TestType", publicItem, eperson, subscriptionParameterList).build();
context.restoreAuthSystemState();
getClient(tokenSubscriber).perform(put("/api/core/subscriptions"))
//The status has to be 403 Not Authorized
.andExpect(status().isOk());
//When we call the root endpoint as anonymous user
getClient(tokenAdmin).perform(put("/api/core/subscriptions"))
ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> newSubscription = new HashMap<>();
newSubscription.put("type", "test");
List<Map<String, Object>> list = new ArrayList<>();
Map<String, Object> sub_list = new HashMap<>();
sub_list.put("name", "frequency");
sub_list.put("value", "daily");
list.add(sub_list);
newSubscription.put("subscriptionParameterList", list);
String tokenSubscriber = getAuthToken(eperson.getEmail(), password);
getClient(tokenSubscriber).perform(put("/api/core/subscriptions/" + subscription.getID() + "?dspace_object_id=" + publicItem.getID() + "&eperson_id=" + eperson.getID())
//The status has to be 403 Not Authorized
.content(objectMapper.writeValueAsString(newSubscription))
.contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isOk())
//We expect the content type to be "application/hal+json;charset=UTF-8"
.andExpect(content().contentType(contentType))
//By default we expect at least 1 submission forms so this to be reflected in the page object
.andExpect(jsonPath("$.type", is("Test")))
.andExpect(jsonPath("$.id", Matchers.endsWith(REST_SERVER_URL + "/api/core/dSpaceObject")))
.andExpect(jsonPath("$.subscriptionParameterList[0].name", is("Frequency")))
.andExpect(jsonPath("$.subscriptionParameterList[0].value", is("Daily")))
.andExpect(jsonPath("$._links.self.href", Matchers.startsWith(REST_SERVER_URL + "/api/core/subscriptions")))
.andExpect(jsonPath("$._links.dSpaceObject.href", Matchers.startsWith(REST_SERVER_URL + "/api/core/subscriptions")))
.andExpect(jsonPath("$._links.dSpaceObject.href", Matchers.endsWith(REST_SERVER_URL + "/api/core/dSpaceObject")))
.andExpect(jsonPath("$._links.ePerson.href", Matchers.startsWith(REST_SERVER_URL + "/api/core/subscriptions")))
.andExpect(jsonPath("$._links.ePerson.href", Matchers.endsWith(REST_SERVER_URL + "/api/core/ePerson")));
.andExpect(jsonPath("$.subscriptionType", is("test")))
.andExpect(jsonPath("$.subscriptionParameterList[0].name", is("frequency")))
.andExpect(jsonPath("$.subscriptionParameterList[0].value", is("daily")))
.andExpect(jsonPath("$._links.self.href", Matchers.startsWith(REST_SERVER_URL + "core/subscriptions")))
.andExpect(jsonPath("$._links.dSpaceObject.href", Matchers.startsWith(REST_SERVER_URL + "core/subscriptions")))
.andExpect(jsonPath("$._links.dSpaceObject.href", Matchers.endsWith("/dSpaceObject")))
.andExpect(jsonPath("$._links.ePerson.href", Matchers.startsWith(REST_SERVER_URL + "core/subscriptions")))
.andExpect(jsonPath("$._links.ePerson.href", Matchers.endsWith("/ePerson")));
}
@Test
@@ -468,26 +387,12 @@ public class SubscriptionRestRepositoryIT extends AbstractControllerIntegrationT
.withLanguage("al")
.build();
String epersonITtoken = getAuthToken(epersonIT.getEmail(), password);
parentCommunity = CommunityBuilder.createCommunity(context)
.withName("Parent Community")
.build();
Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity)
.withName("Sub Community")
.build();
Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
// creation of the item which will be the DSO related with a subscription
Item publicItem1 = ItemBuilder.createItem(context, col1)
.withTitle("Item")
.withIssueDate("2020-10-17")
.withAuthor("John, Doe")
.withSubject("Test")
.build();
List<SubscriptionParameter> subscriptionParameterList = new ArrayList<>();
SubscriptionParameter subscriptionParameter = new SubscriptionParameter();
subscriptionParameter.setName("Frequency");
subscriptionParameter.setValue("Daily");
subscriptionParameterList.add(subscriptionParameter);
Subscription subscription = subscribeService.subscribe(context, eperson, publicItem1, subscriptionParameterList, "Test");
Subscription subscription = SubscribeBuilder.subscribeBuilder(context, "Test", publicItem, eperson, subscriptionParameterList).build();
context.restoreAuthSystemState();
getClient(epersonITtoken).perform(put("/api/core/subscriptions"))
//The status has to be 403 Not Authorized
@@ -497,26 +402,13 @@ public class SubscriptionRestRepositoryIT extends AbstractControllerIntegrationT
@Test
public void deleteSubscriptionAsAdmin() throws Exception {
context.turnOffAuthorisationSystem();
parentCommunity = CommunityBuilder.createCommunity(context)
.withName("Parent Community")
.build();
Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity)
.withName("Sub Community")
.build();
Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
// creation of the item which will be the DSO related with a subscription
Item publicItem1 = ItemBuilder.createItem(context, col1)
.withTitle("Item")
.withIssueDate("2020-10-17")
.withAuthor("John, Doe")
.withSubject("Test")
.build();
List<SubscriptionParameter> subscriptionParameterList = new ArrayList<>();
SubscriptionParameter subscriptionParameter = new SubscriptionParameter();
subscriptionParameter.setName("Frequency");
subscriptionParameter.setValue("Daily");
subscriptionParameterList.add(subscriptionParameter);
Subscription subscription = subscribeService.subscribe(context, eperson, publicItem1, subscriptionParameterList, "Test");
Subscription subscription = SubscribeBuilder.subscribeBuilder(context, "Test", publicItem, eperson, subscriptionParameterList).build();
String token = getAuthToken(admin.getEmail(), password);
context.restoreAuthSystemState();
String token = getAuthToken(admin.getEmail(), password);
getClient(token).perform(put("/api/core/subscriptions"))
@@ -526,26 +418,13 @@ public class SubscriptionRestRepositoryIT extends AbstractControllerIntegrationT
@Test
public void patchReplaceSubscriptionParameterAsAdmin() throws Exception {
parentCommunity = CommunityBuilder.createCommunity(context)
.withName("Parent Community")
.build();
Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity)
.withName("Sub Community")
.build();
Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
// creation of the item which will be the DSO related with a subscription
Item publicItem1 = ItemBuilder.createItem(context, col1)
.withTitle("Item")
.withIssueDate("2020-10-17")
.withAuthor("John, Doe")
.withSubject("Test")
.build();
context.turnOffAuthorisationSystem();
List<SubscriptionParameter> subscriptionParameterList = new ArrayList<>();
SubscriptionParameter subscriptionParameter = new SubscriptionParameter();
subscriptionParameter.setName("TestName");
subscriptionParameter.setValue("TestValue");
subscriptionParameterList.add(subscriptionParameter);
Subscription subscription = subscribeService.subscribe(context, eperson, publicItem1, subscriptionParameterList, "Test");
Subscription subscription = SubscribeBuilder.subscribeBuilder(context, "Test", publicItem, eperson, subscriptionParameterList).build();
String token = getAuthToken(admin.getEmail(), password);
List<Operation> ops = new ArrayList<Operation>();
Map<String, String> value = new HashMap<>();
@@ -575,26 +454,13 @@ public class SubscriptionRestRepositoryIT extends AbstractControllerIntegrationT
@Test
public void patchSubscriptionParameterNotAsAdminNotAsSubscriber() throws Exception {
parentCommunity = CommunityBuilder.createCommunity(context)
.withName("Parent Community")
.build();
Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity)
.withName("Sub Community")
.build();
Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
// creation of the item which will be the DSO related with a subscription
Item publicItem1 = ItemBuilder.createItem(context, col1)
.withTitle("Item")
.withIssueDate("2020-10-17")
.withAuthor("John, Doe")
.withSubject("Test")
.build();
context.turnOffAuthorisationSystem();
List<SubscriptionParameter> subscriptionParameterList = new ArrayList<>();
SubscriptionParameter subscriptionParameter = new SubscriptionParameter();
subscriptionParameter.setName("TestName");
subscriptionParameter.setValue("TestValue");
subscriptionParameterList.add(subscriptionParameter);
Subscription subscription = subscribeService.subscribe(context, eperson, publicItem1, subscriptionParameterList, "Test");
Subscription subscription = SubscribeBuilder.subscribeBuilder(context, "Test", publicItem, eperson, subscriptionParameterList).build();
List<Operation> ops = new ArrayList<Operation>();
Map<String, String> value = new HashMap<>();
value.put("name", "frequency");
@@ -611,32 +477,19 @@ public class SubscriptionRestRepositoryIT extends AbstractControllerIntegrationT
getClient(epersonITtoken).perform(patch("/api/core/subscriptions/" + subscription.getID())
.content(patchBody)
)
//The status has to be 200 OK
.andExpect(status().isUnauthorized());
//The status has to be 403 Forbidden
.andExpect(status().isForbidden());
}
@Test
public void patchAddSubscriptionParameter() throws Exception {
parentCommunity = CommunityBuilder.createCommunity(context)
.withName("Parent Community")
.build();
Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity)
.withName("Sub Community")
.build();
Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
// creation of the item which will be the DSO related with a subscription
Item publicItem1 = ItemBuilder.createItem(context, col1)
.withTitle("Item")
.withIssueDate("2020-10-17")
.withAuthor("John, Doe")
.withSubject("Test")
.build();
context.turnOffAuthorisationSystem();
List<SubscriptionParameter> subscriptionParameterList = new ArrayList<>();
SubscriptionParameter subscriptionParameter = new SubscriptionParameter();
subscriptionParameter.setName("TestName");
subscriptionParameter.setValue("TestValue");
subscriptionParameterList.add(subscriptionParameter);
Subscription subscription = subscribeService.subscribe(context, eperson, publicItem1, subscriptionParameterList, "Test");
Subscription subscription = SubscribeBuilder.subscribeBuilder(context, "Test", publicItem, eperson, subscriptionParameterList).build();
String token = getAuthToken(admin.getEmail(), password);
List<Operation> ops = new ArrayList<Operation>();
Map<String, String> value = new HashMap<>();
@@ -669,26 +522,12 @@ public class SubscriptionRestRepositoryIT extends AbstractControllerIntegrationT
@Test
public void patchRemoveSubscriptionParameter() throws Exception {
context.turnOffAuthorisationSystem();
parentCommunity = CommunityBuilder.createCommunity(context)
.withName("Parent Community")
.build();
Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity)
.withName("Sub Community")
.build();
Collection col1 = CollectionBuilder.createCollection(context, child1).withName("Collection 1").build();
// creation of the item which will be the DSO related with a subscription
Item publicItem1 = ItemBuilder.createItem(context, col1)
.withTitle("Item")
.withIssueDate("2020-10-17")
.withAuthor("John, Doe")
.withSubject("Test")
.build();
List<SubscriptionParameter> subscriptionParameterList = new ArrayList<>();
SubscriptionParameter subscriptionParameter = new SubscriptionParameter();
subscriptionParameter.setName("TestName");
subscriptionParameter.setValue("TestValue");
subscriptionParameterList.add(subscriptionParameter);
Subscription subscription = subscribeService.subscribe(context, eperson, publicItem1, subscriptionParameterList, "Test");
Subscription subscription = SubscribeBuilder.subscribeBuilder(context, "Test", publicItem, eperson, subscriptionParameterList).build();
String token = getAuthToken(admin.getEmail(), password);
List<Operation> ops = new ArrayList<Operation>();
Map<String, String> value = new HashMap<>();