Reverting to spring-boot 1.5
[aai/sparky-be.git] / sparkybe-onap-service / src / test / java / org / onap / aai / sparky / util / ErrorUtilTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2019 Nokia Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *       http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.aai.sparky.util;
21
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.mockito.Mock;
25 import org.mockito.runners.MockitoJUnitRunner;
26
27 import static org.junit.Assert.assertEquals;
28 import static org.mockito.Mockito.when;
29
30 @RunWith(MockitoJUnitRunner.class)
31 public class ErrorUtilTest {
32
33     private StackTraceElement[] stackTraceElements = new StackTraceElement[]{
34             new StackTraceElement("TestClass", "methodA", "a", 2),
35             new StackTraceElement("TestClass", "methodB", "b", 3)
36     };
37
38     @Mock
39     Exception exception;
40
41     @Test
42     public void shouldReturnEmptyStringWhenStackTraceArrayIsEmpty() {
43         assertEquals("", ErrorUtil.extractStackTraceElements(1, exception));
44     }
45
46     @Test
47     public void shouldReturnNarrowedStackTraceElementsAsFormattedStringToPassedMaxNumberOfElements() {
48         // given
49         when(exception.getStackTrace()).thenReturn(stackTraceElements);
50         // when
51         final String actual = ErrorUtil.extractStackTraceElements(1, exception);
52         // then
53         assertEquals("TestClass.methodA(a:2)\n", actual);
54     }
55
56     @Test
57     public void shouldReturnFullStackTraceElementsAsFormattedStringWhenMaxNumberOfElementsIsOutOfRange() {
58         // given
59         when(exception.getStackTrace()).thenReturn(stackTraceElements);
60         // when
61         final String actual = ErrorUtil.extractStackTraceElements(5, exception);
62         // then
63         assertEquals("TestClass.methodA(a:2)\nTestClass.methodB(b:3)\n", actual);
64     }
65
66 }