diff options
author | 2021-08-05 14:22:21 +0000 | |
---|---|---|
committer | 2021-08-05 14:22:21 +0000 | |
commit | 73be382a3558e2d6f1583cbb9bc7bd8d51be059b (patch) | |
tree | bce1335a4154eeff9c961c8e4b0295f5801b6ffc /src/test | |
parent | 8169b24cac135fcaf731bb8014d0115362fda6d5 (diff) | |
parent | c2b5e8523d874302d0f3c7f042d23bacf6d856a3 (diff) |
Merge "Fetch data filtered by Search criteria"
Diffstat (limited to 'src/test')
5 files changed, 407 insertions, 11 deletions
diff --git a/src/test/groovy/org/onap/cps/temporal/domain/SearchCriteriaSpec.groovy b/src/test/groovy/org/onap/cps/temporal/domain/SearchCriteriaSpec.groovy new file mode 100644 index 0000000..d7b6d1f --- /dev/null +++ b/src/test/groovy/org/onap/cps/temporal/domain/SearchCriteriaSpec.groovy @@ -0,0 +1,132 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (c) 2021 Bell Canada. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.cps.temporal.domain + +import org.springframework.data.domain.Sort +import spock.lang.Specification + +import java.time.OffsetDateTime + +class SearchCriteriaSpec extends Specification { + + def myDataspace = 'my-dataspace' + def myAnchorName = 'my-anchor' + def myschemaSetName = 'my-schemaset' + + + def 'Search Criteria has default values if not provided.'() { + def myPayloadFilter = '{"status": "down"}' + when: 'search criteria is created' + def searchCriteria = SearchCriteria.builder() + .dataspaceName(myDataspace) + .schemaSetName(myschemaSetName) + .pagination(0, 10) + .simplePayloadFilter(myPayloadFilter) + .build() + + then: 'search criteria has default value for sort' + searchCriteria.getPageable().getSort() == Sort.by(Sort.Direction.DESC, 'observed_timestamp') + and: 'created before has almost current time as default value' + OffsetDateTime.now().minusMinutes(5).isBefore(searchCriteria.getCreatedBefore()) + and: 'contains the provided value to builder' + searchCriteria.getDataspaceName() == myDataspace + searchCriteria.getSchemaSetName() == myschemaSetName + searchCriteria.getSimplePayloadFilter() == myPayloadFilter + searchCriteria.getPageable().getPageNumber() == 0 + searchCriteria.getPageable().getPageSize() == 10 + + } + + def 'Search Criteria with the provided values.'() { + + given: 'sort by parameter' + def sortBy = Sort.by(Sort.Direction.ASC, 'observed_timestamp') + and: 'data created one day ago' + def lastDayAsCreatedBefore = OffsetDateTime.now().minusDays(1) + and: 'observed timestamp' + def nowAsObservedAfter = OffsetDateTime.now() + + when: 'search criteria is created' + def searchCriteria = SearchCriteria.builder() + .dataspaceName(myDataspace) + .schemaSetName(myschemaSetName) + .anchorName(myAnchorName) + .pagination(0, 10) + .sort(sortBy) + .observedAfter(nowAsObservedAfter) + .createdBefore(lastDayAsCreatedBefore) + .build() + + then: 'search criteria has expected value' + with(searchCriteria) { + dataspaceName == myDataspace + schemaSetName == myschemaSetName + anchorName == myAnchorName + observedAfter == nowAsObservedAfter + createdBefore == lastDayAsCreatedBefore + pageable.getPageNumber() == 0 + pageable.getPageSize() == 10 + pageable.getSort() == sortBy + } + } + + def 'Error handling: missing dataspace.'() { + when: 'search criteria is created without dataspace' + SearchCriteria.builder() + .anchorName(myAnchorName) + .pagination(0, 10) + .build() + then: 'exception is thrown' + thrown(IllegalStateException) + } + + def 'Error handling: missing both schemaset and anchor.'() { + when: 'search criteria is created without schemaset and anchor' + SearchCriteria.builder() + .dataspaceName(myDataspace) + .pagination(0, 10) + .build() + then: 'exception is thrown' + thrown(IllegalStateException) + } + + def 'Error handling: missing pagination.'() { + when: 'search criteria is created without pagination' + SearchCriteria.builder() + .dataspaceName(myDataspace) + .anchorName(myAnchorName) + .build() + then: 'exception is thrown' + thrown(IllegalStateException) + } + + def 'Error Handling: sort must be not null.'() { + when: 'search criteria is created without sorting information' + SearchCriteria.builder() + .dataspaceName(myDataspace) + .anchorName(myAnchorName) + .pagination(0, 1) + .sort(null) + .build() + then: 'exception is thrown' + thrown(IllegalArgumentException) + } + +} diff --git a/src/test/groovy/org/onap/cps/temporal/repository/NetworkDataRepositoryImplSpec.groovy b/src/test/groovy/org/onap/cps/temporal/repository/NetworkDataRepositoryImplSpec.groovy new file mode 100644 index 0000000..a5cc721 --- /dev/null +++ b/src/test/groovy/org/onap/cps/temporal/repository/NetworkDataRepositoryImplSpec.groovy @@ -0,0 +1,206 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (c) 2021 Bell Canada. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.temporal.repository + +import org.onap.cps.temporal.domain.NetworkData +import org.onap.cps.temporal.domain.SearchCriteria +import org.onap.cps.temporal.repository.containers.TimescaleContainer +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest +import org.springframework.data.domain.PageRequest +import org.springframework.data.domain.Slice +import org.springframework.data.domain.Sort +import org.springframework.test.context.jdbc.Sql +import org.testcontainers.spock.Testcontainers +import org.springframework.test.annotation.Rollback +import spock.lang.Shared +import spock.lang.Specification +import java.time.LocalDateTime +import java.time.OffsetDateTime +import java.time.ZoneOffset +import java.time.format.DateTimeFormatter + +/** + * Test specification for network data repository. + */ +@Testcontainers +@DataJpaTest +@Rollback(false) +@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) +class NetworkDataRepositoryImplSpec extends Specification { + + static final String RELOAD_DATA_FOR_SEARCHING = '/data/network-data-changes.sql' + + @Shared + DateTimeFormatter ISO_TIMESTAMP_FORMATTER = DateTimeFormatter.ofPattern('yyyy-MM-dd HH:mm:ss.SSS') + + def queryDataspaceName = 'DATASPACE-01' + @Shared + def querySchemaSetName = 'SCHEMA-SET-01' + @Shared + def queryAnchorName = 'ANCHOR-01' + + @Shared + def observedAscSortOrder = new Sort.Order(Sort.Direction.ASC, 'observed_timestamp') + @Shared + def observedDescSortOrder = new Sort.Order(Sort.Direction.DESC, 'observed_timestamp') + @Shared + def anchorAscSortOrder = new Sort.Order(Sort.Direction.ASC, 'anchor') + + @Autowired + NetworkDataRepository networkDataRepository + + @Shared + TimescaleContainer databaseTestContainer = TimescaleContainer.getInstance() + + @Sql([RELOAD_DATA_FOR_SEARCHING]) + def 'Query: pagination for #scenario'() { + given: 'search criteria' + def searchCriteria = (new SearchCriteria.Builder()) + .dataspaceName(queryDataspaceName) + .anchorName(queryAnchorName) + .pagination(pageNumber, 1) + .build() + when: 'data is fetched' + Slice<NetworkData> result = networkDataRepository.findBySearchCriteria(searchCriteria) + then: 'result has expected values' + result.getNumberOfElements() == 1L + NetworkData networkData = result.getContent().get(0); + networkData.getAnchor() == queryAnchorName + networkData.getDataspace() == queryDataspaceName + networkData.getSchemaSet() == querySchemaSetName + and: ' correct pagination details' + result.hasNext() ? result.nextPageable() : null == expectedNextPageable + result.hasPrevious() ? result.previousPageable() : null == expectedPreviousPageable + where: + scenario | pageNumber || expectedPreviousPageable | expectedNextPageable + 'first Page' | 0 || null | PageRequest.of(1, 1) + 'middle Page' | 1 || PageRequest.of(0, 1) | PageRequest.of(2, 1) + 'last Page' | 2 || PageRequest.of(1, 1) | null + } + + @Sql([RELOAD_DATA_FOR_SEARCHING]) + def 'Query: filter by observed after.'() { + given: 'observed after date' + def observedAfter = getOffsetDateDate('2021-07-22 01:00:01.000') + and: 'search criteria' + def searchCriteria = (new SearchCriteria.Builder()) + .dataspaceName(queryDataspaceName) + .anchorName(queryAnchorName) + .observedAfter(observedAfter) + .pagination(0, 4) + .build() + when: 'data is fetched' + Slice<NetworkData> result = networkDataRepository.findBySearchCriteria(searchCriteria) + then: 'result have expected number of record' + result.getNumberOfElements() == 2L + and: 'each record has observed timestamp on or after the provided value' + for (NetworkData data : result.getContent()) { + assert data.getObservedTimestamp().isAfter(observedAfter) || data.getObservedTimestamp().isEqual(observedAfter) + assert data.getAnchor() == queryAnchorName + assert data.getDataspace() == queryDataspaceName + } + } + + @Sql([RELOAD_DATA_FOR_SEARCHING]) + def 'Query: filter by created before.'() { + given: 'created before date' + def createdBefore = getOffsetDateDate('2021-07-22 23:00:01.000') + and: 'search criteria' + def searchCriteria = (new SearchCriteria.Builder()) + .dataspaceName(queryDataspaceName) + .anchorName(queryAnchorName) + .createdBefore(createdBefore) + .pagination(0, 4) + .build() + when: 'data is fetched' + Slice<NetworkData> result = networkDataRepository.findBySearchCriteria(searchCriteria) + then: 'result have expected number of record' + result.getNumberOfElements() == 2L + and: 'each record has observed timestamp on or after the provided value' + for (NetworkData data : result.getContent()) { + assert data.getCreatedTimestamp().isBefore(createdBefore) || data.getCreatedTimestamp().isEqual(createdBefore) + assert data.getAnchor() == queryAnchorName + assert data.getDataspace() == queryDataspaceName + } + } + + @Sql([RELOAD_DATA_FOR_SEARCHING]) + def 'Query: sort data by #scenario.'() { + given: 'search criteria' + def searchCriteria = (new SearchCriteria.Builder()) + .dataspaceName(queryDataspaceName) + .schemaSetName(querySchemaSetName) + .sort(sortOrder) + .pagination(0, 4) + .build() + when: 'data is fetched' + Slice<NetworkData> result = networkDataRepository.findBySearchCriteria(searchCriteria) + then: 'result has expected values' + result.getNumberOfElements() == 4L + with(result.getContent().get(0)) { + dataspace == queryDataspaceName + schemaSet == querySchemaSetName + anchor == expectedAnchorName + observedTimestamp == getOffsetDateDate(expectedObservedTimestamp) + } + where: + scenario | sortOrder || expectedObservedTimestamp | expectedAnchorName + 'observed timestamp asc' | Sort.by(observedAscSortOrder) || '2021-07-22 00:00:01.000' | 'ANCHOR-01' + 'observed timestamp asc' | Sort.by(observedDescSortOrder) || '2021-07-24 00:00:01.000' | 'ANCHOR-02' + 'anchor asc, ' + + 'observed timestamp desc' | Sort.by(anchorAscSortOrder, observedDescSortOrder) || '2021-07-23 00:00:01.000' | 'ANCHOR-01' + + } + + @Sql([RELOAD_DATA_FOR_SEARCHING]) + def 'Query: filter by payload.'() { + def dataspaceName = 'DATASPACE-02' + given: 'search criteria' + def searchCriteria = (new SearchCriteria.Builder()) + .dataspaceName(dataspaceName) + .schemaSetName(querySchemaSetName) + .simplePayloadFilter(simplePayloadFilter) + .pagination(0, 4) + .build() + when: 'data is fetched' + Slice<NetworkData> result = networkDataRepository.findBySearchCriteria(searchCriteria) + then: 'result has expected values' + result.getNumberOfElements() == expectedRecordsCount + with(result.getContent().get(0)) { + dataspace == dataspaceName + schemaSet == querySchemaSetName + anchor == queryAnchorName + } + where: + simplePayloadFilter || expectedRecordsCount + '{"interfaces": [{"id": "01"}]}' || 2L + '{"interfaces": [{"status": "down"}]}' || 1L + + } + + OffsetDateTime getOffsetDateDate(String dateTimeString) { + def localDateTime = LocalDateTime.parse(dateTimeString, ISO_TIMESTAMP_FORMATTER) + def localZoneOffset = ZoneOffset.systemDefault().getRules().getOffset(localDateTime) + return OffsetDateTime.of(localDateTime, localZoneOffset) + } +} diff --git a/src/test/groovy/org/onap/cps/temporal/repository/NetworkDataRepositorySpec.groovy b/src/test/groovy/org/onap/cps/temporal/repository/NetworkDataRepositorySpec.groovy index 41f3f42..2c7fc5e 100644 --- a/src/test/groovy/org/onap/cps/temporal/repository/NetworkDataRepositorySpec.groovy +++ b/src/test/groovy/org/onap/cps/temporal/repository/NetworkDataRepositorySpec.groovy @@ -13,12 +13,13 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 * ============LICENSE_END========================================================= */ package org.onap.cps.temporal.repository - import org.onap.cps.temporal.domain.NetworkData import org.onap.cps.temporal.repository.containers.TimescaleContainer import org.springframework.beans.factory.annotation.Autowired @@ -29,28 +30,29 @@ import org.springframework.test.annotation.Rollback import org.springframework.test.context.transaction.TestTransaction import spock.lang.Shared import spock.lang.Specification - import java.time.OffsetDateTime /** * Test specification for network data repository. */ @Testcontainers -@DataJpaTest @Rollback(false) +@DataJpaTest +@Rollback(false) @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) class NetworkDataRepositorySpec extends Specification { + @Shared def observedTimestamp = OffsetDateTime.now() - def dataspaceName = 'TEST_DATASPACE' - def schemaSetName = 'TEST_SCHEMA_SET' - def anchorName = 'TEST_ANCHOR' - def payload = '{ "message": "Hello World!" }' + def myDataspaceName = 'MY_DATASPACE' + def mySchemaSetName = 'MY_SCHEMA_SET' + def myAnchorName = 'MY_ANCHOR' + def payload = '{"message": "Hello World!"}' @Autowired NetworkDataRepository networkDataRepository - def networkData = NetworkData.builder().observedTimestamp(observedTimestamp).dataspace(dataspaceName) - .schemaSet(schemaSetName).anchor(anchorName).payload(payload).build() + def networkData = NetworkData.builder().observedTimestamp(observedTimestamp).dataspace(myDataspaceName) + .schemaSet(mySchemaSetName).anchor(myAnchorName).payload(payload).build() @Shared TimescaleContainer databaseTestContainer = TimescaleContainer.getInstance() @@ -71,4 +73,5 @@ class NetworkDataRepositorySpec extends Specification { and: ' the CreationTimestamp is ahead of ObservedTimestamp' savedData.getCreatedTimestamp() > networkData.getObservedTimestamp() } + } diff --git a/src/test/groovy/org/onap/cps/temporal/service/NetworkDataServiceImplSpec.groovy b/src/test/groovy/org/onap/cps/temporal/service/NetworkDataServiceImplSpec.groovy index 9847f54..c55c3c7 100644 --- a/src/test/groovy/org/onap/cps/temporal/service/NetworkDataServiceImplSpec.groovy +++ b/src/test/groovy/org/onap/cps/temporal/service/NetworkDataServiceImplSpec.groovy @@ -13,13 +13,16 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 * ============LICENSE_END========================================================= */ package org.onap.cps.temporal.service import org.onap.cps.temporal.domain.NetworkDataId - +import org.onap.cps.temporal.domain.SearchCriteria +import org.springframework.data.domain.PageImpl import java.time.OffsetDateTime import org.onap.cps.temporal.domain.NetworkData import org.onap.cps.temporal.repository.NetworkDataRepository @@ -50,7 +53,8 @@ class NetworkDataServiceImplSpec extends Specification { } def 'Add network data fails because already added'() { - given: 'network data repository is not able to create data it is asked to persist ' + + given: + 'network data repository is not able to create data it is asked to persist ' + 'and reveals it with null created timestamp on network data entity' def persistedNetworkData = new NetworkData() persistedNetworkData.setCreatedTimestamp(null) @@ -65,4 +69,23 @@ class NetworkDataServiceImplSpec extends Specification { thrown(ServiceException) } + def 'Query network data by search criteria.'() { + given: 'search criteria' + def searchCriteria = SearchCriteria.builder() + .dataspaceName('my-dataspaceName') + .schemaSetName('my-schemaset') + .pagination(0, 10) + .build() + and: 'response from repository' + def pageFromRepository = new PageImpl<>(Collections.emptyList(), searchCriteria.getPageable(), 10) + mockNetworkDataRepository.findBySearchCriteria(searchCriteria) >> pageFromRepository + + when: 'search is executed' + def resultPage = objectUnderTest.searchNetworkData(searchCriteria) + + then: 'data is fetched from repository and returned' + resultPage == pageFromRepository + + } + } diff --git a/src/test/resources/data/network-data-changes.sql b/src/test/resources/data/network-data-changes.sql new file mode 100644 index 0000000..ce15f19 --- /dev/null +++ b/src/test/resources/data/network-data-changes.sql @@ -0,0 +1,32 @@ +--Clear the data before inserting +DELETE FROM NETWORK_DATA WHERE DATASPACE in ( 'DATASPACE-01', 'DATASPACE-02'); +COMMIT; + +-- Test pagination data +-- Test created Before filter +-- Test observed After Filter +INSERT INTO NETWORK_DATA (OBSERVED_TIMESTAMP, DATASPACE, ANCHOR, SCHEMA_SET, PAYLOAD, CREATED_TIMESTAMP) +VALUES +('2021-07-22 00:00:01.000', 'DATASPACE-01', 'ANCHOR-01', 'SCHEMA-SET-01', '{ "status" : "up" }'::jsonb, '2021-07-22 23:00:01.000'), +('2021-07-22 01:00:01.000', 'DATASPACE-01', 'ANCHOR-01', 'SCHEMA-SET-01', '{ "status" : "down" }'::jsonb, '2021-07-22 23:00:01.000'), +('2021-07-23 00:00:01.000', 'DATASPACE-01', 'ANCHOR-01', 'SCHEMA-SET-01', '{ "status" : "up" }'::jsonb, '2021-07-23 23:00:01.000'); + +-- Test sorting on multiple fields +INSERT INTO NETWORK_DATA (OBSERVED_TIMESTAMP, DATASPACE, ANCHOR, SCHEMA_SET, PAYLOAD, CREATED_TIMESTAMP) +VALUES +('2021-07-24 00:00:01.000', 'DATASPACE-01', 'ANCHOR-02', 'SCHEMA-SET-01', '{ "status" : "up" }'::jsonb, '2021-07-24 23:00:01.000'); + + +-- Test simple payload filter on multiple field +INSERT INTO NETWORK_DATA (OBSERVED_TIMESTAMP, DATASPACE, ANCHOR, SCHEMA_SET, PAYLOAD, CREATED_TIMESTAMP) +VALUES +('2021-07-24 00:00:01.000', 'DATASPACE-02', 'ANCHOR-01', 'SCHEMA-SET-01', '{ "interfaces": [ { "id" : "01", "status" : "up" } ]}'::jsonb, '2021-07-24 01:00:01.000'), +('2021-07-24 01:00:01.000', 'DATASPACE-02', 'ANCHOR-01', 'SCHEMA-SET-01', '{ "interfaces": [ { "id" : "01", "status" : "down" } ]}'::jsonb, '2021-07-24 02:00:01.000'), +('2021-07-24 02:00:01.000', 'DATASPACE-02', 'ANCHOR-01', 'SCHEMA-SET-01', '{ "interfaces": [ { "id" : "02", "status" : "up" } ]}'::jsonb, '2021-07-24 03:00:01.000'), +('2021-07-24 03:00:01.000', 'DATASPACE-02', 'ANCHOR-01', 'SCHEMA-SET-01', '{ "interfaces": [ { "id" : "03", "status" : "up" } ]}'::jsonb, '2021-07-24 04:00:01.000'); + +COMMIT; + + + + |