...
Code Block |
---|
language | java |
---|
title | JpaToscaProperty |
---|
linenumbers | true |
---|
|
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;
@Entity
@Table(name = "ToscaProperty")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@Data
@EqualsAndHashCode(callSuper = false)
public class JpaToscaProperty extends PfConcept implements PfAuthorative<ToscaProperty> {
private static final long serialVersionUID = 1675770231921107988L;
@EmbeddedId
@VerifyKey
@NotNull
private PfReferenceKey key;
@Column
@VerifyKey
@NotNull
private PfConceptKey type;
@Column
@NotBlank
private String description;
|
Repository
Using Spring Data JPA it needs to implement a repository.
Code Block |
---|
language | java |
---|
title | JpaToscaPropertyRepository |
---|
linenumbers | true |
---|
|
@Repository
public interface JpaToscaPropertyRepository extends JpaRepository<JpaToscaProperty, PfReferenceKey> {
}
|
and also it needs to implement a JUnit test
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());
}
} |