2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.aai.sparky.util;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.mockito.Mock;
25 import org.mockito.runners.MockitoJUnitRunner;
27 import static org.junit.Assert.assertEquals;
28 import static org.mockito.Mockito.when;
30 @RunWith(MockitoJUnitRunner.class)
31 public class ErrorUtilTest {
33 private StackTraceElement[] stackTraceElements = new StackTraceElement[]{
34 new StackTraceElement("TestClass", "methodA", "a", 2),
35 new StackTraceElement("TestClass", "methodB", "b", 3)
42 public void shouldReturnEmptyStringWhenStackTraceArrayIsEmpty() {
43 assertEquals("", ErrorUtil.extractStackTraceElements(1, exception));
47 public void shouldReturnNarrowedStackTraceElementsAsFormattedStringToPassedMaxNumberOfElements() {
49 when(exception.getStackTrace()).thenReturn(stackTraceElements);
51 final String actual = ErrorUtil.extractStackTraceElements(1, exception);
53 assertEquals("TestClass.methodA(a:2)\n", actual);
57 public void shouldReturnFullStackTraceElementsAsFormattedStringWhenMaxNumberOfElementsIsOutOfRange() {
59 when(exception.getStackTrace()).thenReturn(stackTraceElements);
61 final String actual = ErrorUtil.extractStackTraceElements(5, exception);
63 assertEquals("TestClass.methodA(a:2)\nTestClass.methodB(b:3)\n", actual);