Skip to content

Commit

Permalink
KAA-1146: minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
nocs00 committed Aug 18, 2016
1 parent eb25863 commit bcb7c82
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 62 deletions.
Expand Up @@ -51,15 +51,15 @@ public interface EventClassService {
* @param id the string id of event class family version * @param id the string id of event class family version
* @return the event class family dto object * @return the event class family dto object
*/ */
EventClassFamilyDto findEventClassFamilyByECFVersionId(String id); EventClassFamilyDto findEventClassFamilyByEcfvId(String id);


/** /**
* Find event class family versions by id. * Find event class family versions by id.
* *
* @param id the string id of event class family * @param ecfId the string id of event class family
* @return the event class family dto object * @return the event class family dto object
*/ */
List<EventClassFamilyVersionDto> findEventClassFamilyVersionsById(String id); List<EventClassFamilyVersionDto> findEventClassFamilyVersionsByEcfId(String ecfId);


/** /**
* Save event class family. * Save event class family.
Expand Down Expand Up @@ -88,7 +88,15 @@ public interface EventClassService {
*/ */
List<EventClassDto> findEventClassesByFamilyIdVersionAndType(String ecfId, int version, EventClassType type); List<EventClassDto> findEventClassesByFamilyIdVersionAndType(String ecfId, int version, EventClassType type);


boolean validateEventClassFamilyFqns(String eventClassFamilyId, List<String> fqns); /**
* Check passed FQNs if they are present in event class family.
* FQNs in scope of event class family should be unique.
*
* @param ecfId the string id of event class family
* @param fqns list of fqns to check against family fqns
* @return true is fqns are unique
*/
boolean validateEventClassFamilyFqns(String ecfId, List<String> fqns);


/** /**
* Find event class family by tenant id and name. * Find event class family by tenant id and name.
Expand Down Expand Up @@ -135,5 +143,11 @@ public interface EventClassService {
*/ */
boolean isValidECFListInSdkProfile(List<AefMapInfoDto> ecfList); boolean isValidECFListInSdkProfile(List<AefMapInfoDto> ecfList);


List<String> getFqnListForECF(String eventClassId); /**
* Get list of all events class FQNs in event class family.
*
* @param ecfId string of the event class family id
* @return list of all FQNs
*/
List<String> getFqnListForECF(String ecfId);
} }
Expand Up @@ -61,16 +61,6 @@ public interface EventClassDao<T> extends SqlDao<T> {
*/ */
List<T> findByTenantIdAndFqn(String tenantId, String fqn); List<T> findByTenantIdAndFqn(String tenantId, String fqn);


/**
* Validate list of FQNs for uniqueness within the tenant.
*
* @param tenantId the tenant id
* @param ecfId the event class family id
* @param fqns the list of FQNs
* @return true if FQNs are unique otherwise false
*/
boolean validateFqns(String tenantId, String ecfId, List<String> fqns);

/** /**
* @param tenantId the tenant id * @param tenantId the tenant id
* @param fqn the FQN * @param fqn the FQN
Expand Down
Expand Up @@ -139,27 +139,5 @@ public EventClass findByTenantIdAndFqnAndVersion(String tenantId, String fqn, in
} }
return eventClass; return eventClass;
} }


@Override
public boolean validateFqns(String tenantId, String ecfId, List<String> fqns) { //fixme: drop this as unused?
List<EventClass> eventClasses = Collections.emptyList();
if (isNotBlank(tenantId) && isNotBlank(ecfId) && fqns != null && !fqns.isEmpty()) {
if (LOG.isTraceEnabled()) {
LOG.trace("Validating FQNs by tenant id [{}], ecf id [{}] and FQNs [{}]", tenantId, ecfId, Arrays.toString(fqns.toArray()));
} else {
LOG.debug("Validating FQNs by tenant id [{}], ecf id [{}] and FQNs [{}]", tenantId, ecfId, fqns.size());
}
Criteria criteria = getCriteria();
criteria.createAlias(TENANT_PROPERTY, TENANT_ALIAS);
criteria.createAlias(ECFV_PROPERTY, ECFV_ALIAS);
criteria.add(Restrictions.and(
Restrictions.eq(TENANT_REFERENCE, Long.valueOf(tenantId)),
Restrictions.ne(ECFV_REFERENCE, Long.valueOf(ecfId)),
Restrictions.in(FQN_PROPERTY, fqns)));
eventClasses = findListByCriteria(criteria);
}
boolean result = eventClasses == null || eventClasses.isEmpty();
LOG.debug("[{},{}] Validating result: {}.", tenantId, ecfId, result);
return result;
}
} }
Expand Up @@ -29,7 +29,11 @@
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;


import static org.kaaproject.kaa.server.common.dao.DaoConstants.*; import static org.kaaproject.kaa.server.common.dao.DaoConstants.EVENT_CLASS_FAMILY_VERSION_TABLE_NAME;
import static org.kaaproject.kaa.server.common.dao.DaoConstants.EVENT_CLASS_EVENT_CLASS_FAMILY_VERSION_ID;
import static org.kaaproject.kaa.server.common.dao.DaoConstants.EVENT_CLASS_FAMILY_VERSION_VERSION;
import static org.kaaproject.kaa.server.common.dao.DaoConstants.EVENT_CLASS_FAMILY_VERSION_CREATED_USERNAME;
import static org.kaaproject.kaa.server.common.dao.DaoConstants.EVENT_CLASS_FAMILY_VERSION_CREATED_TIME;
import static org.kaaproject.kaa.server.common.dao.model.sql.ModelUtils.getLongId; import static org.kaaproject.kaa.server.common.dao.model.sql.ModelUtils.getLongId;


@Entity @Entity
Expand Down
Expand Up @@ -99,7 +99,7 @@ public EventClassFamilyDto findEventClassFamilyById(String id) {
} }


@Override @Override
public EventClassFamilyDto findEventClassFamilyByECFVersionId(String id) { public EventClassFamilyDto findEventClassFamilyByEcfvId(String id) {
validateSqlId(id, "Event class family version id is incorrect. Can't find event class family by ECF version id " + id); validateSqlId(id, "Event class family version id is incorrect. Can't find event class family by ECF version id " + id);


EventClassFamily eventClassFamily = eventClassFamilyDao.find().stream(). EventClassFamily eventClassFamily = eventClassFamilyDao.find().stream().
Expand All @@ -110,9 +110,9 @@ public EventClassFamilyDto findEventClassFamilyByECFVersionId(String id) {
} }


@Override @Override
public List<EventClassFamilyVersionDto> findEventClassFamilyVersionsById(String id) { public List<EventClassFamilyVersionDto> findEventClassFamilyVersionsByEcfId(String ecfId) {
validateSqlId(id, "Event class family id is incorrect. Can't find event class family by id " + id); validateSqlId(ecfId, "Event class family id is incorrect. Can't find event class family by id " + ecfId);
EventClassFamily ecf = eventClassFamilyDao.findById(id); EventClassFamily ecf = eventClassFamilyDao.findById(ecfId);
return convertDtoList(ecf.getSchemas()); return convertDtoList(ecf.getSchemas());
} }


Expand Down Expand Up @@ -160,7 +160,7 @@ public void addEventClassFamilyVersion(String eventClassFamilyId,
fqns.add(eventClass.getFqn()); fqns.add(eventClass.getFqn());
} }
if (validateEventClassFamilyFqns(eventClassFamily.getId(), fqns)) { if (validateEventClassFamilyFqns(eventClassFamily.getId(), fqns)) {
List<EventClassFamilyVersionDto> schemasDto = findEventClassFamilyVersionsById(eventClassFamilyId); List<EventClassFamilyVersionDto> schemasDto = findEventClassFamilyVersionsByEcfId(eventClassFamilyId);
int version = 1; int version = 1;
if (schemasDto != null && !schemasDto.isEmpty()) { if (schemasDto != null && !schemasDto.isEmpty()) {
Collections.sort(schemasDto, new Comparator<EventClassFamilyVersionDto>() { Collections.sort(schemasDto, new Comparator<EventClassFamilyVersionDto>() {
Expand Down Expand Up @@ -281,15 +281,15 @@ public boolean isValidECFListInSdkProfile(List<AefMapInfoDto> ecfList) {
} }


@Override @Override
public List<String> getFqnListForECF(String eventClassId) { public List<String> getFqnListForECF(String ecfId) {
if (isValidSqlId(eventClassId)) { if (isValidSqlId(ecfId)) {
LOG.debug("Find event class by id [{}] ", eventClassId); LOG.debug("Get fqn list for event class family by id [{}] ", ecfId);
List<String> storedFQNs = new ArrayList<>(); List<String> storedFQNs = new ArrayList<>();
EventClassFamily ecf = eventClassFamilyDao.findById(eventClassId); EventClassFamily ecf = eventClassFamilyDao.findById(ecfId);
ecf.getSchemas().forEach(ecfv -> ecfv.getRecords().forEach(ec -> storedFQNs.add(ec.getFqn()))); ecf.getSchemas().forEach(ecfv -> ecfv.getRecords().forEach(ec -> storedFQNs.add(ec.getFqn())));
return storedFQNs; return storedFQNs;
} else { } else {
throw new IncorrectParameterException("Incorrect event class id: " + eventClassId); throw new IncorrectParameterException("Incorrect event class family id: " + ecfId);
} }
} }
} }
Expand Up @@ -1029,12 +1029,43 @@ void addEventClassFamilyVersion(String eventClassFamilyId, EventClassFamilyVersi
List<EventClassDto> getEventClassesByFamilyIdVersionAndType(String ecfId, int version, EventClassType type) List<EventClassDto> getEventClassesByFamilyIdVersionAndType(String ecfId, int version, EventClassType type)
throws ControlServiceException; throws ControlServiceException;


/**
* Gets the event class by id.
*
* @param eventClassId
* the event class id
* @return the event class dto
* @throws ControlServiceException
* the control service exception
*/
EventClassDto getEventClassById(String eventClassId) throws ControlServiceException; EventClassDto getEventClassById(String eventClassId) throws ControlServiceException;


boolean validateEventClassFamilyFqns(String eventClassFamilyId, List<String> fqns); /**
* Check passed FQNs if they are present in event class family.
* FQNs in scope of event class family should be unique.
*
* @param ecfId the string id of event class family
* @param fqns list of fqns to check against family fqns
* @return true is fqns are unique
*/
boolean validateEventClassFamilyFqns(String ecfId, List<String> fqns);


List<String> getFqnListForECF(String eventClassId) throws ControlServiceException; /**
* Get list of all events class FQNs in event class family.
*
* @param ecfId string of the event class family id
* @return list of all FQNs
*/
List<String> getFqnListForECF(String ecfId) throws ControlServiceException;


/**
* Check passed event class family mappings for Sdk profile.
* There must not be same FNQs between chosen event class family versions.
*
* @param ecfList list of event class family mappings chosen for Sdk profile
* @throws ControlServiceException
* the control service exception
*/
void validateECFListInSdkProfile(List<AefMapInfoDto> ecfList) throws ControlServiceException; void validateECFListInSdkProfile(List<AefMapInfoDto> ecfList) throws ControlServiceException;


/** /**
Expand Down
Expand Up @@ -1071,7 +1071,7 @@ public FileData generateSdk(SdkProfileDto sdkProfile, SdkPlatform platform) thro
efm.setEcfName(ecf.getName()); efm.setEcfName(ecf.getName());
efm.setEcfNamespace(ecf.getNamespace()); efm.setEcfNamespace(ecf.getNamespace());
efm.setEcfClassName(ecf.getClassName()); efm.setEcfClassName(ecf.getClassName());
List<EventClassFamilyVersionDto> ecfSchemas = eventClassService.findEventClassFamilyVersionsById(aefMap.getEcfId()); List<EventClassFamilyVersionDto> ecfSchemas = eventClassService.findEventClassFamilyVersionsByEcfId(aefMap.getEcfId());
for (EventClassFamilyVersionDto ecfSchema : ecfSchemas) { for (EventClassFamilyVersionDto ecfSchema : ecfSchemas) {
if (ecfSchema.getVersion() == efm.getVersion()) { if (ecfSchema.getVersion() == efm.getVersion()) {
List<EventClassDto> records = eventClassService.findEventClassesByFamilyIdVersionAndType(ecf.getId(), ecfSchema.getVersion(), null); List<EventClassDto> records = eventClassService.findEventClassesByFamilyIdVersionAndType(ecf.getId(), ecfSchema.getVersion(), null);
Expand Down Expand Up @@ -1529,7 +1529,7 @@ public EventClassFamilyDto getEventClassFamily(String eventClassFamilyId) throws
*/ */
@Override @Override
public List<EventClassFamilyVersionDto> getEventClassFamilyVersions(String eventClassFamilyId) throws ControlServiceException { public List<EventClassFamilyVersionDto> getEventClassFamilyVersions(String eventClassFamilyId) throws ControlServiceException {
return eventClassService.findEventClassFamilyVersionsById(eventClassFamilyId); return eventClassService.findEventClassFamilyVersionsByEcfId(eventClassFamilyId);
} }


/* /*
Expand Down Expand Up @@ -1571,13 +1571,13 @@ public EventClassDto getEventClassById(String eventClassId) throws ControlServic
} }


@Override @Override
public boolean validateEventClassFamilyFqns(String eventClassFamilyId, List<String> fqns) { public boolean validateEventClassFamilyFqns(String ecfId, List<String> fqns) {
return eventClassService.validateEventClassFamilyFqns(eventClassFamilyId, fqns); return eventClassService.validateEventClassFamilyFqns(ecfId, fqns);
} }


@Override @Override
public List<String> getFqnListForECF(String eventClassId) { public List<String> getFqnListForECF(String ecfId) {
return eventClassService.getFqnListForECF(eventClassId); return eventClassService.getFqnListForECF(ecfId);
} }


/* /*
Expand Down
Expand Up @@ -694,7 +694,7 @@ public String compute(EventClassFqnKey key) {
List<EventClassDto> eventClasses = eventClassService.findEventClassByTenantIdAndFQN(key.getTenantId(), key.getFqn()); List<EventClassDto> eventClasses = eventClassService.findEventClassByTenantIdAndFQN(key.getTenantId(), key.getFqn());
if (eventClasses != null && !eventClasses.isEmpty()) { if (eventClasses != null && !eventClasses.isEmpty()) {
String ecfvId = eventClasses.get(0).getEcfvId(); String ecfvId = eventClasses.get(0).getEcfvId();
return eventClassService.findEventClassFamilyByECFVersionId(ecfvId).getId(); return eventClassService.findEventClassFamilyByEcfvId(ecfvId).getId();
} else { } else {
LOG.warn("Fetching result for getEcfvId using key {} Failed!", key); LOG.warn("Fetching result for getEcfvId using key {} Failed!", key);
return null; return null;
Expand All @@ -717,7 +717,7 @@ public Set<RouteTableKey> compute(EventClassFqnVersion key) {
key.getVersion()); key.getVersion());


String ecfvId = eventClass.getEcfvId(); String ecfvId = eventClass.getEcfvId();
String ecfId = eventClassService.findEventClassFamilyByECFVersionId(ecfvId).getId(); String ecfId = eventClassService.findEventClassFamilyByEcfvId(ecfvId).getId();


List<ApplicationEventFamilyMapDto> mappingList = applicationEventMapService.findByEcfIdAndVersion(ecfId, List<ApplicationEventFamilyMapDto> mappingList = applicationEventMapService.findByEcfIdAndVersion(ecfId,
key.getVersion()); key.getVersion());
Expand Down
Expand Up @@ -173,7 +173,7 @@ public void testAddEventClassFamilyVersion() throws Exception {
EventClassFamilyDto eventClassFamily = createEventClassFamily(tenantAdmin.getTenantId()); EventClassFamilyDto eventClassFamily = createEventClassFamily(tenantAdmin.getTenantId());
EventClassFamilyVersionDto eventClassFamilyVersion = createEventClassFamilyVersion(tenantAdmin.getTenantId()); EventClassFamilyVersionDto eventClassFamilyVersion = createEventClassFamilyVersion(tenantAdmin.getTenantId());
client.addEventClassFamilyVersion(eventClassFamily.getId(), eventClassFamilyVersion); client.addEventClassFamilyVersion(eventClassFamily.getId(), eventClassFamilyVersion);
List<EventClassFamilyVersionDto> schemas = eventClassService.findEventClassFamilyVersionsById(eventClassFamily.getId()); List<EventClassFamilyVersionDto> schemas = eventClassService.findEventClassFamilyVersionsByEcfId(eventClassFamily.getId());
Assert.assertNotNull(schemas); Assert.assertNotNull(schemas);
Assert.assertEquals(1, schemas.size()); Assert.assertEquals(1, schemas.size());
EventClassFamilyVersionDto eventSchema = schemas.get(0); EventClassFamilyVersionDto eventSchema = schemas.get(0);
Expand All @@ -182,7 +182,7 @@ public void testAddEventClassFamilyVersion() throws Exception {


eventClassFamilyVersion = createEventClassFamilyVersion(tenantAdmin.getTenantId()); eventClassFamilyVersion = createEventClassFamilyVersion(tenantAdmin.getTenantId());
client.addEventClassFamilyVersion(eventClassFamily.getId(), eventClassFamilyVersion); client.addEventClassFamilyVersion(eventClassFamily.getId(), eventClassFamilyVersion);
schemas = eventClassService.findEventClassFamilyVersionsById(eventClassFamily.getId()); schemas = eventClassService.findEventClassFamilyVersionsByEcfId(eventClassFamily.getId());
Assert.assertNotNull(schemas); Assert.assertNotNull(schemas);
Assert.assertEquals(2, schemas.size()); Assert.assertEquals(2, schemas.size());
eventSchema = schemas.get(1); eventSchema = schemas.get(1);
Expand Down
Expand Up @@ -338,7 +338,7 @@ public EventClassFamilyDto answer(InvocationOnMock invocation) throws Throwable
} }
}); });


when(eventClassService.findEventClassFamilyByECFVersionId(EVENT_CLASS_FAMILY_VERSION_ID)).then(new Answer<EventClassFamilyDto>() { when(eventClassService.findEventClassFamilyByEcfvId(EVENT_CLASS_FAMILY_VERSION_ID)).then(new Answer<EventClassFamilyDto>() {
@Override @Override
public EventClassFamilyDto answer(InvocationOnMock invocation) throws Throwable { public EventClassFamilyDto answer(InvocationOnMock invocation) throws Throwable {
sleepABit(); sleepABit();
Expand Down

0 comments on commit bcb7c82

Please sign in to comment.