From d31d8e1cc167abf7835a1771b5ddc8d78aba9ac6 Mon Sep 17 00:00:00 2001 From: "puthuparambil.aditya" Date: Thu, 6 May 2021 16:12:44 +0100 Subject: Implement service and repository layers for storing temporal data 1. Basic structure created 2. Basic tests added 3. lombok.config included for coverage Issue-ID: CPS-194 Signed-off-by: puthuparambil.aditya Change-Id: Icf23c2e647106f7985dff14d9901806f7c4aa55a --- .../controller/QuerryControllerSpec.groovy | 37 ------------ .../temporal/controller/QueryControllerSpec.groovy | 37 ++++++++++++ .../repository/NetworkDataRepositorySpec.groovy | 70 ++++++++++++++++++++++ .../service/NetworkDataServiceImplSpec.groovy | 45 ++++++++++++++ 4 files changed, 152 insertions(+), 37 deletions(-) delete mode 100644 src/test/groovy/org/onap/cps/temporal/controller/QuerryControllerSpec.groovy create mode 100644 src/test/groovy/org/onap/cps/temporal/controller/QueryControllerSpec.groovy create mode 100644 src/test/groovy/org/onap/cps/temporal/repository/NetworkDataRepositorySpec.groovy create mode 100644 src/test/groovy/org/onap/cps/temporal/service/NetworkDataServiceImplSpec.groovy (limited to 'src/test/groovy') diff --git a/src/test/groovy/org/onap/cps/temporal/controller/QuerryControllerSpec.groovy b/src/test/groovy/org/onap/cps/temporal/controller/QuerryControllerSpec.groovy deleted file mode 100644 index f718bf4..0000000 --- a/src/test/groovy/org/onap/cps/temporal/controller/QuerryControllerSpec.groovy +++ /dev/null @@ -1,37 +0,0 @@ -/* - * ============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. - * ============LICENSE_END========================================================= - */ - -package org.onap.cps.temporal.controller - -import spock.lang.Specification - -/** - * Specification for Query Controller. - */ -class QueryControllerSpec extends Specification { - - def objectUnderTest = new QueryController() - - def 'Get home returns some data'() { - when: 'get home is invoked' - def response = objectUnderTest.home() - then: 'a response is returned' - ! response.empty - } - -} \ No newline at end of file diff --git a/src/test/groovy/org/onap/cps/temporal/controller/QueryControllerSpec.groovy b/src/test/groovy/org/onap/cps/temporal/controller/QueryControllerSpec.groovy new file mode 100644 index 0000000..f718bf4 --- /dev/null +++ b/src/test/groovy/org/onap/cps/temporal/controller/QueryControllerSpec.groovy @@ -0,0 +1,37 @@ +/* + * ============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. + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.temporal.controller + +import spock.lang.Specification + +/** + * Specification for Query Controller. + */ +class QueryControllerSpec extends Specification { + + def objectUnderTest = new QueryController() + + def 'Get home returns some data'() { + when: 'get home is invoked' + def response = objectUnderTest.home() + then: 'a response is returned' + ! response.empty + } + +} \ No newline at end of file diff --git a/src/test/groovy/org/onap/cps/temporal/repository/NetworkDataRepositorySpec.groovy b/src/test/groovy/org/onap/cps/temporal/repository/NetworkDataRepositorySpec.groovy new file mode 100644 index 0000000..ec976ee --- /dev/null +++ b/src/test/groovy/org/onap/cps/temporal/repository/NetworkDataRepositorySpec.groovy @@ -0,0 +1,70 @@ +/* + * ============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. + * ============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 +import org.springframework.boot.test.context.SpringBootTest +import org.testcontainers.junit.jupiter.Testcontainers +import spock.lang.Shared +import spock.lang.Specification + +import java.time.OffsetDateTime + +@SpringBootTest +@Testcontainers +class NetworkDataRepositorySpec extends Specification { + + def observedTimestamp = OffsetDateTime.now() + def dataspaceName = 'TEST_DATASPACE' + def schemaSetName = 'TEST_SCHEMA_SET' + def anchorName = 'TEST_ANCHOR' + def payload = '{ \"message\": \"Hello World!\" }' + + @Autowired + NetworkDataRepository networkDataRepository + + def networkData = NetworkData.builder().observedTimestamp(observedTimestamp).dataspace(dataspaceName) + .schemaSet(schemaSetName).anchor(anchorName).payload(payload).build() + + @Shared + def databaseTestContainer = TimescaleContainer.getInstance() + + def setupSpec() { + databaseTestContainer.start() + } + + def 'Store latest network data in timeseries database.'() { + when: 'a new Network Data is stored' + NetworkData savedData = networkDataRepository.save(networkData) + then: ' the saved Network Data is returned' + savedData.getDataspace() == networkData.getDataspace() + savedData.getSchemaSet() == networkData.getSchemaSet() + savedData.getAnchor() == networkData.getAnchor() + savedData.getPayload() == networkData.getPayload() + savedData.getObservedTimestamp() == networkData.getObservedTimestamp() + and: ' createdTimestamp is auto populated by db ' + networkData.getCreatedTimestamp() == null + savedData.getCreatedTimestamp() != null + 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 new file mode 100644 index 0000000..70ac2bc --- /dev/null +++ b/src/test/groovy/org/onap/cps/temporal/service/NetworkDataServiceImplSpec.groovy @@ -0,0 +1,45 @@ +/* + * ============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. + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.temporal.service + +import java.time.OffsetDateTime +import org.onap.cps.temporal.domain.NetworkData +import org.onap.cps.temporal.repository.NetworkDataRepository +import spock.lang.Specification + +class NetworkDataServiceImplSpec extends Specification { + + def objectUnderTest = new NetworkDataServiceImpl() + + def mockNetworkDataRepository = Mock(NetworkDataRepository) + + def networkData = new NetworkData() + + def setup() { + objectUnderTest.networkDataRepository = mockNetworkDataRepository + } + + def 'Add network data in timeseries database.'() { + when: 'a new network data is added' + objectUnderTest.addNetworkData(networkData) + then: ' repository service is called with the correct parameters' + 1 * mockNetworkDataRepository.save(networkData) + } + +} -- cgit 1.2.3-korg