/*
 * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.wso2.carbon.identity.oauth.dcr.util;

import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.wso2.carbon.identity.oauth.dcr.DCRMConstants;
import org.wso2.carbon.identity.oauth.dcr.exception.DCRMClientException;
import org.wso2.carbon.identity.oauth.dcr.exception.DCRMServerException;
import org.wso2.carbon.identity.testutil.IdentityBaseTest;

import static org.testng.Assert.fail;

public class DCRMUtilsTest extends IdentityBaseTest{

    @DataProvider(name = "BuildRedirectUrl")
    public Object[][] buildRedirectUrl() {
        return new Object[][] {
                {"http://example.com/", true},
                {null, false},
                {"", false},
        };
    }

    @Test(dataProvider = "BuildRedirectUrl")
    public void testIsRedirectionUriValid(String url, boolean response) throws Exception {
        Assert.assertEquals(DCRMUtils.isRedirectionUriValid(url), response);
    }

    @DataProvider(name = "BuildServerException")
    public Object[][] buildServerException() {
        return new Object[][] {
                {DCRMConstants.ErrorMessages.BAD_REQUEST_INVALID_INPUT,""},
                {DCRMConstants.ErrorMessages.BAD_REQUEST_INVALID_INPUT,"error from bad request"}
        };
    }

    @Test(dataProvider = "BuildServerException")
    public void testGenerateServerException(DCRMConstants.ErrorMessages error, String data) throws Exception {
        Throwable e = new Throwable();
        try {
            DCRMUtils.generateServerException(error,data,e);
        } catch (DCRMServerException ex) {
            Assert.assertEquals(ex.getMessage(), data);
        }

    }

    @Test(dataProvider = "BuildServerException")
    public void testGenerateServerException1(DCRMConstants.ErrorMessages error, String data) throws Exception {
        try {
            DCRMUtils.generateServerException(error,data);
        } catch (DCRMServerException e){
            Assert.assertEquals(e.getMessage(),data);
        }

    }

    @Test(dataProvider = "BuildServerException")
    public void testGenerateClientException(DCRMConstants.ErrorMessages error, String data) throws Exception {
        Throwable e = new Throwable();
        try {
            DCRMUtils.generateClientException(error,data,e);
        } catch (DCRMClientException ex) {
            Assert.assertEquals(ex.getMessage(),data);
        }
    }

    @Test(dataProvider = "BuildServerException")
    public void testGenerateClientException1(DCRMConstants.ErrorMessages error, String data) throws Exception {
        try {
            DCRMUtils.generateClientException(error,data);
        } catch (DCRMClientException e){
            Assert.assertEquals(e.getMessage(),data);
        }

    }
}

@@ -1839,16 +1839,17 @@
 D_INPUT,
+ 
 %22%22%7D,%0A   
@@ -1916,16 +1916,17 @@
 D_INPUT,
+ 
 %22error f
@@ -2000,32 +2000,80 @@
 ServerException%22
+, expectedExceptions = DCRMServerException.class
 )%0A    public voi
@@ -2070,39 +2070,40 @@
 public void test
-Generat
+Throwabl
 eServerException
@@ -2218,32 +2218,20 @@
        t
-ry %7B%0A           
+hrow
  DCRMUti
@@ -2259,37 +2259,39 @@
 ption(error,
+ 
 data,
+ 
 e);%0A    
     %7D catch 
@@ -2278,33 +2278,50 @@
 e);%0A    
+%7D%0A%0A
     
-%7D catch (DCRM
+@Test(dataProvider = %22Build
 ServerEx
@@ -2331,120 +2331,36 @@
 tion
+%22,
  ex
-) %7B%0A            Assert.assertEquals(ex.getMessage(), data);%0A        %7D%0A%0A    %7D%0A%0A    @Test(dataProvider = %22Build
+pectedExceptions = DCRM
 Serv
@@ -2362,33 +2362,38 @@
 MServerException
-%22
+.class
 )%0A    public voi
@@ -2413,33 +2413,32 @@
 eServerException
-1
 (DCRMConstants.E
@@ -2498,32 +2498,20 @@
        t
-ry %7B%0A           
+hrow
  DCRMUti
@@ -2547,103 +2547,9 @@
 ror,
-data);%0A        %7D catch (DCRMServerException e)%7B%0A            Assert.assertEquals(e.getMessage(),
+ 
 data
@@ -2555,28 +2555,19 @@
 a);%0A    
-  
-  
 %7D%0A%0A
-    %7D
 %0A%0A    @T
@@ -2599,32 +2599,80 @@
 ServerException%22
+, expectedExceptions = DCRMClientException.class
 )%0A    public voi
@@ -2669,39 +2669,40 @@
 public void test
-Generat
+Throwabl
 eClientException
@@ -2817,32 +2817,20 @@
        t
-ry %7B%0A           
+hrow
  DCRMUti
@@ -2866,124 +2866,19 @@
 ror,
+ 
 data,
+ 
 e);%0A
-        %7D catch (DCRMClientException ex) %7B%0A            Assert.assertEquals(ex.getMessage(),data);%0A        %7D
 %0A   
@@ -2920,32 +2920,80 @@
 ServerException%22
+, expectedExceptions = DCRMClientException.class
 )%0A    public voi
@@ -3021,17 +3021,16 @@
 xception
-1
 (DCRMCon
@@ -3102,24 +3102,12 @@
    t
-ry %7B%0A           
+hrow
  DCR
@@ -3147,119 +3147,15 @@
 ror,
+ 
 data);
-%0A        %7D catch (DCRMClientException e)%7B%0A            Assert.assertEquals(e.getMessage(),data);%0A        %7D
 %0A%0A  

Improve DCRMUtils unit test coverage.