/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package eu.dnetlib.espas.pep;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Arrays;

import junit.framework.TestCase;

import org.apache.log4j.Logger;

/**
 *
 * @author gathanas
 */
public class AuthenticationPEPTest extends TestCase {
    
    public AuthenticationPEPTest(String testName) {
        super(testName);
    }
    
    @Override
    protected void setUp() throws Exception {
        super.setUp();
    }
//
//    public void testIsPermitedRequest() {
//        try {
//            System.out.println("isPermitedRequest");
//            String requestedOb = "http://resources.espas-fp7.eu/observation/noa/mapFoF2_201303260715_201303260815/1";
//            String userId = "stefania.martziou@gmail.com";
//            String actionName = "access";
//            AuthenticationPEP instance = new AuthenticationPEP();
//            boolean expResult = false;
//            boolean result = instance.isPermitedRequest(requestedOb, userId, actionName, null);
//            assertEquals(expResult, result);
//        } catch (Exception ex) {
//            Logger.getLogger(AuthenticationPEP.class).error(null, ex);
//            assert true;
//        }
//    }
//    
//    public void testIsPermited() {
//        try {
//            System.out.println("isPermited");
//            InputStream contentStream  = ClassLoader.getSystemResourceAsStream("Test-Request.xml");
//            ByteArrayOutputStream outWriter = new ByteArrayOutputStream();
//            boolean flag=true;
//            do{
//                byte[] bytes = new byte[64];
//                int i = contentStream.read(bytes);
//                if(i>0)
//                    outWriter.write(Arrays.copyOf(bytes, i));
//                else
//                    flag=false;
//            }while(flag);
//            outWriter.flush();
//            Logger.getLogger(AuthenticationPEP.class).info("Request Content is:"+new String(outWriter.toByteArray()));
//            AuthenticationPEP instance = new AuthenticationPEP();
//            boolean expResult = false;
//            boolean result = instance.isPermitedRequest(new String(outWriter.toByteArray()));
//            assertEquals(expResult, result);
//        } catch (Exception ex) {
//            Logger.getLogger(AuthenticationPEP.class).error(null, ex);
//            assert true;
//        }
//    }

    public void testGetPDPResults() throws Exception {
        System.out.println("getPDPResults");
        String response = "                     <Response xmlns=\"urn:oasis:names:tc:xacml:3.0:core:schema:wd-17\">" +
"<Result>" +
"<Decision>Permit</Decision>" +
"<Status>" +
"<StatusCode Value=\"urn:oasis:names:tc:xacml:1.0:status:ok\"/>" +
"</Status>" +
"<AssociatedAdvice>" +
"<Advice AdviceId=\"RuleName\" >" +
"<AttributeAssignment  AttributeId=\"responseMsg\" DataType=\"http://www.w3.org/2001/XMLSchema#string\">Enabled By Rule EnableWithResponse</AttributeAssignment>" +
"</Advice>" +
"</AssociatedAdvice>" +
"</Result>" +
"</Response>";
        AuthenticationPEP instance = new AuthenticationPEP();
        String[] expResult = new String[]{"Permit","Enabled By Rule EnableWithResponse"};
        String[] result = instance.getPDPResults(response);
        assertEquals(expResult[0], result[0]);
        
    }
    
}
