package com.axibase.tsd.client;

import com.axibase.tsd.util.AtsdUtil;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;

import java.util.Arrays;
import java.util.Collection;

import static org.junit.Assert.assertEquals;

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(Parameterized.class)
@PrepareForTest( {AtsdUtil.class})
public class HttpUtilsTest {

    @Parameter
    public String clientName;
    @Parameter(1)
    public String version;
    @Parameter(2)
    public String expectedUserAgent;

    @Parameters(name = "Compiling of UserAgent header for version {0} and client name {1} should return {2}")
    public static Collection<Object[]> params() {
        return Arrays.asList(
                new Object[][] {
                        {"axibase-collector/19768", "1.0.6", "axibase-collector/19768 atsd-api-java/1.0.6"},
                        {null, "1.0.6", "atsd-api-java/1.0.6"},
                        {"axibase-collector/19768", null, "axibase-collector/19768 atsd-api-java"},
                        {null, null, "atsd-api-java"}
                }
        );
    }

    @Before
    public void init() {
        PowerMockito.mockStatic(AtsdUtil.class);
        PowerMockito.when(AtsdUtil.getVersion()).thenReturn(version);
    }

    @Test
    public void compileUserAgent() {

        final String assertMessage = String.format("Incorrect compiled user-agent name for version: %s and client name: %s",
                version, clientName);
        assertEquals(assertMessage, expectedUserAgent, HttpUtils.compileUserAgent(clientName));
    }
}
@@ -569,16 +569,75 @@
 ction;%0A%0A
+import static org.apache.commons.lang3.ArrayUtils.toArray;%0A
 import s
@@ -673,16 +673,16 @@
 Equals;%0A
-
 %0A@RunWit
@@ -1173,24 +1173,16 @@
     
-new Object%5B%5D%5B%5D %7B
+toArray(
 %0A   
@@ -1194,33 +1194,41 @@
                 
-%7B
+testCase(
 %22axibase-collect
@@ -1284,33 +1284,33 @@
 -api-java/1.0.6%22
-%7D
+)
 ,%0A              
@@ -1315,25 +1315,33 @@
             
-%7B
+testCase(
 null, %221.0.6
@@ -1364,17 +1364,17 @@
 a/1.0.6%22
-%7D
+)
 ,%0A      
@@ -1391,17 +1391,25 @@
         
-%7B
+testCase(
 %22axibase
@@ -1472,17 +1472,17 @@
 pi-java%22
-%7D
+)
 ,%0A      
@@ -1499,17 +1499,25 @@
         
-%7B
+testCase(
 null, nu
@@ -1535,17 +1535,17 @@
 pi-java%22
-%7D
+)
 %0A       
@@ -1553,17 +1553,17 @@
         
-%7D
+)
 %0A       
@@ -1947,16 +1947,16 @@
 tName);%0A
-
         
@@ -2048,10 +2048,106 @@
 );%0A    %7D
+%0A%0A    private static Object%5B%5D testCase(Object... params) %7B%0A        return toArray(params);%0A    %7D
 %0A%7D

test refactoring