From c2b5e8523d874302d0f3c7f042d23bacf6d856a3 Mon Sep 17 00:00:00 2001 From: Renu Kumari Date: Wed, 28 Jul 2021 00:07:41 -0400 Subject: Fetch data filtered by Search criteria Implemented Service and repository layer Issue-ID: CPS-375 Signed-off-by: Renu Kumari Change-Id: Iedac0e48b2391a60f3eb9ab710ccdff0c9185407 --- .../cps/temporal/domain/SearchCriteriaSpec.groovy | 132 +++++++++++++ .../NetworkDataRepositoryImplSpec.groovy | 206 +++++++++++++++++++++ .../repository/NetworkDataRepositorySpec.groovy | 21 ++- .../service/NetworkDataServiceImplSpec.groovy | 27 ++- 4 files changed, 375 insertions(+), 11 deletions(-) create mode 100644 src/test/groovy/org/onap/cps/temporal/domain/SearchCriteriaSpec.groovy create mode 100644 src/test/groovy/org/onap/cps/temporal/repository/NetworkDataRepositoryImplSpec.groovy (limited to 'src/test/groovy') 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 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 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 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 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 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 + + } + } -- cgit 1.2.3-korg