...
Code Block |
---|
language | java |
---|
title | JpaToscaPropertyRepositoryTest |
---|
linenumbers | true |
---|
|
@ExtendWith(SpringExtension.class)
@DataJpaTest
@Import(value = ParticipantPolicyParameters.class)
@TestPropertySource(locations = {"classpath:application_test.properties"})
class JpaToscaPropertyRepositoryTest {
@Autowired
private JpaToscaPropertyRepository toscaPropertyRepository;
@Test
void test() {
JpaToscaProperty toscaProperty = new JpaToscaProperty();
PfReferenceKey key = toscaProperty.getKey();
Map<String, String> metadata = new HashMap<>();
metadata.put("Key", "Value");
metadata.put("K", "V");
List<JpaToscaConstraint> constraints = new ArrayList<>();
String[] list = new String[] {"First", "Second"};
constraints.add(new JpaToscaConstraintValidValues(Stream.of(list).collect(Collectors.toList())));
toscaProperty.setDefaultValue("DefaultValue");
toscaProperty.setDescription("Description");
toscaProperty.setRequired(true);
toscaProperty.setStatus(ToscaProperty.Status.EXPERIMENTAL);
toscaProperty.setMetadata(metadata);
toscaProperty.setConstraints(constraints);
toscaPropertyRepository.save(toscaProperty);
Optional<JpaToscaProperty> opt = toscaPropertyRepository.findById(key);
assertThat(opt).isNotEmpty();
JpaToscaProperty actual = opt.get();
assertThat(actual.getDefaultValue()).isEqualTo(toscaProperty.getDefaultValue());
assertThat(actual.getDescription()).isEqualTo(toscaProperty.getDescription());
assertThat(actual.isRequired()).isEqualTo(toscaProperty.isRequired());
assertThat(actual.getStatus()).isEqualTo(toscaProperty.getStatus());
assertThat(actual.getType()).isEqualTo(toscaProperty.getType());
assertThat(actual.getConstraints()).isEqualTo(toscaProperty.getConstraints());
}
} |
Example using Transactional
Code Block |
---|
language | java |
---|
title | ControlLoopInstantiationProvider |
---|
linenumbers | true |
---|
|
@Service
public class ControlLoopInstantiationProvider {
private final ControlLoopProvider controlLoopProvider;
private final CommissioningProvider commissioningProvider;
/**
* Create a instantiation provider.
*
* @param databaseProviderParameters the parameters for database access
*/
public ControlLoopInstantiationProvider(ControlLoopProvider controlLoopProvider,
CommissioningProvider commissioningProvider) {
this.controlLoopProvider = controlLoopProvider;
this.commissioningProvider = commissioningProvider;
}
@Transactional
private void createControlLoopsOp(ControlLoops controlLoops) throws PfModelException {
for (ControlLoop controlLoop : controlLoops.getControlLoopList()) {
ControlLoop checkControlLoop = controlLoopProvider.getControlLoop(controlLoop.getKey().asIdentifier());
if (checkControlLoop != null) {
throw new PfModelException(Response.Status.BAD_REQUEST,
controlLoop.getKey().asIdentifier() + " already defined");
}
}
BeanValidationResult validationResult = validateControlLoops(controlLoops);
if (!validationResult.isValid()) {
throw new PfModelException(Response.Status.BAD_REQUEST, validationResult.getResult());
}
controlLoopProvider.createControlLoops(controlLoops.getControlLoopList());
}
/**
* Create control loops.
*
* @param controlLoops the control loop
* @return the result of the instantiation operation
* @throws PfModelException on creation errors
*/
public InstantiationResponse createControlLoops(ControlLoops controlLoops) throws PfModelException {
createControlLoopsOp(controlLoops);
InstantiationResponse response = new InstantiationResponse();
response.setAffectedControlLoops(controlLoops.getControlLoopList().stream()
.map(cl -> cl.getKey().asIdentifier()).collect(Collectors.toList()));
return response;
}
|
...