use of com.icthh.xm.ms.entity.domain.spec.StateSpec in project xm-ms-entity by xm-online.
the class StateKeyValidator method isValid.
@Override
public boolean isValid(XmEntity value, ConstraintValidatorContext context) {
TypeSpec typeSpec = xmEntitySpecService.findTypeByKey(value.getTypeKey());
if (typeSpec == null) {
return true;
}
if (isEmpty(typeSpec.getStates()) && value.getStateKey() == null) {
return true;
}
List<StateSpec> stateSpecs = typeSpec.getStates();
stateSpecs = (stateSpecs != null) ? stateSpecs : Collections.emptyList();
Set<String> stateKeys = stateSpecs.stream().map(StateSpec::getKey).collect(toSet());
log.debug("Type specification states {}, checked state {}", stateKeys, value.getStateKey());
return stateKeys.contains(value.getStateKey());
}
use of com.icthh.xm.ms.entity.domain.spec.StateSpec in project xm-ms-entity by xm-online.
the class XmEntityResourceIntTest method changeStateError.
@Test
@Transactional
public void changeStateError() throws Exception {
XmEntitySpecService xmEntitySpecService = Mockito.mock(XmEntitySpecService.class);
StateSpec nextSpec = new StateSpec();
nextSpec.setKey("NEXT_STATE");
when(xmEntitySpecService.nextStates(eq(DEFAULT_TYPE_KEY), eq(DEFAULT_STATE_KEY))).thenReturn(Collections.singletonList(nextSpec));
XmEntity tenant = createEntity(em);
xmEntityServiceImpl.save(tenant);
restXmEntityMockMvc.perform(put("/api/xm-entities/{idOrKey}/states/{stateKey}", tenant.getId(), "INVALID_NEXT_STATE").contentType(TestUtil.APPLICATION_JSON_UTF8)).andExpect(status().is4xxClientError());
}
use of com.icthh.xm.ms.entity.domain.spec.StateSpec in project xm-ms-entity by xm-online.
the class XmEntityServiceImpl method updateState.
@Override
public XmEntity updateState(IdOrKey idOrKey, String stateKey, Map<String, Object> context) {
XmEntityStateProjection entity = findStateProjectionById(idOrKey).orElseThrow(() -> new EntityNotFoundException("XmEntity with key [" + idOrKey.getKey() + "] not found"));
List<StateSpec> stateSpecs = xmEntitySpecService.nextStates(entity.getTypeKey(), entity.getStateKey());
if (stateSpecs.stream().map(StateSpec::getKey).anyMatch(stateKey::equals)) {
LifecycleLepStrategy lifecycleLepStrategy = lifecycleLepStrategyFactory.getLifecycleLepStrategy();
return lifecycleLepStrategy.changeState(idOrKey, entity.getTypeKey(), entity.getStateKey(), stateKey, context);
} else {
throw new BusinessException(ErrorConstants.ERR_VALIDATION, "Entity " + entity + " can not go from [" + entity.getStateKey() + "] to [" + stateKey + "]");
}
}
Aggregations