2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Nordix Foundation
4 * Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.api.impl
24 import org.onap.cps.spi.DataPersistenceService
25 import spock.lang.Specification
27 class CpServiceImplSpec extends Specification {
29 def mockDataPersistenceService = Mock(DataPersistenceService)
30 def objectUnderTest = new CpServiceImpl()
33 objectUnderTest.dataPersistenceService = mockDataPersistenceService
36 def 'Cps Service provides to its client the id assigned by the system when storing a data structure'() {
37 given: 'that data persistence service is giving id 123 to a data structure it is asked to store'
38 mockDataPersistenceService.storeJsonStructure(_) >> 123
39 expect: 'Cps service returns the same id when storing data structure'
40 objectUnderTest.storeJsonStructure('') == 123
43 def 'Read a JSON object with a valid identifier'(){
44 given: 'that the data persistence service returns a JSON structure for identifier 1'
45 mockDataPersistenceService.getJsonById(1) >> '{name : hello}'
46 expect: 'that the same JSON structure is returned by CPS'
47 objectUnderTest.getJsonById(1) == '{name : hello}'
50 def 'Read a JSON object with an identifier that does not exist'(){
51 given: 'that the data persistence service throws an exception'
52 def exceptionThrownByPersistenceService = new IllegalStateException()
53 mockDataPersistenceService.getJsonById(_) >> {throw exceptionThrownByPersistenceService}
54 when: 'we try to get the JSON structure'
55 objectUnderTest.getJsonById(1);
56 then: 'the same exception is thrown by CPS'
57 thrown(IllegalStateException)
60 def 'Delete a JSON object with a valid identifier'(){
61 given: 'that the data persistence service can delete a JSON structure for identifier 1'
62 mockDataPersistenceService.deleteJsonById(1)
63 expect: 'No exception is thrown when we delete a JSON structure with identifier 1'
64 objectUnderTest.deleteJsonById(1)
67 def 'Delete a JSON object with an identifier that does not exist'(){
68 given: 'that the data persistence service throws an exception'
69 mockDataPersistenceService.deleteJsonById(_) >> {throw new IllegalStateException()}
70 when: 'we try to delete a JSON structure'
71 objectUnderTest.deleteJsonById(100);
72 then: 'the same exception is thrown by CPS'
73 thrown(IllegalStateException)