Code development platform for open source projects from the European Union institutions :large_blue_circle: EU Login authentication by SMS has been phased out. To see alternatives please check here

Skip to content
Snippets Groups Projects
Commit 81e1e9ed authored by Joze RIHTARSIC's avatar Joze RIHTARSIC
Browse files

Cleaning the code

parent fdf70bec
Branches
Tags
No related merge requests found
Pipeline #72607 passed with warnings
Showing
with 54 additions and 1799 deletions
...@@ -21,8 +21,8 @@ import java.util.ArrayList; ...@@ -21,8 +21,8 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
@Service @Service
public class UIServiceGroupSearchService extends UIServiceBase<DBResource, ServiceGroupSearchRO> { public class UIResourceSearchService extends UIServiceBase<DBResource, ServiceGroupSearchRO> {
private static final SMPLogger LOG = SMPLoggerFactory.getLogger(UIServiceGroupSearchService.class); private static final SMPLogger LOG = SMPLoggerFactory.getLogger(UIResourceSearchService.class);
@Autowired @Autowired
DomainDao domainDao; DomainDao domainDao;
......
package eu.europa.ec.edelivery.smp.services.ui;
import eu.europa.ec.edelivery.smp.conversion.IdentifierService;
import eu.europa.ec.edelivery.smp.data.dao.BaseDao;
import eu.europa.ec.edelivery.smp.data.dao.DomainDao;
import eu.europa.ec.edelivery.smp.data.dao.ResourceDao;
import eu.europa.ec.edelivery.smp.data.dao.UserDao;
import eu.europa.ec.edelivery.smp.data.model.DBDomainResourceDef;
import eu.europa.ec.edelivery.smp.data.model.doc.DBResource;
import eu.europa.ec.edelivery.smp.data.model.doc.DBSubresource;
import eu.europa.ec.edelivery.smp.data.ui.*;
import eu.europa.ec.edelivery.smp.data.ui.enums.EntityROStatus;
import eu.europa.ec.edelivery.smp.data.ui.enums.SMLStatusEnum;
import eu.europa.ec.edelivery.smp.exceptions.SMPRuntimeException;
import eu.europa.ec.edelivery.smp.identifiers.Identifier;
import eu.europa.ec.edelivery.smp.logging.SMPLogger;
import eu.europa.ec.edelivery.smp.logging.SMPLoggerFactory;
import eu.europa.ec.edelivery.smp.security.ResourceGuard;
import eu.europa.ec.edelivery.smp.services.ConfigurationService;
import eu.europa.ec.edelivery.smp.services.SMLIntegrationService;
import eu.europa.ec.edelivery.smp.services.ui.filters.ResourceFilter;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.UnsupportedEncodingException;
import java.util.*;
import static eu.europa.ec.edelivery.smp.exceptions.ErrorCode.*;
@Service
public class UIServiceGroupService extends UIServiceBase<DBResource, ServiceGroupRO> {
private static final SMPLogger LOG = SMPLoggerFactory.getLogger(UIServiceGroupService.class);
protected final DomainDao domainDao;
protected final ResourceDao serviceGroupDao;
protected final UserDao userDao;
protected final IdentifierService identifierService;
protected final SMLIntegrationService smlIntegrationService;
protected final ConfigurationService configurationService;
protected final ResourceGuard resourceGuard;
public UIServiceGroupService(DomainDao domainDao,
ResourceDao serviceGroupDao,
UserDao userDao,
IdentifierService identifierService,
SMLIntegrationService smlIntegrationService,
ConfigurationService configurationService,
ResourceGuard resourceGuard) {
this.domainDao = domainDao;
this.serviceGroupDao = serviceGroupDao;
this.userDao = userDao;
this.identifierService = identifierService;
this.smlIntegrationService = smlIntegrationService;
this.configurationService = configurationService;
this.resourceGuard = resourceGuard;
}
@Override
protected BaseDao<DBResource> getDatabaseDao() {
return serviceGroupDao;
}
/**
* Method return list of service group entities with service metadata for given search parameters and page.
*
* @param page
* @param pageSize
* @param sortField
* @param sortOrder
* @param filter
* @return
*/
@Transactional
public ServiceResult<ServiceGroupRO> getTableList(int page, int pageSize,
String sortField,
String sortOrder, ResourceFilter filter) {
ServiceResult<ServiceGroupRO> sg = new ServiceResult<>();
sg.setPage(page < 0 ? 0 : page);
sg.setPageSize(pageSize);
long iCnt = serviceGroupDao.getServiceGroupCount(filter);
sg.setCount(iCnt);
if (iCnt > 0) {
int iStartIndex = pageSize < 0 ? -1 : page * pageSize;
if (iStartIndex >= iCnt && page > 0) {
page = page - 1;
sg.setPage(page); // go back for a page
iStartIndex = pageSize < 0 ? -1 : page * pageSize;
}
List<DBResource> lst = serviceGroupDao.getServiceGroupList(iStartIndex, pageSize, sortField, sortOrder, filter);
List<ServiceGroupRO> lstRo = new ArrayList<>();
for (DBResource dbServiceGroup : lst) {
ServiceGroupRO serviceGroupRo = convertToRo(dbServiceGroup);
serviceGroupRo.setStatus(EntityROStatus.PERSISTED.getStatusNumber());
serviceGroupRo.setIndex(iStartIndex++);
lstRo.add(serviceGroupRo);
}
sg.getServiceEntities().addAll(lstRo);
}
return sg;
}
@Transactional
public ServiceGroupRO getServiceGroupById(Long serviceGroupId) {
DBResource dbServiceGroup = getDatabaseDao().find(serviceGroupId);
dbServiceGroup.getSubresources().size();
return convertToRo(dbServiceGroup);
}
@Transactional
public ServiceGroupRO getOwnedServiceGroupById(Long userId, Long serviceGroupId) {
DBResource dbServiceGroup = getDatabaseDao().find(serviceGroupId);
if (resourceGuard.isResourceAdmin(userId, dbServiceGroup)) {
convertToRo(dbServiceGroup);
}
return null;
}
@Transactional
public ServiceGroupValidationRO getServiceGroupExtensionById(Long serviceGroupId) {
/*
ServiceGroupValidationRO ex = new ServiceGroupValidationRO();
DBResource dbServiceGroup = getDatabaseDao().find(serviceGroupId);
ex.setServiceGroupId(dbServiceGroup.getId());
ex.setParticipantIdentifier(dbServiceGroup.getIdentifierValue());
ex.setParticipantScheme(dbServiceGroup.getIdentifierScheme());
if (dbServiceGroup.getExtension() != null) {
ex.setExtension(getConvertExtensionToString(serviceGroupId, dbServiceGroup.getExtension()));
}
return ex;
*/
return null;
}
private String getConvertExtensionToString(Long id, byte[] extension) {
try {
return new String(extension, "UTF-8");
} catch (UnsupportedEncodingException e) {
LOG.error("Error converting the extension to String for id:" + id, e);
return null;
}
}
/**
* Method validates and converts UI resource object entity to database entity and persists it to database
*
* @param serviceGroupRO
*/
protected List<ParticipantSMLRecord> addNewServiceGroup(ServiceGroupRO serviceGroupRO) {
// normalize identifiers
normalizeIdentifiers(serviceGroupRO);
DBResource dbServiceGroup = new DBResource();
dbServiceGroup.setIdentifierValue(serviceGroupRO.getParticipantIdentifier());
dbServiceGroup.setIdentifierScheme(serviceGroupRO.getParticipantScheme());
// add users
updateUsersOnServiceGroup(serviceGroupRO, dbServiceGroup);
// first update domains
// validate (if domains are added only once) and create domain list for service group.
List<ParticipantSMLRecord> listOfActions = createDomainsForNewServiceGroup(serviceGroupRO, dbServiceGroup);
/*
// sort service metadata by domain
List<ServiceMetadataRO> serviceMetadataROList = serviceGroupRO.getServiceMetadata();
serviceMetadataROList.forEach(serviceMetadataRO -> {
// find the domain
Optional<DBDomainResourceDef> dbServiceGroupDomain = dbServiceGroup.getServiceGroupForDomain(serviceMetadataRO.getDomainCode());
if (dbServiceGroupDomain.isPresent()) {
dbServiceGroupDomain.get().addServiceMetadata(createServiceMetadataFromRo(serviceMetadataRO));
} else {
throw new SMPRuntimeException(SG_NOT_REGISTRED_FOR_DOMAIN, serviceMetadataRO.getDomainCode(),
serviceGroupRO.getParticipantIdentifier(), serviceGroupRO.getParticipantScheme());
}
});
// add extension
if (serviceGroupRO.getExtension() != null) {
byte[] buff = validateExtension(serviceGroupRO);
dbServiceGroup.setExtension(buff);
}
*/
getDatabaseDao().persistFlushDetach(dbServiceGroup);
return listOfActions;
}
private void normalizeIdentifiers(ServiceGroupRO sgo) {
Identifier pti = identifierService.normalizeParticipant(sgo.getParticipantScheme(),
sgo.getParticipantIdentifier());
sgo.setParticipantScheme(pti.getScheme());
sgo.setParticipantIdentifier(pti.getValue());
sgo.getServiceMetadata().forEach(smd -> {
Identifier dit = identifierService.normalizeDocument(smd.getDocumentIdentifierScheme(),
smd.getDocumentIdentifier());
smd.setDocumentIdentifierScheme(dit.getScheme());
smd.setDocumentIdentifier(dit.getValue());
});
}
/**
* Validate (if domains are added only once) and create domain list for service group.
*
* @param serviceGroupRO
* @param resource
*/
protected List<ParticipantSMLRecord> createDomainsForNewServiceGroup(ServiceGroupRO serviceGroupRO, DBResource resource) {
List<ParticipantSMLRecord> participantSMLRecordList = new ArrayList<>();
// first update domains
List<ServiceGroupDomainRO> serviceGroupDomainROList = serviceGroupRO.getServiceGroupDomains();
/*
// validate (if domains are added only once) and create domain list for service group.
serviceGroupDomainROList.forEach(dro -> {
// everything ok find domain and add it to service group
Optional<DBDomain> dmn = domainDao.getDomainByCode(dro.getDomainCode());
if (dmn.isPresent()) {
DBDomainResourceDef domain = resource.addDomain(dmn.get());
participantSMLRecordList.add(new ParticipantSMLRecord(SMLStatusEnum.REGISTER,
serviceGroupRO.getParticipantIdentifier(),
serviceGroupRO.getParticipantScheme(),
domain.getDomain()));
} else {
throw new SMPRuntimeException(DOMAIN_NOT_EXISTS, dro.getDomainCode());
}
});
*/
return participantSMLRecordList;
}
/**
* Method converts UI resource object entity to database entity and update changes to database
*
* @param serviceGroupRO
*/
protected List<ParticipantSMLRecord> updateServiceGroup(ServiceGroupRO serviceGroupRO, boolean serviceGroupAdmin) {
// normalize identifiers
normalizeIdentifiers(serviceGroupRO);
// find and validate service group
DBResource dbServiceGroup = findAndValidateServiceGroup(serviceGroupRO);
List<ParticipantSMLRecord> participantSMLRecordList = Collections.emptyList();
if (serviceGroupAdmin) {
// update users
updateUsersOnServiceGroup(serviceGroupRO, dbServiceGroup);
// update domain
participantSMLRecordList = updateDomainsForServiceGroup(serviceGroupRO, dbServiceGroup);
}
/*
//update service metadata
List<ServiceMetadataRO> serviceMetadataROList = serviceGroupRO.getServiceMetadata();
serviceMetadataROList.forEach(serviceMetadataRO -> {
Optional<DBDomainResourceDef> optionalDbServiceGroupDomain =
dbServiceGroup.findServiceGroupDomainForMetadata(serviceMetadataRO.getDocumentIdentifier(), serviceMetadataRO.getDocumentIdentifierScheme());
// remove service metadata
if (serviceMetadataRO.getStatus() == EntityROStatus.REMOVE.getStatusNumber()) {
// if the domain was not removed then remove only metadata
if (optionalDbServiceGroupDomain.isPresent()) {
DBDomainResourceDef dbServiceGroupDomain = optionalDbServiceGroupDomain.get();
// remove from domain
dbServiceGroupDomain.removeServiceMetadata(serviceMetadataRO.getDocumentIdentifier(),
serviceMetadataRO.getDocumentIdentifierScheme());
}
} else if (serviceMetadataRO.getStatus() == EntityROStatus.NEW.getStatusNumber()) {
// add to new service group.. find servicegroup domain by code
optionalDbServiceGroupDomain = dbServiceGroup.getServiceGroupForDomain(serviceMetadataRO.getDomainCode());
if (optionalDbServiceGroupDomain.isPresent()) {
optionalDbServiceGroupDomain.get().addServiceMetadata(createServiceMetadataFromRo(serviceMetadataRO));
} else {
throw new SMPRuntimeException(SG_NOT_REGISTRED_FOR_DOMAIN, serviceMetadataRO.getDomainCode(),
serviceGroupRO.getParticipantIdentifier(), serviceGroupRO.getParticipantScheme());
}
} else if (serviceMetadataRO.getStatus() == EntityROStatus.UPDATED.getStatusNumber()) {
if (optionalDbServiceGroupDomain.isPresent()) {
DBDomainResourceDef dbServiceGroupDomain = optionalDbServiceGroupDomain.get();
DBSubresource DBSubresource = dbServiceGroupDomain.getServiceMetadata(serviceMetadataRO.getDocumentIdentifier(),
serviceMetadataRO.getDocumentIdentifierScheme());
if (serviceMetadataRO.getXmlContentStatus() == EntityROStatus.UPDATED.getStatusNumber()) {
// get service metadata
byte[] buff = validateServiceMetadata(serviceMetadataRO);
DBSubresource.setXmlContent(buff);
}
if (!Objects.equals(serviceMetadataRO.getDomainCode(), dbServiceGroupDomain.getDomain().getDomainCode())) {
// remove from old domain
LOG.info("Move service metadata from domain {} to domain: {}", dbServiceGroupDomain.getDomain().getDomainCode(),
serviceMetadataRO.getDomainCode());
DBSubresource smd = dbServiceGroupDomain.removeServiceMetadata(serviceMetadataRO.getDocumentIdentifier(),
serviceMetadataRO.getDocumentIdentifierScheme());
// find new domain and add
Optional<DBDomainResourceDef> optNewDomain = dbServiceGroup.getServiceGroupForDomain(serviceMetadataRO.getDomainCode());
if (optNewDomain.isPresent()) {
LOG.info("ADD service metadata to domain {} ", optNewDomain.get().getDomain().getDomainCode(),
serviceMetadataRO.getDomainCode());
// create new because the old service metadata will be deleted
DBSubresource smdNew = new DBSubresource();
smdNew.setDocumentIdentifier(DBSubresource.getDocumentIdentifier());
smdNew.setDocumentIdentifierScheme(DBSubresource.getDocumentIdentifierScheme());
smdNew.setServiceGroupDomain(optNewDomain.get());
smdNew.setServiceMetadataXml(DBSubresource.getServiceMetadataXml());
smdNew.setCreatedOn(DBSubresource.getCreatedOn());
optNewDomain.get().addServiceMetadata(smdNew);
} else {
throw new SMPRuntimeException(SG_NOT_REGISTRED_FOR_DOMAIN, serviceMetadataRO.getDomainCode(),
serviceGroupRO.getParticipantIdentifier(), serviceGroupRO.getParticipantScheme());
}
}
} else {
throw new SMPRuntimeException(SG_NOT_REGISTRED_FOR_DOMAIN, serviceMetadataRO.getDomainCode(),
serviceGroupRO.getParticipantIdentifier(), serviceGroupRO.getParticipantScheme());
}
}
});
//
// add extension
if (serviceGroupRO.getExtensionStatus() != EntityROStatus.PERSISTED.getStatusNumber()) {
byte[] buff = validateExtension(serviceGroupRO);
dbServiceGroup.setExtension(buff);
}
*/
// persist it to database
getDatabaseDao().update(dbServiceGroup);
return participantSMLRecordList;
}
/**
* Validate (if domains are added only once) and update domain list for service group.
*
* @param serviceGroupRO
* @param dbServiceGroup
*/
protected List<ParticipantSMLRecord> updateDomainsForServiceGroup(ServiceGroupRO serviceGroupRO, DBResource dbServiceGroup) {
List<ParticipantSMLRecord> participantSMLRecordList = new ArrayList<>();
/*
// / validate (if domains are added only once) and create domain list for service group.
List<ServiceGroupDomainRO> serviceGroupDomainROList = serviceGroupRO.getServiceGroupDomains();
// copy array list of old domains and then put them back. Domain not added back will be deleted by hibernate
// ...
List<DBDomainResourceDef> lstOldSGDomains = new ArrayList<>();
lstOldSGDomains.addAll(dbServiceGroup.getResourceDomains());
dbServiceGroup.getResourceDomains().clear();
serviceGroupDomainROList.forEach(serviceGroupDomainRO -> {
DBDomainResourceDef dsg = getSGDomainFromList(lstOldSGDomains, serviceGroupDomainRO);
if (dsg != null) {
// put it back - no need to call addDomain
dbServiceGroup.getResourceDomains().add(dsg);
// remove from old domain list
lstOldSGDomains.remove(dsg);
} else {
// new domain - find dbDomain and add it to service group
Optional<DBDomain> dmn = domainDao.getDomainByCode(serviceGroupDomainRO.getDomainCode());
if (dmn.isPresent()) {
DBDomainResourceDef sgd = dbServiceGroup.addDomain(dmn.get());
participantSMLRecordList.add(new ParticipantSMLRecord(SMLStatusEnum.REGISTER,
sgd.getServiceGroup().getIdentifierValue(),
sgd.getServiceGroup().getIdentifierScheme(),
sgd.getDomain()));
} else {
throw new SMPRuntimeException(DOMAIN_NOT_EXISTS, serviceGroupDomainRO.getDomainCode());
}
}
});
// remove old domains
lstOldSGDomains.forEach(dbServiceGroupDomain -> {
participantSMLRecordList.add(new ParticipantSMLRecord(SMLStatusEnum.UNREGISTER,
dbServiceGroupDomain.getServiceGroup().getIdentifierValue(),
dbServiceGroupDomain.getServiceGroup().getIdentifierScheme(),
dbServiceGroupDomain.getDomain()));
dbServiceGroupDomain.setServiceGroup(null);
}); */
return participantSMLRecordList;
}
/**
* Update users on service group. Method is OK for update and add new domain
*
* @param serviceGroupRO
* @param dbServiceGroup
*/
protected void updateUsersOnServiceGroup(ServiceGroupRO serviceGroupRO, DBResource dbServiceGroup) {
// update users
/* TODO!
dbServiceGroup.getMembers().clear();
List<UserRO> lstUsers = serviceGroupRO.getUsers();
for (UserRO userRO : lstUsers) {
Long userid = SessionSecurityUtils.decryptEntityId(userRO.getUserId());
Optional<DBUser> optUser = userDao.findUser(userid);
if (!optUser.isPresent()) {
throw new SMPRuntimeException(INTERNAL_ERROR,
"Database changed", "User " + userRO.getUsername() + " not exists! (Refresh data)");
}
dbServiceGroup.getMembers().add(optUser.get());
}*/
}
/**
* Method retrieve servicegroup data from database and validates id and participant
*
* @param serviceGroupRO
* @return
*/
private DBResource findAndValidateServiceGroup(ServiceGroupRO serviceGroupRO) {
// find and validate service group
if (serviceGroupRO.getId() == null) {
throw new SMPRuntimeException(MISSING_SG_ID, serviceGroupRO.getParticipantIdentifier(), serviceGroupRO.getParticipantScheme());
}
// validate service group id
boolean schemeMandatory = configurationService.getParticipantSchemeMandatory();
LOG.debug("Validate service group [{}] with [{}] scheme", serviceGroupRO.getParticipantIdentifier(), (schemeMandatory ? "mandatory" : "optional"));
DBResource dbServiceGroup = getDatabaseDao().find(serviceGroupRO.getId());
if (!Objects.equals(serviceGroupRO.getParticipantIdentifier(), dbServiceGroup.getIdentifierValue())
|| schemeMandatory &&
!Objects.equals(serviceGroupRO.getParticipantScheme(), dbServiceGroup.getIdentifierScheme())) {
throw new SMPRuntimeException(INVALID_SG_ID, serviceGroupRO.getParticipantIdentifier(),
serviceGroupRO.getParticipantScheme(), serviceGroupRO.getId());
}
return dbServiceGroup;
}
/**
* Check if service metadata parsers and if data match servicemetadata and service group...
*
* @param serviceMetadataRO
* @return
*/
private byte[] validateServiceMetadata(ServiceMetadataRO serviceMetadataRO) {
byte[] buff = null;
/*
try {
buff = serviceMetadataRO.getXmlContent().getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
throw new SMPRuntimeException(INVALID_ENCODING, "UTF-8");
}
try {
BdxSmpOasisValidator.validateXSD(buff);
} catch (XmlInvalidAgainstSchemaException e) {
throw new SMPRuntimeException(INVALID_SMD_XML, ExceptionUtils.getRootCauseMessage(e));
}
*/
/*
ServiceMetadata smd = ServiceMetadataConverter.unmarshal(buff);
if (smd.getServiceInformation() != null) {
Identifier di = identifierService.normalizeDocument(smd.getServiceInformation().getDocumentIdentifier());
if (Objects.equals(di.getScheme(), serviceMetadataRO.getDocumentIdentifierScheme())
&& Objects.equals(di.getValue(), serviceMetadataRO.getDocumentIdentifier())) {
} else {
throw new SMPRuntimeException(INVALID_SMD_DOCUMENT_DATA, di.getValue(), di.getScheme(),
serviceMetadataRO.getDocumentIdentifier(), serviceMetadataRO.getDocumentIdentifierScheme());
}
}
*/
return buff;
}
/**
* Convert Database object to Rest object for UI. It does not set blobs - extensions and metadataservice xml!
* They are retrieved to UI when needed.
*
* @param dbServiceGroup - database entity
* @return ServiceGroupRO
*/
public ServiceGroupRO convertToRo(DBResource dbServiceGroup) {
ServiceGroupRO serviceGroupRo = new ServiceGroupRO();
serviceGroupRo.setId(dbServiceGroup.getId());
serviceGroupRo.setParticipantIdentifier(dbServiceGroup.getIdentifierValue());
serviceGroupRo.setParticipantScheme(dbServiceGroup.getIdentifierScheme());
dbServiceGroup.getSubresources().stream().map(this::convertServiceMetadataToRo)
.forEach(smdro -> {
serviceGroupRo.getServiceMetadata().add(smdro);
});
/*
// add domains
dbServiceGroup.getResourceDomains().forEach(sgd -> {
ServiceGroupDomainRO servGrpDomain = new ServiceGroupDomainRO();
servGrpDomain.setId(sgd.getId());
servGrpDomain.setDomainId(sgd.getDomain().getId());
servGrpDomain.setDomainCode(sgd.getDomain().getDomainCode());
servGrpDomain.setSmlSubdomain(sgd.getDomain().getSmlSubdomain());
// add service metadata to service group NOT TO service group domain
// little different view from DB Model - all for the users :) ..
sgd.getSubresourcesList().stream().map(this::convertServiceMetadataToRo)
.forEach(smdro -> {
smdro.setSmlSubdomain(servGrpDomain.getSmlSubdomain());
smdro.setDomainCode(servGrpDomain.getDomainCode());
smdro.setServiceGroupDomainId(servGrpDomain.getId());
serviceGroupRo.getServiceMetadata().add(smdro);
});
//also add domain to service group
serviceGroupRo.getServiceGroupDomains().add(servGrpDomain);
});
*/
/*TODO
// add users add just encrypted ID
dbServiceGroup.getUsers().forEach(usr -> {
UserRO userRO = new UserRO();
userRO.setUserId(SessionSecurityUtils.encryptedEntityId(usr.getId()));
serviceGroupRo.getUsers().add(userRO);
});
*/
// do not add service extension to gain performance.
return serviceGroupRo;
}
/**
* Convert database entity to resource object ServiceMetadataRO. To gain UI performance do not copy XM for UI.
* It is retrieved when needed!
*
* @param sgmd
* @return
*/
private ServiceMetadataRO convertServiceMetadataToRo(DBSubresource sgmd) {
ServiceMetadataRO smdro = new ServiceMetadataRO();
smdro.setId(sgmd.getId());
smdro.setDocumentIdentifier(sgmd.getIdentifierValue());
smdro.setDocumentIdentifierScheme(sgmd.getIdentifierScheme());
return smdro;
}
/**
* Create new database entity - service metadata from resource object
*
* @param serviceMetadataRO
* @return new database entity DBSubresource
*/
private DBSubresource createServiceMetadataFromRo(ServiceMetadataRO serviceMetadataRO) {
byte[] buff = validateServiceMetadata(serviceMetadataRO);
DBSubresource DBSubresource = new DBSubresource();
Identifier docIdent = identifierService.normalizeDocument(serviceMetadataRO.getDocumentIdentifierScheme(),
serviceMetadataRO.getDocumentIdentifier());
DBSubresource.setIdentifierValue(docIdent.getValue());
return DBSubresource;
}
/**
* for ServiceGroupDomainRO returns DBServiceGroupDomain from ServiceGroup list of domain. ServiceGroup domain is matched by Id
* and verified by domain id.
*
* @param lstSGDomains
* @param domainRo
* @return
*/
private DBDomainResourceDef getSGDomainFromList(List<DBDomainResourceDef> lstSGDomains, ServiceGroupDomainRO domainRo) {
for (DBDomainResourceDef dbServiceGroupDomain : lstSGDomains) {
if (Objects.equals(dbServiceGroupDomain.getId(), domainRo.getId())) {
// double check for domain
if (!Objects.equals(dbServiceGroupDomain.getDomain().getId(), domainRo.getDomainId())) {
throw new SMPRuntimeException(INVALID_REQUEST, "Domain mismatch!", "Domain id for does not match for servicegroup domain");
}
return dbServiceGroupDomain;
}
}
return null;
}
/**
* Validate if extension is valid by schema.
*
* @param serviceGroup
* @return
*/
public ServiceGroupValidationRO validateServiceGroup(ServiceGroupValidationRO serviceGroup) {
/**
if (serviceGroup == null) {
throw new SMPRuntimeException(INVALID_REQUEST, "Validate extension", "Missing Extension parameter");
} // if new check if service group already exist
if (serviceGroup.getStatusAction() == EntityROStatus.NEW.getStatusNumber()) {
Identifier normalizedParticipant = identifierService
.normalizeParticipant(
serviceGroup.getParticipantScheme(),
serviceGroup.getParticipantIdentifier());
Optional<DBResource> sg = serviceGroupDao.findServiceGroup(normalizedParticipant.getValue(),
normalizedParticipant.getScheme());
if (sg.isPresent()) {
serviceGroup.setErrorMessage("Service group: " + serviceGroup.getParticipantScheme() + ":" + serviceGroup.getParticipantIdentifier() +
" already exists!");
serviceGroup.setErrorCode(ERROR_CODE_SERVICE_GROUP_EXISTS);
return serviceGroup;
}
}
if (StringUtils.isBlank(serviceGroup.getExtension())) {
// empty extension is also a valid extension
serviceGroup.setErrorMessage(null);
} else {
try {
byte[] buff = serviceGroup.getExtension().getBytes("UTF-8");
ExtensionConverter.validateExtensionBySchema(buff); // validate by schema
serviceGroup.setErrorMessage(null);
serviceGroup.setErrorCode(ERROR_CODE_OK);
} catch (XmlInvalidAgainstSchemaException | UnsupportedEncodingException e) {
serviceGroup.setErrorMessage(ExceptionUtils.getRootCauseMessage(e));
serviceGroup.setErrorCode(ERROR_CODE_INVALID_EXTENSION);
}
}
*/
return serviceGroup;
}
/**
* Validate if extension is valid by schema.
*
* @param serviceGroupRO
* @return
*/
public byte[] validateExtension(ServiceGroupRO serviceGroupRO) {
/*
if (StringUtils.isBlank(serviceGroupRO.getExtension())) {
return null;
}
try {
byte[] buff = serviceGroupRO.getExtension().getBytes("UTF-8");
ExtensionConverter.validateExtensionBySchema(buff); // validate by schema
return buff;
} catch (UnsupportedEncodingException | XmlInvalidAgainstSchemaException e) {
throw new SMPRuntimeException(INVALID_EXTENSION_FOR_SG, serviceGroupRO.getParticipantIdentifier(),
serviceGroupRO.getParticipantScheme(), ExceptionUtils.getRootCauseMessage(e));
}
*/
return null;
}
}
package eu.europa.ec.edelivery.smp.services.ui;
import eu.europa.ec.edelivery.smp.conversion.IdentifierService;
import eu.europa.ec.edelivery.smp.data.dao.BaseDao;
import eu.europa.ec.edelivery.smp.data.dao.DomainDao;
import eu.europa.ec.edelivery.smp.data.dao.SubresourceDao;
import eu.europa.ec.edelivery.smp.data.dao.UserDao;
import eu.europa.ec.edelivery.smp.data.model.doc.DBSubresource;
import eu.europa.ec.edelivery.smp.data.ui.ServiceMetadataRO;
import eu.europa.ec.edelivery.smp.data.ui.ServiceMetadataValidationRO;
import eu.europa.ec.edelivery.smp.logging.SMPLogger;
import eu.europa.ec.edelivery.smp.logging.SMPLoggerFactory;
import eu.europa.ec.edelivery.smp.services.ConfigurationService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.UnsupportedEncodingException;
import java.nio.charset.IllegalCharsetNameException;
import java.security.cert.CertificateException;
/**
* Services for managing the Service metadata
*/
@Service
public class UIServiceMetadataService extends UIServiceBase<DBSubresource, ServiceMetadataRO> {
private static final SMPLogger LOG = SMPLoggerFactory.getLogger(UIServiceMetadataService.class);
protected final DomainDao domainDao;
protected final SubresourceDao serviceMetadataDao;
protected final UserDao userDao;
protected final IdentifierService caseSensitivityNormalizer;
protected final ConfigurationService configurationService;
public UIServiceMetadataService(DomainDao domainDao,
SubresourceDao serviceMetadataDao,
UserDao userDao,
IdentifierService caseSensitivityNormalizer,
ConfigurationService configurationService) {
this.domainDao = domainDao;
this.serviceMetadataDao = serviceMetadataDao;
this.userDao = userDao;
this.caseSensitivityNormalizer = caseSensitivityNormalizer;
this.configurationService = configurationService;
}
@Override
protected BaseDao<DBSubresource> getDatabaseDao() {
return serviceMetadataDao;
}
@Transactional
public ServiceMetadataRO getServiceMetadataXMLById(Long serviceMetadataId) {
LOG.debug("Get service metadata: {}", serviceMetadataId);
DBSubresource dbSubresource = serviceMetadataDao.find(serviceMetadataId);
ServiceMetadataRO serviceMetadataRO = new ServiceMetadataRO();
serviceMetadataRO.setId(dbSubresource.getId());
serviceMetadataRO.setDocumentIdentifier(dbSubresource.getIdentifierValue());
serviceMetadataRO.setDocumentIdentifierScheme(dbSubresource.getIdentifierScheme());
return serviceMetadataRO;
}
private String getConvertServiceMetadataToString(Long id, byte[] extension) {
try {
return new String(extension, "UTF-8");
} catch (UnsupportedEncodingException e) {
LOG.error("Can not convert ServiceMetadata bytearray to 'UTF-8'", e);
throw new IllegalCharsetNameException("UTF-8");
}
}
/**
* Check if service metadata parsers and if data match servicemetadata and service group...
*
* @param serviceMetadataRO
* @return
*/
public ServiceMetadataValidationRO validateServiceMetadata(ServiceMetadataValidationRO serviceMetadataRO) {
/* byte[] buff;
if (serviceMetadataRO == null) {
throw new SMPRuntimeException(INVALID_REQUEST, "Validate service metadata", "Missing servicemetadata parameter");
} else if (StringUtils.isBlank(serviceMetadataRO.getXmlContent())) {
serviceMetadataRO.setErrorMessage("Service metadata xml must not be empty");
} else {
// validate xml - first byte-array is expected to be in utf8 format
// convert to utf-8 byte array
try {
buff = serviceMetadataRO.getXmlContent().getBytes("UTF-8");
serviceMetadataRO.setXmlContent(""); // no need to return back schema
} catch (UnsupportedEncodingException e) {
serviceMetadataRO.setErrorMessage(ExceptionUtils.getRootCauseMessage(e));
serviceMetadataRO.setXmlContent(""); // no need to return back schema
return serviceMetadataRO;
}
Identifier headerDI = caseSensitivityNormalizer.normalizeDocument(
serviceMetadataRO.getDocumentIdentifierScheme(),
serviceMetadataRO.getDocumentIdentifier());
Identifier headerPI = caseSensitivityNormalizer.normalizeParticipant(
serviceMetadataRO.getParticipantScheme(),
serviceMetadataRO.getParticipantIdentifier());
// validate by schema
try {
BdxSmpOasisValidator.validateXSD(buff);
} catch (XmlInvalidAgainstSchemaException e) {
serviceMetadataRO.setErrorMessage(ExceptionUtils.getRootCauseMessage(e));
return serviceMetadataRO;
}
*/
/* TODO
// validate data
ServiceMetadata smd = ServiceMetadataConverter.unmarshal(buff);
if (smd.getRedirect() != null) {
if (StringUtils.isBlank(smd.getRedirect().getHref())) {
serviceMetadataRO.setErrorMessage("Redirect URL must must be empty!");
return serviceMetadataRO;
}
}
if (smd.getServiceInformation() != null) {
Identifier xmlDI = caseSensitivityNormalizer.normalizeDocument(smd.getServiceInformation().getDocumentIdentifier());
ParticipantIdentifierType xmlPI = caseSensitivityNormalizer.normalizeParticipant(smd.getServiceInformation().getParticipantIdentifier());
if (!xmlDI.equals(headerDI)) {
serviceMetadataRO.setErrorMessage("Document identifier and scheme do not match!");
return serviceMetadataRO;
}
if (!xmlPI.equals(headerPI)) {
serviceMetadataRO.setErrorMessage("Participant identifier and scheme do not match!");
return serviceMetadataRO;
}
}
if (serviceMetadataRO.getStatusAction() == EntityROStatus.NEW.getStatusNumber()) {
// check if service metadata already exists
Optional<DBSubresource> exists = serviceMetadataDao.findServiceMetadata(headerPI.getValue(), headerPI.getScheme(),
headerDI.getValue(), headerDI.getScheme());
if (exists.isPresent()) {
serviceMetadataRO.setErrorMessage("Document identifier and scheme already exist in database!");
return serviceMetadataRO;
}
}
try {
validateServiceMetadataCertificates(smd);
} catch (CertificateException e) {
serviceMetadataRO.setErrorMessage(ExceptionUtils.getRootCauseMessage(e));
return serviceMetadataRO;
}
}
*/
//return serviceMetadataRO;
return null;
}
/**
* Method validates certificates in all endpoints.
*
* @param smd ServiceMetadata document
* @throws CertificateException exception if certificate is not valid or the allowed key type
public void validateServiceMetadataCertificates(ServiceMetadata smd) throws CertificateException {
List<EndpointType> endpointTypeList = searchAllEndpoints(smd);
for (EndpointType endpointType : endpointTypeList) {
validateCertificate(endpointType.getCertificate());
}
}
*/
/**
* Method returns all EndpointTypes
*
* @param smd
* @return public List<EndpointType> searchAllEndpoints(ServiceMetadata smd) {
List<ProcessType> processTypeList = smd.getServiceInformation() != null ?
smd.getServiceInformation().getProcessList().getProcesses() : Collections.emptyList();
List<EndpointType> endpointTypeList = new ArrayList<>();
processTypeList.stream().forEach(processType -> endpointTypeList.addAll(processType.getServiceEndpointList() != null ?
processType.getServiceEndpointList().getEndpoints() : Collections.emptyList()));
return endpointTypeList;
}
*/
/**
* Validate the certificate
*
* @param crtData x509 encoded byte array
* @throws CertificateException
public void validateCertificate(byte[] crtData) throws CertificateException {
if (crtData == null || crtData.length == 0) {
LOG.debug("Skip certificate validation: Empty certificate.");
return;
}
X509Certificate cert = X509CertificateUtils.getX509Certificate(crtData);
// validate is certificate is valid
cert.checkValidity();
// validate if certificate has the right key algorithm
PublicKey key = cert.getPublicKey();
List<String> allowedKeyAlgs = configurationService.getAllowedDocumentCertificateTypes();
if (allowedKeyAlgs == null || allowedKeyAlgs.isEmpty()) {
LOG.debug("Ignore the service metadata certificate key type validation (Empty property: [{}]).", DOCUMENT_RESTRICTION_CERT_TYPES.getProperty());
return;
}
if (StringUtils.equalsAnyIgnoreCase(key.getAlgorithm(), allowedKeyAlgs.toArray(new String[]{}))) {
LOG.debug("Certificate has valid key algorithm [{}]. Allowed algorithms: [{}] .", key.getAlgorithm(), allowedKeyAlgs);
return;
}
LOG.debug("Certificate has invalid key algorithm [{}]. Allowed algorithms: [{}] .", key.getAlgorithm(), allowedKeyAlgs);
throw new CertificateException("Certificate does not have allowed key type!");
} */
}
package eu.europa.ec.edelivery.smp.services.ui;
import eu.europa.ec.edelivery.smp.config.ConversionTestConfig;
import eu.europa.ec.edelivery.smp.data.ui.ServiceGroupSearchRO;
import eu.europa.ec.edelivery.smp.data.ui.ServiceResult;
import eu.europa.ec.edelivery.smp.services.AbstractServiceIntegrationTest;
import eu.europa.ec.edelivery.smp.services.ui.filters.ResourceFilter;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@ContextConfiguration(classes = {UIResourceSearchService.class, ConversionTestConfig.class})
public class UIResourceSearchServiceTest extends AbstractServiceIntegrationTest {
@Autowired
protected UIResourceSearchService testInstance;
@Before
public void prepareDatabase() {
// setup initial data!
testUtilsDao.clearData();
testUtilsDao.createSubresources();
}
@Test
public void testGetTableList() {
ResourceFilter filter = new ResourceFilter();
ServiceResult<ServiceGroupSearchRO> result = testInstance.getTableList(-1, -1, null, null, filter);
assertNotNull(result);
assertEquals(2, result.getCount().intValue());
}
@Test
public void testGetTableListWithFilter() {
ResourceFilter filter = new ResourceFilter();
filter.setIdentifierValueLike(testUtilsDao.getResourceD1G1RD1().getIdentifierValue());
ServiceResult<ServiceGroupSearchRO> result = testInstance.getTableList(-1, -1, null, null, filter);
assertNotNull(result);
assertEquals(1, result.getCount().intValue());
}
}
package eu.europa.ec.edelivery.smp.services.ui;
import eu.europa.ec.edelivery.smp.data.model.doc.DBResource;
import eu.europa.ec.edelivery.smp.data.model.user.DBUser;
import eu.europa.ec.edelivery.smp.data.ui.ServiceGroupSearchRO;
import eu.europa.ec.edelivery.smp.data.ui.ServiceResult;
import eu.europa.ec.edelivery.smp.services.AbstractServiceIntegrationTest;
import eu.europa.ec.edelivery.smp.testutil.TestConstants;
import eu.europa.ec.edelivery.smp.testutil.TestDBUtils;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
@ContextConfiguration(classes = {UIServiceGroupSearchService.class, UIServiceMetadataService.class})
@Ignore
public class UIServiceGroupSearchServiceTest extends AbstractServiceIntegrationTest {
@Autowired
protected UIServiceGroupSearchService testInstance;
@Autowired
protected UIServiceMetadataService uiServiceMetadataService;
protected void insertDataObjectsForOwner(int size, DBUser owner) {
for (int i = 0; i < size; i++) {
insertServiceGroup(String.format("%4d", i), true, owner);
}
}
protected void insertDataObjects(int size) {
insertDataObjectsForOwner(size, null);
}
protected DBResource insertServiceGroup(String id, boolean withExtension, DBUser owner) {
DBResource d = TestDBUtils.createDBResource(String.format("0007:%s:utest", id), TestConstants.TEST_SG_SCHEMA_1, withExtension);
if (owner != null) {
// d.getUsers().add(owner);
}
serviceGroupDao.persistFlushDetach(d);
return d;
}
@Test
public void testGetTableListEmpty() {
// given
//when
ServiceResult<ServiceGroupSearchRO> res = testInstance.getTableList(-1, -1, null, null, null);
// then
assertNotNull(res);
assertEquals(0, res.getCount().intValue());
assertEquals(0, res.getPage().intValue());
assertEquals(-1, res.getPageSize().intValue());
assertEquals(0, res.getServiceEntities().size());
assertNull(res.getFilter());
}
@Test
public void testGetTableList15() {
// given
insertDataObjects(15);
//when
ServiceResult<ServiceGroupSearchRO> res = testInstance.getTableList(-1, -1, null, null, null);
// then
assertNotNull(res);
assertEquals(15, res.getCount().intValue());
assertEquals(0, res.getPage().intValue());
assertEquals(-1, res.getPageSize().intValue());
assertEquals(15, res.getServiceEntities().size());
assertNull(res.getFilter());
// all table properties should not be null
assertNotNull(res);
assertNotNull(res.getServiceEntities().get(0).getParticipantIdentifier());
assertNotNull(res.getServiceEntities().get(0).getParticipantScheme());
}
@Test
public void convertToRo() {
// given
DBResource sg = TestDBUtils.createDBResource();
// then when
ServiceGroupSearchRO sgr = testInstance.convertToRo(sg);
// then
assertEquals(sg.getId(), sgr.getId());
assertEquals(sg.getIdentifierScheme(), sgr.getParticipantScheme());
assertEquals(sg.getIdentifierValue(), sgr.getParticipantIdentifier());
}
}
package eu.europa.ec.edelivery.smp.services.ui;
import eu.europa.ec.edelivery.smp.data.model.doc.DBResource;
import eu.europa.ec.edelivery.smp.data.model.user.DBResourceMember;
import eu.europa.ec.edelivery.smp.data.model.user.DBUser;
import eu.europa.ec.edelivery.smp.data.ui.ServiceGroupRO;
import eu.europa.ec.edelivery.smp.data.ui.ServiceResult;
import eu.europa.ec.edelivery.smp.services.AbstractServiceIntegrationTest;
import eu.europa.ec.edelivery.smp.testutil.TestConstants;
import eu.europa.ec.edelivery.smp.testutil.TestDBUtils;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import static org.hamcrest.text.MatchesPattern.matchesPattern;
import static org.junit.Assert.*;
/**
* Purpose of class is to test ServiceGroupService base methods
*
* @author Joze Rihtarsic
* @since 4.1
*/
@Ignore
@ContextConfiguration(classes = {UIServiceGroupService.class, UIServiceMetadataService.class})
public class UIServiceGroupServiceIntegrationTest extends AbstractServiceIntegrationTest {
@Rule
public ExpectedException expectedExeption = ExpectedException.none();
@Autowired
protected UIServiceGroupService testInstance;
@Autowired
protected UIServiceMetadataService uiServiceMetadataService;
protected void insertDataObjectsForOwner(int size, DBUser owner) {
for (int i = 0; i < size; i++) {
insertServiceGroup(String.format("%4d", i), true, owner);
}
}
protected void insertDataObjects(int size) {
insertDataObjectsForOwner(size, null);
}
protected DBResource insertServiceGroup(String id, boolean withExtension, DBUser owner) {
DBResource d = TestDBUtils.createDBResource(String.format("0007:%s:utest", id), TestConstants.TEST_SG_SCHEMA_1, withExtension);
if (owner!= null) {
d.getMembers().add(new DBResourceMember(d, owner));
}
serviceGroupDao.persistFlushDetach(d);
return d;
}
@Test
public void testGetTableListEmpty() {
// given
//when
ServiceResult<ServiceGroupRO> res = testInstance.getTableList(-1, -1, null, null, null);
// then
assertNotNull(res);
assertEquals(0, res.getCount().intValue());
assertEquals(0, res.getPage().intValue());
assertEquals(-1, res.getPageSize().intValue());
assertEquals(0, res.getServiceEntities().size());
assertNull(res.getFilter());
}
/*
@Test
public void testGetTableList15() {
// given
insertDataObjects(15);
//when
ServiceResult<ServiceGroupRO> res = testInstance.getTableList(-1, -1, null, null, null);
// then
assertNotNull(res);
assertEquals(15, res.getCount().intValue());
assertEquals(0, res.getPage().intValue());
assertEquals(-1, res.getPageSize().intValue());
assertEquals(15, res.getServiceEntities().size());
assertNull(res.getFilter());
// all table properties should not be null
assertNotNull(res);
assertNotNull(res.getServiceEntities().get(0).getParticipantIdentifier());
assertNotNull(res.getServiceEntities().get(0).getParticipantScheme());
}
@Test
public void testAddServiceWithMetadata() {
// given
DBDomain testDomain01 = TestDBUtils.createDBDomain(TestConstants.TEST_DOMAIN_CODE_1);
domainDao.persistFlushDetach(testDomain01);
ServiceGroupRO sgnew = TestROUtils.createROServiceGroupForDomains(testDomain01);
// add service metadata
ServiceMetadataRO mtro = TestROUtils.createServiceMetadataDomain(testDomain01, sgnew, TestConstants.TEST_DOC_ID_1, TestConstants.TEST_DOC_SCHEMA_1);
sgnew.getServiceMetadata().add(mtro);
//when
testInstance.updateServiceGroupList(Collections.singletonList(sgnew), true);
// then
ServiceResult<ServiceGroupRO> res = testInstance.getTableList(-1, -1, null, null, null);
assertNotNull(res);
assertEquals(1, res.getCount().intValue());
ServiceGroupRO sgAdded = res.getServiceEntities().get(0);
ServiceGroupValidationRO sgExt = testInstance.getServiceGroupExtensionById(sgAdded.getId());
// all table properties should not be null
assertNotNull(sgAdded);
assertEquals(sgnew.getParticipantIdentifier(), sgAdded.getParticipantIdentifier());
assertEquals(sgnew.getParticipantScheme(), sgAdded.getParticipantScheme());
assertNull(sgAdded.getExtension()); // with list extension must be empty - extension is retrived by some other call
assertEquals(sgnew.getExtension(), sgExt.getExtension());
assertEquals(1, sgAdded.getServiceGroupDomains().size());
assertEquals(1, sgAdded.getServiceMetadata().size());
}
@Test
public void testUpdateServiceGroupExtensionAndServiceMetadaXML() {
// given
DBDomain testDomain01 = TestDBUtils.createDBDomain(TestConstants.TEST_DOMAIN_CODE_1);
domainDao.persistFlushDetach(testDomain01);
DBResource dbServiceGroup = TestDBUtils.createDBServiceGroup();
dbServiceGroup.addDomain(testDomain01);
DBSubresource DBSubresource = TestDBUtils.createDBSubresource(dbServiceGroup.getIdentifierValue(), dbServiceGroup.getIdentifierScheme());
dbServiceGroup.getResourceDomains().get(0).addServiceMetadata(DBSubresource);
serviceGroupDao.persistFlushDetach(dbServiceGroup);
String newMetadataXML = TestROUtils.generateServiceMetadata(dbServiceGroup.getIdentifierValue(), dbServiceGroup.getIdentifierScheme(),
DBSubresource.getDocumentIdentifier(), DBSubresource.getDocumentIdentifierScheme());
String newExtension = TestROUtils.generateExtension();
ServiceResult<ServiceGroupRO> res = testInstance.getTableList(-1, -1, null, null, null);
assertEquals(1, res.getCount().intValue());
ServiceGroupRO sgChange = res.getServiceEntities().get(0);
ServiceMetadataRO smdXML = uiServiceMetadataService.getServiceMetadataXMLById(res.getServiceEntities().get(0).getServiceMetadata().get(0).getId());
// test new extension
assertNotEquals(newExtension, sgChange.getExtension());
assertNotEquals(newMetadataXML, smdXML.getXmlContent());
// set new extension
sgChange.setStatus(EntityROStatus.UPDATED.getStatusNumber());
sgChange.setExtension(newExtension);
sgChange.setExtensionStatus(EntityROStatus.UPDATED.getStatusNumber());
// set new XMLContent
sgChange.getServiceMetadata().get(0).setStatus(EntityROStatus.UPDATED.getStatusNumber());
sgChange.getServiceMetadata().get(0).setXmlContentStatus(EntityROStatus.UPDATED.getStatusNumber());
sgChange.getServiceMetadata().get(0).setXmlContent(newMetadataXML);
//when
testInstance.updateServiceGroupList(Collections.singletonList(sgChange), true);
// then
res = testInstance.getTableList(-1, -1, null, null, null);
assertNotNull(res);
assertEquals(1, res.getCount().intValue());
ServiceGroupRO sgUpdated = res.getServiceEntities().get(0);
ServiceGroupValidationRO sgExt = testInstance.getServiceGroupExtensionById(sgUpdated.getId());
assertEquals(1, sgChange.getServiceMetadata().size());
// retrive service metadata xml with special service - it is not retrieve by browsing list
ServiceMetadataRO smdXMLNew = uiServiceMetadataService.getServiceMetadataXMLById(sgUpdated.getServiceMetadata().get(0).getId());
// all table properties should not be null
assertNotNull(sgUpdated);
assertEquals(sgUpdated.getParticipantIdentifier(), sgUpdated.getParticipantIdentifier());
assertEquals(sgUpdated.getParticipantScheme(), sgUpdated.getParticipantScheme());
assertEquals(newExtension, sgExt.getExtension());
assertEquals(1, sgChange.getServiceGroupDomains().size());
assertNotNull(smdXMLNew.getXmlContent());
assertEquals(newMetadataXML, smdXMLNew.getXmlContent());
}
@Test
public void testUpdateServiceMatadataChangeDomain() {
// given
DBDomain testDomain01 = TestDBUtils.createDBDomain(TestConstants.TEST_DOMAIN_CODE_1);
domainDao.persistFlushDetach(testDomain01);
DBDomain testDomain02 = TestDBUtils.createDBDomain(TestConstants.TEST_DOMAIN_CODE_2);
domainDao.persistFlushDetach(testDomain02);
DBResource dbServiceGroup = TestDBUtils.createDBServiceGroup();
dbServiceGroup.addDomain(testDomain01);
DBSubresource DBSubresource = TestDBUtils.createDBSubresource(dbServiceGroup.getIdentifierValue(), dbServiceGroup.getIdentifierScheme());
dbServiceGroup.getResourceDomains().get(0).addServiceMetadata(DBSubresource);
// add second domain
dbServiceGroup.addDomain(testDomain02);
serviceGroupDao.persistFlushDetach(dbServiceGroup);
ServiceResult<ServiceGroupRO> res = testInstance.getTableList(-1, -1, null, null, null);
assertNotNull(res);
assertEquals(1, res.getCount().intValue());
ServiceGroupRO sgChanged = res.getServiceEntities().get(0);
ServiceMetadataRO smdToChange = sgChanged.getServiceMetadata().get(0);
assertEquals(testDomain01.getDomainCode(), smdToChange.getDomainCode());
assertEquals(testDomain01.getSmlSubdomain(), smdToChange.getSmlSubdomain());
// then
sgChanged.setStatus(EntityROStatus.UPDATED.getStatusNumber());
smdToChange.setStatus(EntityROStatus.UPDATED.getStatusNumber());
smdToChange.setDomainCode(testDomain02.getDomainCode());
smdToChange.setSmlSubdomain(testDomain02.getSmlSubdomain());
testInstance.updateServiceGroupList(Collections.singletonList(sgChanged), true);
res = testInstance.getTableList(-1, -1, null, null, null);
ServiceGroupRO sgUpdated = res.getServiceEntities().get(0);
ServiceMetadataRO smdUpdated = sgUpdated.getServiceMetadata().get(0);
assertEquals(testDomain02.getDomainCode(), smdUpdated.getDomainCode());
assertEquals(testDomain02.getSmlSubdomain(), smdUpdated.getSmlSubdomain());
}
@Test
public void testUpdateServiceMatadataChangeDomainReverseOrder() {
// given
DBDomain testDomain01 = TestDBUtils.createDBDomain(TestConstants.TEST_DOMAIN_CODE_1);
DBDomain testDomain02 = TestDBUtils.createDBDomain(TestConstants.TEST_DOMAIN_CODE_2);
domainDao.persistFlushDetach(testDomain02);
domainDao.persistFlushDetach(testDomain01);
DBResource dbServiceGroup = TestDBUtils.createDBServiceGroup();
dbServiceGroup.addDomain(testDomain02);
dbServiceGroup.addDomain(testDomain01);
DBSubresource DBSubresource = TestDBUtils.createDBSubresource(dbServiceGroup.getIdentifierValue(), dbServiceGroup.getIdentifierScheme());
dbServiceGroup.getResourceDomains().get(1 ).addServiceMetadata(DBSubresource);
// add second domain
serviceGroupDao.persistFlushDetach(dbServiceGroup);
ServiceResult<ServiceGroupRO> res = testInstance.getTableList(-1, -1, null, null, null);
assertNotNull(res);
assertEquals(1, res.getCount().intValue());
ServiceGroupRO sgChanged = res.getServiceEntities().get(0);
ServiceMetadataRO smdToChange = sgChanged.getServiceMetadata().get(0);
assertEquals(testDomain01.getDomainCode(), smdToChange.getDomainCode());
assertEquals(testDomain01.getSmlSubdomain(), smdToChange.getSmlSubdomain());
// then
sgChanged.setStatus(EntityROStatus.UPDATED.getStatusNumber());
smdToChange.setStatus(EntityROStatus.UPDATED.getStatusNumber());
smdToChange.setDomainCode(testDomain02.getDomainCode());
smdToChange.setSmlSubdomain(testDomain02.getSmlSubdomain());
testInstance.updateServiceGroupList(Collections.singletonList(sgChanged), true);
res = testInstance.getTableList(-1, -1, null, null, null);
ServiceGroupRO sgUpdated = res.getServiceEntities().get(0);
ServiceMetadataRO smdUpdated = sgUpdated.getServiceMetadata().get(0);
assertEquals(testDomain02.getDomainCode(), smdUpdated.getDomainCode());
assertEquals(testDomain02.getSmlSubdomain(), smdUpdated.getSmlSubdomain());
}
@Test
public void validateExtensionValid() throws IOException {
// given
ServiceGroupValidationRO sg = TestROUtils.getValidExtension();
// when
testInstance.validateServiceGroup(sg);
// then
assertNull(sg.getErrorMessage());
assertNotNull(sg.getExtension());
}
@Test
public void validateExtensionMultipleValid() throws IOException {
// given
ServiceGroupValidationRO sg = TestROUtils.getValidMultipleExtension();
// when
testInstance.validateServiceGroup(sg);
// then
assertNull(sg.getErrorMessage());
assertNotNull(sg.getExtension());
}
@Test
public void validateExtensionCustomTextInvalid() throws IOException {
// given
ServiceGroupValidationRO sg = TestROUtils.getValidCustomText();
// when
testInstance.validateServiceGroup(sg);
// then
assertNotNull(sg.getErrorMessage());
assertThat(sg.getErrorMessage(), containsString("Element 'ServiceGroup' cannot have character "));
assertNotNull(sg.getExtension());
}
@Test
public void validateExtensionInvalid() throws IOException {
ServiceGroupValidationRO sg = TestROUtils.getInvalid();
// when
testInstance.validateServiceGroup(sg);
// then
assertNotNull(sg.getErrorMessage());
assertThat(sg.getErrorMessage(), matchesPattern(".*cvc-complex-type.2.4.a: Invalid content was found starting with element \\'\\{?(\"http://docs.oasis-open.org/bdxr/ns/SMP/2016/05\")?:?ExtensionID\\}?\\'.*"));
assertNotNull(sg.getExtension());
}
@Test
public void validateCustomExtension() throws IOException {
ServiceGroupValidationRO sg = TestROUtils.getCustomExtension();
// when
testInstance.validateServiceGroup(sg);
// then
assertNull(sg.getErrorMessage());
assertNotNull(sg.getExtension());
}
@Test
public void getEmptyExtensionById() throws IOException {
DBResource sg = insertServiceGroup("testExt", false, null);
assertNotNull(sg);
assertNotNull(sg.getId());
assertNull(sg.getExtension());
// when
ServiceGroupValidationRO res = testInstance.getServiceGroupExtensionById(sg.getId());
// then
assertNotNull(res);
assertNull(res.getExtension());
}
@Test
public void getExtensionById() throws IOException {
DBResource sg = insertServiceGroup("testExt", true, null);
assertNotNull(sg);
assertNotNull(sg.getId());
assertNotNull(sg.getExtension());
// when
ServiceGroupValidationRO res = testInstance.getServiceGroupExtensionById(sg.getId());
// then
assertNotNull(res);
assertNotNull(res.getExtension());
}
*/
}
package eu.europa.ec.edelivery.smp.services.ui;
import eu.europa.ec.edelivery.smp.config.SmlIntegrationConfiguration;
import eu.europa.ec.edelivery.smp.data.model.DBDomain;
import eu.europa.ec.edelivery.smp.data.model.doc.DBResource;
import eu.europa.ec.edelivery.smp.data.model.user.DBUser;
import eu.europa.ec.edelivery.smp.data.ui.ParticipantSMLRecord;
import eu.europa.ec.edelivery.smp.data.ui.ServiceGroupRO;
import eu.europa.ec.edelivery.smp.data.ui.ServiceResult;
import eu.europa.ec.edelivery.smp.data.ui.enums.SMLStatusEnum;
import eu.europa.ec.edelivery.smp.config.enums.SMPPropertyEnum;
import eu.europa.ec.edelivery.smp.services.AbstractServiceIntegrationTest;
import eu.europa.ec.edelivery.smp.testutil.TestConstants;
import eu.europa.ec.edelivery.smp.testutil.TestDBUtils;
import eu.europa.ec.edelivery.smp.testutil.TestROUtils;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.transaction.annotation.Transactional;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
import static eu.europa.ec.edelivery.smp.testutil.TestConstants.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
/**
* Purpose of class is to test ServiceGroupService base methods
*
* @author Joze Rihtarsic
* @since 4.1
*/
@Ignore
@ContextConfiguration(classes = {UIServiceGroupService.class, UIServiceMetadataService.class,
SmlIntegrationConfiguration.class})
public class UIServiceGroupServiceUpdateListIntegrationTest extends AbstractServiceIntegrationTest {
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Autowired
UIServiceGroupService testInstance;
@Autowired
UIServiceMetadataService uiServiceMetadataService;
@Autowired
SmlIntegrationConfiguration integrationMock;
@Before
public void setup() throws IOException {
resetKeystore();
setDatabaseProperty(SMPPropertyEnum.SML_PHYSICAL_ADDRESS, "0.0.0.0");
setDatabaseProperty(SMPPropertyEnum.SML_LOGICAL_ADDRESS, "http://localhost/smp");
setDatabaseProperty(SMPPropertyEnum.SML_URL, "http://localhost/edelivery-sml");
setDatabaseProperty(SMPPropertyEnum.SML_ENABLED, "true");
prepareDatabaseForMultipeDomainEnv();
integrationMock.reset();
}
protected void insertDataObjectsForOwner(int size, DBUser owner) {
for (int i = 0; i < size; i++) {
insertServiceGroup(String.format("%4d", i), true, owner);
}
}
protected void insertDataObjects(int size) {
insertDataObjectsForOwner(size, null);
}
protected DBResource insertServiceGroup(String id, boolean withExtension, DBUser owner) {
DBResource d = TestDBUtils.createDBResource(String.format("0007:%s:utest", id), TestConstants.TEST_SG_SCHEMA_1, withExtension);
if (owner != null) {
// d.getUsers().add(owner);
}
serviceGroupDao.persistFlushDetach(d);
return d;
}
/*
@Test
public void addNewServiceGroupTestSMLRecords() {
// given
DBDomain dbDomain1 = domainDao.getDomainByCode(TEST_DOMAIN_CODE_1).get();
DBDomain dbDomain2 = domainDao.getDomainByCode(TEST_DOMAIN_CODE_2).get();
ServiceGroupRO serviceGroupRO = TestROUtils.createROServiceGroupForDomains(UUID.randomUUID().toString(), TEST_SG_SCHEMA_1,
dbDomain1, dbDomain2);
// When
List<ParticipantSMLRecord> lst = testInstance.addNewServiceGroup(serviceGroupRO);
// then
assertEquals(2, lst.size());
assertEquals(SMLStatusEnum.REGISTER, lst.get(0).getStatus());
assertEquals(SMLStatusEnum.REGISTER, lst.get(1).getStatus());
assertEquals(dbDomain1, lst.get(0).getDomain());
assertEquals(dbDomain2, lst.get(1).getDomain());
assertEquals(lst.get(0).getParticipantIdentifier(), lst.get(1).getParticipantIdentifier());
assertEquals(serviceGroupRO.getParticipantIdentifier(), lst.get(0).getParticipantIdentifier());
assertEquals(serviceGroupRO.getParticipantScheme(), lst.get(0).getParticipantScheme());
}
@Test
@Transactional
public void updateServiceGroupTestSMLRecordsRemoveDomain() {
// given
DBDomain dbDomain1 = domainDao.getDomainByCode(TEST_DOMAIN_CODE_1).get();
DBDomain dbDomain2 = domainDao.getDomainByCode(TEST_DOMAIN_CODE_2).get();
DBResource dbServiceGroup = TestDBUtils.createDBServiceGroupRandom();
dbServiceGroup.addDomain(dbDomain1);
dbServiceGroup.addDomain(dbDomain2);
serviceGroupDao.persistFlushDetach(dbServiceGroup);
ServiceGroupRO roToUpdate = testInstance.getServiceGroupById(dbServiceGroup.getId());
// when
ServiceGroupDomainRO dro = roToUpdate.getServiceGroupDomains().remove(0);
List<ParticipantSMLRecord> lst = testInstance.updateServiceGroup(roToUpdate, true);
// then
assertEquals(1, lst.size());
assertEquals(SMLStatusEnum.UNREGISTER, lst.get(0).getStatus());
assertEquals(dro.getDomainCode(), lst.get(0).getDomain().getDomainCode());
assertEquals(roToUpdate.getParticipantIdentifier(), lst.get(0).getParticipantIdentifier());
assertEquals(roToUpdate.getParticipantScheme(), lst.get(0).getParticipantScheme());
}
@Test
@Transactional
public void updateServiceGroupTestSMLRecordsAddDomain() {
// given
DBDomain dbDomain1 = domainDao.getDomainByCode(TEST_DOMAIN_CODE_1).get();
DBDomain dbDomain2 = domainDao.getDomainByCode(TEST_DOMAIN_CODE_2).get();
DBResource dbServiceGroup = TestDBUtils.createDBServiceGroupRandom();
dbServiceGroup.addDomain(dbDomain1);
serviceGroupDao.persistFlushDetach(dbServiceGroup);
ServiceGroupRO roToUpdate = testInstance.getServiceGroupById(dbServiceGroup.getId());
// when
ServiceGroupDomainRO sgr = new ServiceGroupDomainRO();
sgr.setDomainCode(dbDomain2.getDomainCode());
sgr.setSmlSubdomain(dbDomain2.getSmlSubdomain());
sgr.setDomainId(dbDomain2.getId());
roToUpdate.getServiceGroupDomains().add(sgr);
List<ParticipantSMLRecord> lst = testInstance.updateServiceGroup(roToUpdate, true);
// then
assertEquals(1, lst.size());
assertEquals(SMLStatusEnum.REGISTER, lst.get(0).getStatus());
assertEquals(sgr.getDomainCode(), lst.get(0).getDomain().getDomainCode());
assertEquals(roToUpdate.getParticipantIdentifier(), lst.get(0).getParticipantIdentifier());
}
*/
/*
@Test
@Transactional
public void updateListSMLRecordsAddDomain() {
// given
DBDomain dbDomain1 = domainDao.getDomainByCode(TEST_DOMAIN_CODE_1).get();
DBDomain dbDomain2 = domainDao.getDomainByCode(TEST_DOMAIN_CODE_2).get();
DBResource dbServiceGroup1 = TestDBUtils.createDBServiceGroupRandom();
DBResource dbServiceGroup2 = TestDBUtils.createDBServiceGroupRandom();
dbServiceGroup1.addDomain(dbDomain1);
dbServiceGroup2.addDomain(dbDomain1);
serviceGroupDao.persistFlushDetach(dbServiceGroup1);
serviceGroupDao.persistFlushDetach(dbServiceGroup2);
ServiceGroupRO serviceGroupROAdd = TestROUtils.createROServiceGroupForDomains(UUID.randomUUID().toString(), UUID.randomUUID().toString(),
dbDomain1, dbDomain2);
ServiceGroupRO serviceGroupROUpdate = testInstance.getServiceGroupById(dbServiceGroup1.getId());
ServiceGroupRO serviceGroupRORemove = testInstance.getServiceGroupById(dbServiceGroup2.getId());
serviceGroupROAdd.setStatus(EntityROStatus.NEW.getStatusNumber());
serviceGroupRORemove.setStatus(EntityROStatus.REMOVE.getStatusNumber());
serviceGroupROUpdate.setStatus(EntityROStatus.UPDATED.getStatusNumber());
serviceGroupROUpdate.getServiceGroupDomains().clear();
ServiceGroupDomainRO sgr = new ServiceGroupDomainRO();
sgr.setDomainCode(dbDomain2.getDomainCode());
sgr.setSmlSubdomain(dbDomain2.getSmlSubdomain());
sgr.setDomainId(dbDomain2.getId());
serviceGroupROUpdate.getServiceGroupDomains().add(sgr);
List<ServiceGroupRO> lstRo = Arrays.asList(serviceGroupROAdd, serviceGroupRORemove, serviceGroupROUpdate);
List<ParticipantSMLRecord> lst = testInstance.updateServiceGroupList(lstRo);
// then
assertEquals(5, lst.size());
assertEquals(SMLAction.REGISTER, lst.get(0).getStatus());
assertEquals(serviceGroupROAdd.getParticipantIdentifier(), lst.get(0).getParticipantIdentifier());
assertEquals(SMLAction.REGISTER, lst.get(1).getStatus());
assertEquals(serviceGroupROAdd.getParticipantIdentifier(), lst.get(1).getParticipantIdentifier());
assertEquals(SMLAction.UNREGISTER, lst.get(2).getStatus());
assertEquals(serviceGroupRORemove.getParticipantIdentifier(), lst.get(2).getParticipantIdentifier());
assertEquals(SMLAction.REGISTER, lst.get(3).getStatus());
assertEquals(dbDomain2.getDomainCode(), lst.get(3).getDomain().getDomainCode());
assertEquals(serviceGroupROUpdate.getParticipantIdentifier(), lst.get(3).getParticipantIdentifier());
assertEquals(SMLAction.UNREGISTER, lst.get(4).getStatus());
assertEquals(dbDomain1.getDomainCode(), lst.get(4).getDomain().getDomainCode());
assertEquals(5, integrationMock.getParticipantManagmentClientMocks().size());
}
*/
}
package eu.europa.ec.edelivery.smp.services.ui;
import eu.europa.ec.edelivery.smp.data.model.doc.DBSubresource;
import eu.europa.ec.edelivery.smp.data.ui.ServiceMetadataRO;
import eu.europa.ec.edelivery.smp.data.ui.ServiceMetadataValidationRO;
import eu.europa.ec.edelivery.smp.services.AbstractServiceIntegrationTest;
import eu.europa.ec.edelivery.smp.services.ConfigurationService;
import eu.europa.ec.edelivery.smp.testutil.TestDBUtils;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.transaction.annotation.Transactional;
import java.io.IOException;
import java.security.cert.CertificateException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import static eu.europa.ec.edelivery.smp.testutil.TestConstants.*;
import static org.junit.Assert.*;
@Ignore
@ContextConfiguration(classes = {UIServiceGroupSearchService.class, UIServiceMetadataService.class})
public class UIServiceMetadataServiceTest extends AbstractServiceIntegrationTest {
private static final String RES_PATH = "/examples/services/";
private static final String RES_PATH_CONV = "/examples/conversion/";
@Autowired
protected UIServiceMetadataService testInstance;
@Before
@Transactional
public void prepareDatabase() {
prepareDatabaseForSingleDomainEnv();
}
/*
@Test
public void getServiceMetadataXMLById() {
Optional<DBSubresource> smd = serviceMetadataDao.findServiceMetadata(TEST_SG_ID_1, TEST_SG_SCHEMA_1, TEST_DOC_ID_1,
TEST_DOC_SCHEMA_1);
assertTrue(smd.isPresent());
ServiceMetadataRO smdro = testInstance.getServiceMetadataXMLById(smd.get().getId());
assertNotNull(smdro);
assertNotNull(smdro.getXmlContent());
assertEquals(smd.get().getId(), smdro.getId());
}
@Test
public void validateServiceMetadataValid() {
DBSubresource md = TestDBUtils.createDBSubresource("partId", TEST_SG_SCHEMA_1);
ServiceMetadataValidationRO smv = new ServiceMetadataValidationRO();
smv.setDocumentIdentifier(md.getIdentifierValue());
smv.setDocumentIdentifierScheme(md.getIdentifierScheme());
smv.setParticipantIdentifier("partId");
smv.setParticipantScheme(TEST_SG_SCHEMA_1);
smv.setXmlContent(new String(md.getXmlContent()));
smv = testInstance.validateServiceMetadata(smv);
assertNull(smv.getErrorMessage());
}
@Test
public void validateServiceMetadataRedirectValid() {
DBSubresource md = TestDBUtils.createDBSubresourceRedirect("docId", "docSch", "http://10.1.1.10:1027/test-service-data");
ServiceMetadataValidationRO smv = new ServiceMetadataValidationRO();
smv.setDocumentIdentifier(md.getIdentifierValue());
smv.setDocumentIdentifierScheme(md.getIdentifierScheme());
smv.setParticipantIdentifier("partId");
smv.setParticipantScheme(TEST_SG_SCHEMA_1);
smv.setXmlContent(new String(md.getXmlContent()));
smv = testInstance.validateServiceMetadata(smv);
assertNull(smv.getErrorMessage());
}
@Test
public void validateServiceMetadataRedirectInvalid() {
DBSubresource md = TestDBUtils.createDBSubresourceRedirect("docId", "docSch", "");
ServiceMetadataValidationRO smv = new ServiceMetadataValidationRO();
smv.setDocumentIdentifier(md.getIdentifierValue());
smv.setDocumentIdentifierScheme(md.getIdentifierScheme());
smv.setParticipantIdentifier("partId");
smv.setParticipantScheme(TEST_SG_SCHEMA_1);
smv.setXmlContent(new String(md.getXmlContent()));
smv = testInstance.validateServiceMetadata(smv);
assertNotNull(smv.getErrorMessage());
assertEquals("Redirect URL must must be empty!", smv.getErrorMessage());
}
@Test
public void validateServiceMetadataParticipantNotMatch() {
DBSubresource md = TestDBUtils.createDBSubresource("partId", TEST_SG_SCHEMA_1);
ServiceMetadataValidationRO smv = new ServiceMetadataValidationRO();
smv.setDocumentIdentifier(md.getIdentifierValue());
smv.setDocumentIdentifierScheme(md.getIdentifierScheme());
smv.setParticipantIdentifier("partIdNotMatch");
smv.setParticipantScheme(TEST_SG_SCHEMA_1);
smv.setXmlContent(new String(md.getXmlContent()));
smv = testInstance.validateServiceMetadata(smv);
assertEquals("Participant identifier and scheme do not match!",smv.getErrorMessage());
}
@Test
public void validateServiceMetadataDocumentNotMatch() {
DBSubresource md = TestDBUtils.createDBSubresource("partId", TEST_SG_SCHEMA_1);
ServiceMetadataValidationRO smv = new ServiceMetadataValidationRO();
smv.setDocumentIdentifier(md.getIdentifierScheme());
smv.setDocumentIdentifierScheme(md.getIdentifierValue());
smv.setParticipantIdentifier("partId");
smv.setParticipantScheme(TEST_SG_SCHEMA_1);
smv.setXmlContent(new String(md.getXmlContent()));
smv = testInstance.validateServiceMetadata(smv);
assertEquals("Document identifier and scheme do not match!",smv.getErrorMessage());
}
@Test
public void validateServiceMetadataInvalidXML() {
DBSubresource md = TestDBUtils.createDBSubresource("partId", TEST_SG_SCHEMA_1);
ServiceMetadataValidationRO smv = new ServiceMetadataValidationRO();
smv.setDocumentIdentifier(md.getIdentifierScheme());
smv.setDocumentIdentifierScheme(md.getIdentifierValue());
smv.setParticipantIdentifier("partId");
smv.setParticipantScheme(TEST_SG_SCHEMA_1);
smv.setXmlContent(new String(md.getXmlContent()) + "Something to invalidate xml");
smv = testInstance.validateServiceMetadata(smv);
assertEquals("SAXParseException: Content is not allowed in trailing section.",smv.getErrorMessage());
}
@Test
public void testSearchAllEndpoints() throws IOException {
//given
byte[] inputDoc = XmlTestUtils.loadDocumentAsByteArray(RES_PATH + "ServiceMetadataDifferentCertificatesTypes.xml");
ServiceMetadata serviceMetadata = ServiceMetadataConverter.unmarshal(inputDoc);
List<EndpointType> endpointTypeList = testInstance.searchAllEndpoints(serviceMetadata);
assertEquals(3, endpointTypeList.size());
}
@Test
public void testSearchAllEndpointsEmptyList() throws IOException {
//given
byte[] inputDoc = XmlTestUtils.loadDocumentAsByteArray(RES_PATH_CONV + "ServiceMetadataWithRedirect.xml");
ServiceMetadata serviceMetadata = ServiceMetadataConverter.unmarshal(inputDoc);
List<EndpointType> endpointTypeList = testInstance.searchAllEndpoints(serviceMetadata);
assertEquals(0, endpointTypeList.size());
}
@Test
public void testValidateServiceMetadataCertificatesEmptyOK() throws IOException, CertificateException {
//given
byte[] inputDoc = XmlTestUtils.loadDocumentAsByteArray(RES_PATH + "ServiceMetadataDifferentCertificatesTypes.xml");
ServiceMetadata serviceMetadata = ServiceMetadataConverter.unmarshal(inputDoc);
// then
testInstance.validateServiceMetadataCertificates(serviceMetadata);
// no error is expected
}
@Test
public void testValidateServiceMetadataCertificatesRSAOK() throws IOException, CertificateException {
ConfigurationService configurationService = Mockito.mock(ConfigurationService.class);
UIServiceMetadataService testInstance = new UIServiceMetadataService(null, null,
null, null,
configurationService);
Mockito.doReturn(Arrays.asList("RSA","ED25519","ED448")).when(configurationService).getAllowedDocumentCertificateTypes();
//given
byte[] inputDoc = XmlTestUtils.loadDocumentAsByteArray(RES_PATH + "ServiceMetadataDifferentCertificatesTypes.xml");
ServiceMetadata serviceMetadata = ServiceMetadataConverter.unmarshal(inputDoc);
// then
testInstance.validateServiceMetadataCertificates(serviceMetadata);
}
@Test
public void testValidateServiceMetadataCertificatesNotAllowed() throws IOException{
ConfigurationService configurationService = Mockito.mock(ConfigurationService.class);
UIServiceMetadataService testInstance = new UIServiceMetadataService(null, null,
null, null,
configurationService);
Mockito.doReturn(Collections.singletonList("testKeyAlg")).when(configurationService).getAllowedDocumentCertificateTypes();
//given
byte[] inputDoc = XmlTestUtils.loadDocumentAsByteArray(RES_PATH + "ServiceMetadataDifferentCertificatesTypes.xml");
ServiceMetadata serviceMetadata = ServiceMetadataConverter.unmarshal(inputDoc);
// then
CertificateException result = assertThrows(CertificateException.class, () -> testInstance.validateServiceMetadataCertificates(serviceMetadata));
// no error is expected
assertEquals("Certificate does not have allowed key type!", result.getMessage());
}
*/
}
...@@ -6,7 +6,7 @@ import eu.europa.ec.edelivery.smp.data.ui.ServiceGroupSearchRO; ...@@ -6,7 +6,7 @@ import eu.europa.ec.edelivery.smp.data.ui.ServiceGroupSearchRO;
import eu.europa.ec.edelivery.smp.data.ui.ServiceResult; import eu.europa.ec.edelivery.smp.data.ui.ServiceResult;
import eu.europa.ec.edelivery.smp.logging.SMPLogger; import eu.europa.ec.edelivery.smp.logging.SMPLogger;
import eu.europa.ec.edelivery.smp.logging.SMPLoggerFactory; import eu.europa.ec.edelivery.smp.logging.SMPLoggerFactory;
import eu.europa.ec.edelivery.smp.services.ui.UIServiceGroupSearchService; import eu.europa.ec.edelivery.smp.services.ui.UIResourceSearchService;
import eu.europa.ec.edelivery.smp.services.ui.filters.ResourceFilter; import eu.europa.ec.edelivery.smp.services.ui.filters.ResourceFilter;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.util.MimeTypeUtils; import org.springframework.util.MimeTypeUtils;
...@@ -29,10 +29,10 @@ public class SearchResource { ...@@ -29,10 +29,10 @@ public class SearchResource {
private static final SMPLogger LOG = SMPLoggerFactory.getLogger(SearchResource.class); private static final SMPLogger LOG = SMPLoggerFactory.getLogger(SearchResource.class);
final UIServiceGroupSearchService uiServiceGroupService; final UIResourceSearchService uiServiceGroupService;
final DomainDao domainDao; final DomainDao domainDao;
public SearchResource(UIServiceGroupSearchService uiServiceGroupService, DomainDao domainDao) { public SearchResource(UIResourceSearchService uiServiceGroupService, DomainDao domainDao) {
this.uiServiceGroupService = uiServiceGroupService; this.uiServiceGroupService = uiServiceGroupService;
this.domainDao = domainDao; this.domainDao = domainDao;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment