...
To better understand these functionality, it has been created a Demo Application. That it is available here (https://gerrit.nordix.org/c/local/onap/clamp-pilot/+/11966).
The Java code shown below, is the model used by Rest Api Controller. (Validations and Swagger annotations have been removed for simplicity)
...
The Json shown below is an example using of the model shown before.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
{ "key": "1", "text": "This is an example", "properties": { "additionalProp1": "Val 10", "additionalProp2": "11", "additionalProp3": "12" }, "document": { "name": "DocumentExample", "elements": { "additionalProp1": { "name": "ElementExample 1", "tags": [ "tag1" ] }, "additionalProp2": { "name": "ElementExample 2", "tags": [ "tag2" ] }, "additionalProp3": { "name": "ElementExample 3", "tags": [ "tag3" ] } } } } |
...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
@Repository public interface ItemRepository extends JpaRepository<Item, String> { @Query(value = " SELECT * FROM item where document['name'] @> :value ", nativeQuery = true) Stream<Item> findByName(@Param("value") String value); @Modifying @Query(value = " UPDATE item SET document['name']=:value where key=:key ", nativeQuery = true) void updateNameByKey(@Param("key") String key, @Param("value") String value); @Query( value = " SELECT Cast(jsonb_path_query_array(document['elements'], '$[*].keyvalue().value.tags') as varchar) FROM item ", nativeQuery = true) Stream<String> findTags(); } |
In order to make unit testtests, we can not use H2 due of the incompatibility. Spring supports Testcontainers where we can configure a PostgreSQL Container that will up and running during tests.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
@Testcontainers @ExtendWith(SpringExtension.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) @ContextConfiguration( initializers = DemoApplicationTests.Initializer.class, classes = DemoApplication.class) @TestPropertySource(locations = {"classpath:application_test.properties"}) class DemoApplicationTests { @Test void contextLoads() { } @Container public static PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer("postgres:latest") .withDatabaseName("db_demo").withUsername("sa").withPassword("sa"); @Configuration static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> { @Override public void initialize(ConfigurableApplicationContext configurableApplicationContext) { TestPropertyValues .of("spring.datasource.url=" + postgreSQLContainer.getJdbcUrl(), "spring.datasource.username=" + postgreSQLContainer.getUsername(), "spring.datasource.password=" + postgreSQLContainer.getPassword()) .applyTo(configurableApplicationContext.getEnvironment()); } } } |
...
Use case scenarios
- The principal model used by Business logic is the Json model
- Collection of values: Map of String as example
...