@BeforeMethod
 public void setUp() throws ComponentInitializationException {
   action = new AddAudienceRestrictionToAssertions();
   action.setAudienceRestrictionsLookupStrategy(
       new Function<ProfileRequestContext, Collection<String>>() {
         public Collection<String> apply(ProfileRequestContext input) {
           return ImmutableList.of(AUDIENCE1, AUDIENCE2);
         }
       });
   action.initialize();
 }
  /** Test that action errors out properly if there is no response. */
  @Test
  public void testNoResponse() throws Exception {
    final ProfileRequestContext prc = new RequestContextBuilder().buildProfileRequestContext();

    action.execute(prc);
    ActionTestingSupport.assertEvent(prc, EventIds.INVALID_MSG_CTX);
  }
  /** Test that the condition is properly added if there are multiple assertions in the response. */
  @Test
  public void testMultipleAssertion() throws Exception {
    final Response response = SAML1ActionTestingSupport.buildResponse();
    response.getAssertions().add(SAML1ActionTestingSupport.buildAssertion());
    response.getAssertions().add(SAML1ActionTestingSupport.buildAssertion());
    response.getAssertions().add(SAML1ActionTestingSupport.buildAssertion());

    final ProfileRequestContext prc =
        new RequestContextBuilder().setOutboundMessage(response).buildProfileRequestContext();

    action.execute(prc);
    ActionTestingSupport.assertProceedEvent(prc);

    Assert.assertNotNull(response.getAssertions());
    Assert.assertEquals(response.getAssertions().size(), 3);

    for (Assertion assertion : response.getAssertions()) {
      Assert.assertNotNull(assertion.getConditions());
      Assert.assertEquals(assertion.getConditions().getAudienceRestrictionConditions().size(), 1);
      final AudienceRestrictionCondition audcond =
          assertion.getConditions().getAudienceRestrictionConditions().get(0);
      Assert.assertEquals(audcond.getAudiences().size(), 2);
      Assert.assertEquals(audcond.getAudiences().get(0).getUri(), AUDIENCE1);
      Assert.assertEquals(audcond.getAudiences().get(1).getUri(), AUDIENCE2);
    }
  }
  /** Test that an addition condition is not added if an assertion already contains one. */
  @Test
  public void testSingleAssertionWithExistingAudienceCondition() throws Exception {
    final SAMLObjectBuilder<AudienceRestrictionCondition> conditionBuilder =
        (SAMLObjectBuilder<AudienceRestrictionCondition>)
            XMLObjectProviderRegistrySupport.getBuilderFactory()
                .getBuilder(AudienceRestrictionCondition.DEFAULT_ELEMENT_NAME);
    final AudienceRestrictionCondition condition = conditionBuilder.buildObject();

    final SAMLObjectBuilder<Conditions> conditionsBuilder =
        (SAMLObjectBuilder<Conditions>)
            XMLObjectProviderRegistrySupport.getBuilderFactory()
                .getBuilder(Conditions.DEFAULT_ELEMENT_NAME);
    final Conditions conditions = conditionsBuilder.buildObject();
    conditions.getAudienceRestrictionConditions().add(condition);

    final Assertion assertion = SAML1ActionTestingSupport.buildAssertion();
    assertion.setConditions(conditions);

    final Response response = SAML1ActionTestingSupport.buildResponse();
    response.getAssertions().add(assertion);

    final ProfileRequestContext prc =
        new RequestContextBuilder().setOutboundMessage(response).buildProfileRequestContext();

    action.execute(prc);
    ActionTestingSupport.assertProceedEvent(prc);

    Assert.assertNotNull(assertion.getConditions());
    Assert.assertEquals(assertion.getConditions().getAudienceRestrictionConditions().size(), 1);
    final AudienceRestrictionCondition audcond =
        assertion.getConditions().getAudienceRestrictionConditions().get(0);
    Assert.assertEquals(audcond.getAudiences().size(), 2);
    Assert.assertEquals(audcond.getAudiences().get(0).getUri(), AUDIENCE1);
    Assert.assertEquals(audcond.getAudiences().get(1).getUri(), AUDIENCE2);
  }
  /** Test that action behaves properly if there is no assertion in the response. */
  @Test
  public void testNoAssertion() throws Exception {
    final ProfileRequestContext prc =
        new RequestContextBuilder()
            .setOutboundMessage(SAML1ActionTestingSupport.buildResponse())
            .buildProfileRequestContext();

    action.execute(prc);
    ActionTestingSupport.assertProceedEvent(prc);
  }
  /**
   * Test that the condition is properly added if there is a single assertion, without a Conditions
   * element, in the response.
   */
  @Test
  public void testSAML2Assertion() throws Exception {
    final org.opensaml.saml.saml2.core.Assertion assertion =
        SAML2ActionTestingSupport.buildAssertion();
    final org.opensaml.saml.saml2.core.Response response =
        SAML2ActionTestingSupport.buildResponse();
    response.getAssertions().add(assertion);

    final ProfileRequestContext prc =
        new RequestContextBuilder().setOutboundMessage(response).buildProfileRequestContext();

    action.execute(prc);
    ActionTestingSupport.assertProceedEvent(prc);

    Assert.assertNotNull(response.getAssertions());
    Assert.assertEquals(response.getAssertions().size(), 1);

    Assert.assertNotNull(assertion.getConditions());
    Assert.assertEquals(assertion.getConditions().getAudienceRestrictions().size(), 1);
    final AudienceRestriction audcond = assertion.getConditions().getAudienceRestrictions().get(0);
    Assert.assertEquals(audcond.getAudiences().size(), 2);
    Assert.assertEquals(audcond.getAudiences().get(0).getAudienceURI(), AUDIENCE1);
    Assert.assertEquals(audcond.getAudiences().get(1).getAudienceURI(), AUDIENCE2);
  }