package org.automon.utils;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.reflect.CodeSignature;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.sql.SQLException;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class UtilsTest {

    @Before
    public void setUp() throws Exception {

    }

    @After
    public void tearDown() throws Exception {

    }

    @Test
    public void testBasicThrowableLabel() throws Exception {
        RuntimeException runtimeException = new RuntimeException("My RTE exception");
        assertThat(Utils.getLabel(runtimeException)).isEqualTo("java.lang.RuntimeException");
    }

    @Test
    public void testSqlThrowableLabel() throws Exception {
        SQLException sqlException = new SQLException("My SQL exception", "Login failure", 400);
        assertThat(Utils.getLabel(sqlException)).isEqualTo("java.sql.SQLException,ErrorCode=400,SQLState=Login failure");
    }

    @Test
    public void testArgNameValuePairs_Empty() throws Exception {
        JoinPoint jp = mock(JoinPoint.class);
        assertThat(Utils.getArgNameValuePairs(jp)).isEmpty();
    }

    @Test
    public void testArgNameValuePairs_ArgValueNoArgName() throws Exception {
        JoinPoint jp = mock(JoinPoint.class);
        when(jp.getArgs()).thenReturn(new Object[]{"Steve"});
        assertThat(Utils.getArgNameValuePairs(jp)).containsExactly("0: Steve");
    }

    @Test
    public void testArgNameValuePairs_ArgValueNoArgName_multiple() throws Exception {
        JoinPoint jp = mock(JoinPoint.class);
        when(jp.getArgs()).thenReturn(new Object[]{"Steve", 20});
        assertThat(Utils.getArgNameValuePairs(jp)).containsExactly("0: Steve",  "1: 20");
    }

    @Test
    public void testArgNameValuePairs_ArgValueAndArgName() throws Exception {
        JoinPoint jp = mock(JoinPoint.class);
        CodeSignature signature = mock(CodeSignature.class);
        when(jp.getSignature()).thenReturn(signature);
        when(signature.getParameterNames()).thenReturn(new String[]{"firstName"});
        when(jp.getArgs()).thenReturn(new Object[]{"Steve"});
        assertThat(Utils.getArgNameValuePairs(jp)).containsExactly("firstName: Steve");
    }

    @Test
    public void testArgNameValuePairs_ArgValueAndArgName_multiple() throws Exception {
        JoinPoint jp = mock(JoinPoint.class);
        CodeSignature signature = mock(CodeSignature.class);
        when(jp.getSignature()).thenReturn(signature);
        when(signature.getParameterNames()).thenReturn(new String[]{"firstName", "number"});
        when(jp.getArgs()).thenReturn(new Object[]{"Steve", 20});
        assertThat(Utils.getArgNameValuePairs(jp)).containsExactly("firstName: Steve", "number: 20");
    }
}
@@ -205,16 +205,67 @@
 ception;
+%0Aimport java.util.ArrayList;%0Aimport java.util.List;
 %0A%0Aimport
@@ -2896,9 +2896,1579 @@
 ;%0A    %7D%0A
+%0A    @Test%0A    public void testArgNameValuePairsToString() throws Exception %7B%0A        List%3CString%3E args = new ArrayList%3CString%3E();%0A        assertThat(Utils.argNameValuePairsToString(null)).isEqualTo(Utils.UNKNOWN);%0A        assertThat(Utils.argNameValuePairsToString(args)).contains(%22Parameter%22);%0A        args.add(%22fname: Steve%22);%0A        args.add(%22lname: Souza%22);%0A        assertThat(Utils.argNameValuePairsToString(args)).contains(%22Parameter%22, %22fname: Steve%22, %22lname: Souza%22);%0A    %7D%0A%0A    @Test%0A    public void testToStringWithLimit() throws Exception %7B%0A        assertThat(Utils.toStringWithLimit(null)).isEqualTo(Utils.NULL_STR);%0A        assertThat(Utils.toStringWithLimit(%22hi%22)).describedAs(%22Normal length string%22).isEqualTo(%22hi%22);%0A        StringBuilder sb = new StringBuilder();%0A        int SIZE=1000;%0A        for (int i=0;i%3CSIZE;i++) %7B%0A            sb.append(%22A%22);%0A        %7D%0A        assertThat(Utils.toStringWithLimit(sb.toString()).length()).describedAs(%22Long strings should be truncated%22).isLessThan(SIZE);%0A        assertThat(Utils.toStringWithLimit(sb.toString())).describedAs(%22String too long should be truncated%22).endsWith(Utils.DEFAULT_MAX_STRING_ENDING);%0A    %7D%0A%0A    @Test%0A    public void testGetExceptionTrace() throws Exception %7B%0A        assertThat(Utils.getExceptionTrace(null)).isEqualTo(Utils.UNKNOWN);%0A        RuntimeException e = new RuntimeException(%22my exception%22);%0A        assertThat(Utils.getExceptionTrace(e)).contains(%22java.lang.RuntimeException: my exception%22);%0A        assertThat(Utils.getExceptionTrace(e)).contains(getClass().getName());%0A    %7D%0A%0A
 %7D

test - added tests for getting string version of stacktrace and parameters