package pdp.web;

import org.apache.commons.io.IOUtils;
import org.apache.openaz.xacml.api.Decision;
import org.apache.openaz.xacml.api.Response;
import org.apache.openaz.xacml.std.json.JSONResponse;
import org.apache.openaz.xacml.std.json.JSONStructureException;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.WebIntegrationTest;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.client.RestTemplate;
import pdp.AbstractPdpIntegrationTest;
import pdp.PdpApplication;
import pdp.domain.JsonPolicyRequest;
import pdp.domain.PdpPolicy;
import pdp.domain.PdpPolicyDefinition;
import pdp.policies.PolicyLoader;
import pdp.util.StreamUtils;

import java.io.IOException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static pdp.access.FederatedUserBuilder.*;
import static pdp.util.StreamUtils.singletonCollector;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = PdpApplication.class)
@WebIntegrationTest(randomPort = true, value = { "spring.profiles.active=dev"})
public class PdpApiControllerIntegrationTest extends AbstractPdpIntegrationTest {

  @Override
  public RestTemplate getRestTemplate() {
    return testRestTemplate;
  }

  @Before
  @Override
  public void before() throws IOException {
    super.before();
    headers.set(X_IDP_ENTITY_ID, PolicyLoader.authenticatingAuthority);
    headers.set(X_UNSPECIFIED_NAME_ID, "urn:collab:person:example.com:mary.doe");
    headers.set(X_DISPLAY_NAME, "Mary Doe");
  }

  @Test
  public void testAssertBadRequestUnknownIdentityProvider() throws Exception {
    PdpPolicyDefinition policyDefinition = getPdpPolicyDefinitionFromExistingPolicy();

    headers.set(X_IDP_ENTITY_ID, "http://xxx-idp");

    ResponseEntity<String> post = post("internal/policies", policyDefinition);

    assertEquals(HttpStatus.BAD_REQUEST, post.getStatusCode());
    assertTrue(post.getBody().contains("http://xxx-idp is not a valid or known IdP / SP entityId"));
  }

  @Test
  public void testInternalDecide() throws IOException, JSONStructureException {
    JsonPolicyRequest policyRequest = getJsonPolicyRequest();
    String jsonRequest = objectMapper.writeValueAsString(policyRequest);
    HttpEntity<String> request = new HttpEntity<>(jsonRequest, headers);
    String jsonResponse = testRestTemplate.postForObject("http://localhost:" + port + "/pdp/api/internal/decide/policy", request, String.class);
    Response response = JSONResponse.load(jsonResponse);
    assertEquals(Decision.NOTAPPLICABLE, response.getResults().stream().collect(singletonCollector()).getDecision());
  }



}
@@ -1109,16 +1109,38 @@
 ception;
+%0Aimport java.util.Map;
 %0A%0Aimport
@@ -3070,11 +3070,607 @@
 );%0A  %7D%0A%0A
+  @Test%0A  public void testCreatePolicyWithNameError() throws Exception %7B%0A    PdpPolicy policy = getExistingPolicy();%0A    PdpPolicyDefinition policyDefinition = pdpPolicyDefinitionParser.parse(policy);%0A    policyDefinition.setId(null);%0A    ResponseEntity%3CString%3E response = post(%22internal/policies%22, policyDefinition);%0A%0A    assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());%0A%0A    Map map = objectMapper.readValue(response.getBody(), Map.class);%0A    String message = (String) ((Map) map.get(%22details%22)).get(%22name%22);%0A    assertTrue(message.contains(%22Policy name must be unique%22));%0A  %7D%0A%0A
 %0A%0A%7D

Test unique name for policy