Merge "CM SUBSCRIPTION: add new subscription for non existing xpath"
[cps.git] / cps-service / src / test / groovy / org / onap / cps / aop / CpsLoggingAspectServiceSpec.groovy
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 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.aop
22
23 import org.aspectj.lang.ProceedingJoinPoint
24 import org.aspectj.lang.reflect.MethodSignature
25 import spock.lang.Specification
26 import java.util.logging.Level
27 import java.util.logging.Logger
28
29 class CpsLoggingAspectServiceSpec extends Specification {
30
31     private static final Logger logger = Logger.getLogger('org.onap.cps')
32
33     def mockProceedingJoinPoint = Mock(ProceedingJoinPoint)
34     def mockMethodSignature = Mock(MethodSignature)
35     def objectUnderTest = Spy(CpsLoggingAspectService)
36
37     def setup() {
38         mockMethodSignature.getDeclaringType() >> this.getClass()
39         mockProceedingJoinPoint.getSignature() >> mockMethodSignature
40     }
41
42     def 'Log method execution time for log level : #logLevel.'() {
43         given: 'normal method and log level of #logLevel'
44             mockMethodSignature.getName() >> 'some method'
45             logger.setLevel(logLevel)
46         when: 'cps method is intercepted'
47             objectUnderTest.interceptMethodCall(mockProceedingJoinPoint)
48         then: 'logging is only done for correct levels'
49             expectedNumberOfMethodExecution * objectUnderTest.logMethodCall(*_)
50         where: 'the following log levels are used'
51             logLevel     || expectedNumberOfMethodExecution
52             Level.INFO   || 0
53             Level.FINE   || 1
54             Level.FINEST || 1
55     }
56
57     def 'Exception thrown during method execution.'() {
58         given: 'some exception is thrown'
59             def originalException = new Exception('some exception')
60             mockProceedingJoinPoint.proceed() >> {
61                 throw originalException
62             }
63         when: 'cps method is intercepted'
64             objectUnderTest.interceptMethodCall(mockProceedingJoinPoint)
65         then: 'the same exception is still thrown'
66             def thrownException = thrown(Exception)
67             assert thrownException == originalException
68     }
69
70     def 'Masking sensitive data.'() {
71         given: 'method named #methodName returns some value'
72             mockMethodSignature.getName() >> methodName
73             mockProceedingJoinPoint.proceed() >> 'original return value'
74         and: 'the logger level is set to FINE'
75             logger.setLevel(Level.FINE)
76         when: 'cps method is intercepted'
77            objectUnderTest.interceptMethodCall(mockProceedingJoinPoint)
78         then: 'the expected value is being logged'
79             1 * objectUnderTest.logMethodCall(_, _, _, expectedLogValue)
80         where: 'the following method names are used'
81             methodName        || expectedLogValue
82             'normalMethod'    || 'original return value'
83             'getAuthPassword' || '***********'
84     }
85
86 }