DMI Data AVC to use kafka headers
[cps.git] / integration-test / src / test / groovy / org / onap / cps / integration / performance / cps / CpsModuleServicePerfTest.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2022-2023 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.integration.performance.cps
22
23 import org.onap.cps.api.CpsModuleService
24 import org.onap.cps.integration.performance.base.CpsPerfTestBase
25 import org.onap.cps.spi.model.ModuleReference
26 import org.springframework.util.StopWatch
27
28 import java.util.concurrent.ThreadLocalRandom
29
30 class CpsModuleServicePerfTest extends CpsPerfTestBase {
31
32     def NEW_RESOURCE_CONTENT = 'module stores {\n' +
33         '    yang-version 1.1;\n' +
34         '    namespace "org:onap:ccsdk:sample";\n' +
35         '\n' +
36         '    prefix book-store;\n' +
37         '\n' +
38         '    revision "2020-09-15" {\n' +
39         '        description\n' +
40         '        "Sample Model";\n' +
41         '    }' +
42         '}'
43
44     CpsModuleService objectUnderTest
45
46     def setup() { objectUnderTest = cpsModuleService }
47
48     def 'Store new schema set with many modules'() {
49         when: 'a new schema set with 200 modules is stored'
50             def newYangResourcesNameToContentMap = [:]
51             (1..200).each {
52                 def year = 2000 + it
53                 def resourceName = "module${it}".toString()
54                 def moduleName = "stores${it}"
55                 def content = NEW_RESOURCE_CONTENT.replace('2020',String.valueOf(year)).replace('stores',moduleName)
56                 newYangResourcesNameToContentMap.put(resourceName, content)
57             }
58             objectUnderTest.createSchemaSet(CPS_PERFORMANCE_TEST_DATASPACE, 'perfSchemaSet', newYangResourcesNameToContentMap)
59         then: 'the schema set is persisted correctly'
60             def result =  cpsModuleService.getSchemaSet(CPS_PERFORMANCE_TEST_DATASPACE, 'perfSchemaSet')
61             result.moduleReferences.size() == 200
62         and: 'identification of new module resources is fast enough (1,000 executions less then 6,000 milliseconds)'
63             def stopWatch = new StopWatch()
64             1000.times() {
65                 def moduleReferencesToCheck = createModuleReferencesWithRandomMatchingExistingModuleReferences()
66                 stopWatch.start()
67                 def newModuleReferences = objectUnderTest.identifyNewModuleReferences(moduleReferencesToCheck)
68                 stopWatch.stop()
69                 assert newModuleReferences.size() > 0 && newModuleReferences.size() < 300
70             }
71             assert stopWatch.getTotalTimeMillis() < 6000
72     }
73
74     def createModuleReferencesWithRandomMatchingExistingModuleReferences() {
75         def moduleReferences = []
76         (1..250).each {
77             def randomNumber = ThreadLocalRandom.current().nextInt(1, 300)
78             def year = 2000 + randomNumber
79             def moduleName = "stores${randomNumber}"
80             moduleReferences.add(new ModuleReference(moduleName, "${year}-09-15"))
81         }
82         return moduleReferences
83     }
84
85 }