<commit_before>/*
* JBoss, Home of Professional Open Source.
* Copyright 2013, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.wildfly.test.extension.rts;

import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Link;
import javax.ws.rs.core.Response;

import org.codehaus.jettison.json.JSONArray;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.as.arquillian.container.ManagementClient;
import org.jboss.jbossts.star.util.TxLinkNames;
import org.jboss.jbossts.star.util.TxStatusMediaType;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.wildfly.test.extension.rts.common.InboundBridgeResource;
import org.wildfly.test.extension.rts.common.LoggingRestATResource;
import org.wildfly.test.extension.rts.common.LoggingXAResource;

/**
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
@RunAsClient
@RunWith(Arquillian.class)
public final class InboundBridgeTestCase extends AbstractTestCase {

    private static final String DEPENDENCIES = "Dependencies: org.jboss.narayana.rts, org.jboss.jts, org.codehaus.jettison\n";

    private String inboundBridgeResourceUrl;

    private String loggingRestATParticipantUrl;

    private String loggingRestATParticipantInvocationsUrl;

    @ArquillianResource
    private ManagementClient managementClient;

    @Deployment
    public static WebArchive getDeployment() {
        return AbstractTestCase.getDeployment()
                .addClasses(InboundBridgeResource.class, LoggingXAResource.class, LoggingRestATResource.class)
                .addAsWebInfResource(InboundBridgeTestCase.class.getClassLoader().getResource("web.xml"),"web.xml")
                .addAsManifestResource(new StringAsset(DEPENDENCIES), "MANIFEST.MF");
    }

    @Before
    public void before() {
        super.before();
        inboundBridgeResourceUrl = getDeploymentUrl() + InboundBridgeResource.URL_SEGMENT;
        loggingRestATParticipantUrl = getDeploymentUrl() + LoggingRestATResource.BASE_URL_SEGMENT;
        loggingRestATParticipantInvocationsUrl = loggingRestATParticipantUrl + "/" + LoggingRestATResource.INVOCATIONS_URL_SEGMENT;
        resetInvocations();
    }

    @Test
    public void testCommit() throws Exception {
        txSupport.startTx();
        enlistInboundBridgeResource();
        txSupport.commitTx();

        final JSONArray jsonArray = getInboundBridgeResourceInvocations();

        Assert.assertEquals(4, jsonArray.length());
        Assert.assertEquals("LoggingXAResource.start", jsonArray.get(0));
        Assert.assertEquals("LoggingXAResource.end", jsonArray.get(1));
        Assert.assertEquals("LoggingXAResource.prepare", jsonArray.get(2));
        Assert.assertEquals("LoggingXAResource.commit", jsonArray.get(3));
    }

    @Test
    public void testRollback() throws Exception {
        txSupport.startTx();
        enlistInboundBridgeResource();
        txSupport.rollbackTx();

        JSONArray jsonArray = getInboundBridgeResourceInvocations();

        Assert.assertEquals(3, jsonArray.length());
        Assert.assertEquals("LoggingXAResource.start", jsonArray.get(0));
        Assert.assertEquals("LoggingXAResource.end", jsonArray.get(1));
        Assert.assertEquals("LoggingXAResource.rollback", jsonArray.get(2));
    }

    @Test
    public void testCommitWithTwoParticipants() throws Exception {
        txSupport.startTx();
        enlistLoggingRestATParticipant();
        enlistInboundBridgeResource();
        txSupport.commitTx();

        JSONArray participantResourceInvocations = getLoggingRestATParticipantInvocations();
        JSONArray xaResourceInvocations = getInboundBridgeResourceInvocations();

        Assert.assertEquals(2, participantResourceInvocations.length());
        Assert.assertEquals("LoggingRestATResource.terminateParticipant(" + TxStatusMediaType.TX_PREPARED + ")",
                participantResourceInvocations.get(0));
        Assert.assertEquals("LoggingRestATResource.terminateParticipant(" + TxStatusMediaType.TX_COMMITTED + ")",
                participantResourceInvocations.get(1));

        Assert.assertEquals(4, xaResourceInvocations.length());
        Assert.assertEquals("LoggingXAResource.start", xaResourceInvocations.get(0));
        Assert.assertEquals("LoggingXAResource.end", xaResourceInvocations.get(1));
        Assert.assertEquals("LoggingXAResource.prepare", xaResourceInvocations.get(2));
        Assert.assertEquals("LoggingXAResource.commit", xaResourceInvocations.get(3));

    }

    @Test
    public void testRollbackWithTwoParticipants() throws Exception {
        txSupport.startTx();
        enlistLoggingRestATParticipant();
        enlistInboundBridgeResource();
        txSupport.rollbackTx();

        JSONArray participantResourceInvocations = getLoggingRestATParticipantInvocations();
        JSONArray xaResourceInvocations = getInboundBridgeResourceInvocations();

        Assert.assertEquals(1, participantResourceInvocations.length());
        Assert.assertEquals("LoggingRestATResource.terminateParticipant(" + TxStatusMediaType.TX_ROLLEDBACK + ")",
                participantResourceInvocations.get(0));

        Assert.assertEquals(3, xaResourceInvocations.length());
        Assert.assertEquals("LoggingXAResource.start", xaResourceInvocations.get(0));
        Assert.assertEquals("LoggingXAResource.end", xaResourceInvocations.get(1));
        Assert.assertEquals("LoggingXAResource.rollback", xaResourceInvocations.get(2));
    }

    protected String getBaseUrl() {
        return managementClient.getWebUri().toString();
    }

    private void enlistInboundBridgeResource() {
        final Link participantLink = Link.fromUri(txSupport.getTxnUri()).rel(TxLinkNames.PARTICIPANT).title(TxLinkNames.PARTICIPANT).build();
        final Response response = ClientBuilder.newClient().target(inboundBridgeResourceUrl).request()
                .header("link", participantLink).post(null);

        Assert.assertEquals(200, response.getStatus());
    }

    private void enlistLoggingRestATParticipant() {
        String linkHeader = txSupport.makeTwoPhaseAwareParticipantLinkHeader(loggingRestATParticipantUrl, false, null, null);
        String recoveryUrl = txSupport.enlistParticipant(txSupport.getTxnUri(), linkHeader);

        Assert.assertFalse(recoveryUrl == null);
    }

    private void resetInvocations() {
        Response response = ClientBuilder.newClient().target(inboundBridgeResourceUrl).request().put(null);
        Assert.assertEquals(200, response.getStatus());

        response = ClientBuilder.newClient().target(loggingRestATParticipantInvocationsUrl).request().put(null);
        Assert.assertEquals(200, response.getStatus());
    }

    private JSONArray getInboundBridgeResourceInvocations() throws Exception {
        final String response = ClientBuilder.newClient().target(inboundBridgeResourceUrl).request().get(String.class);
        final JSONArray jsonArray = new JSONArray(response);

        return jsonArray;
    }

    private JSONArray getLoggingRestATParticipantInvocations() throws Exception {
        String response = ClientBuilder.newClient().target(loggingRestATParticipantInvocationsUrl).request().get(String.class);
        JSONArray jsonArray = new JSONArray(response);

        return jsonArray;
    }

}<commit_msg>[WFLY-11849] fixing the rts inbound test to be more resilient to changes as now there is setTimeout called up to the start/end/commit<commit_after>/*
* JBoss, Home of Professional Open Source.
* Copyright 2013, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.wildfly.test.extension.rts;

import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Link;
import javax.ws.rs.core.Response;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.as.arquillian.container.ManagementClient;
import org.jboss.jbossts.star.util.TxLinkNames;
import org.jboss.jbossts.star.util.TxStatusMediaType;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.wildfly.test.extension.rts.common.InboundBridgeResource;
import org.wildfly.test.extension.rts.common.LoggingRestATResource;
import org.wildfly.test.extension.rts.common.LoggingXAResource;

/**
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
@RunAsClient
@RunWith(Arquillian.class)
public final class InboundBridgeTestCase extends AbstractTestCase {

    private static final String DEPENDENCIES = "Dependencies: org.jboss.narayana.rts, org.jboss.jts, org.codehaus.jettison\n";

    private String inboundBridgeResourceUrl;

    private String loggingRestATParticipantUrl;

    private String loggingRestATParticipantInvocationsUrl;

    @ArquillianResource
    private ManagementClient managementClient;

    @Deployment
    public static WebArchive getDeployment() {
        return AbstractTestCase.getDeployment()
                .addClasses(InboundBridgeResource.class, LoggingXAResource.class, LoggingRestATResource.class)
                .addAsWebInfResource(InboundBridgeTestCase.class.getClassLoader().getResource("web.xml"),"web.xml")
                .addAsManifestResource(new StringAsset(DEPENDENCIES), "MANIFEST.MF");
    }

    @Before
    public void before() {
        super.before();
        inboundBridgeResourceUrl = getDeploymentUrl() + InboundBridgeResource.URL_SEGMENT;
        loggingRestATParticipantUrl = getDeploymentUrl() + LoggingRestATResource.BASE_URL_SEGMENT;
        loggingRestATParticipantInvocationsUrl = loggingRestATParticipantUrl + "/" + LoggingRestATResource.INVOCATIONS_URL_SEGMENT;
        resetInvocations();
    }

    @Test
    public void testCommit() throws Exception {
        txSupport.startTx();
        enlistInboundBridgeResource();
        txSupport.commitTx();

        final JSONArray jsonArray = getInboundBridgeResourceInvocations();

        assertJsonArray(jsonArray, "LoggingXAResource.start", 1);
        assertJsonArray(jsonArray, "LoggingXAResource.end", 1);
        assertJsonArray(jsonArray, "LoggingXAResource.prepare", 1);
        assertJsonArray(jsonArray, "LoggingXAResource.commit", 1);
        assertJsonArray(jsonArray, "LoggingXAResource.rollback", 0);
    }

    @Test
    public void testRollback() throws Exception {
        txSupport.startTx();
        enlistInboundBridgeResource();
        txSupport.rollbackTx();

        JSONArray jsonArray = getInboundBridgeResourceInvocations();

        assertJsonArray(jsonArray, "LoggingXAResource.start", 1);
        assertJsonArray(jsonArray, "LoggingXAResource.end", 1);
        assertJsonArray(jsonArray, "LoggingXAResource.prepare", 0);
        assertJsonArray(jsonArray, "LoggingXAResource.commit", 0);
        assertJsonArray(jsonArray, "LoggingXAResource.rollback", 1);
    }

    @Test
    public void testCommitWithTwoParticipants() throws Exception {
        txSupport.startTx();
        enlistLoggingRestATParticipant();
        enlistInboundBridgeResource();
        txSupport.commitTx();

        JSONArray participantResourceInvocations = getLoggingRestATParticipantInvocations();
        JSONArray xaResourceInvocations = getInboundBridgeResourceInvocations();

        Assert.assertEquals(2, participantResourceInvocations.length());
        Assert.assertEquals("LoggingRestATResource.terminateParticipant(" + TxStatusMediaType.TX_PREPARED + ")",
                participantResourceInvocations.get(0));
        Assert.assertEquals("LoggingRestATResource.terminateParticipant(" + TxStatusMediaType.TX_COMMITTED + ")",
                participantResourceInvocations.get(1));

        assertJsonArray(xaResourceInvocations, "LoggingXAResource.start", 1);
        assertJsonArray(xaResourceInvocations, "LoggingXAResource.end", 1);
        assertJsonArray(xaResourceInvocations, "LoggingXAResource.prepare", 1);
        assertJsonArray(xaResourceInvocations, "LoggingXAResource.commit", 1);
        assertJsonArray(xaResourceInvocations, "LoggingXAResource.rollback", 0);
    }

    @Test
    public void testRollbackWithTwoParticipants() throws Exception {
        txSupport.startTx();
        enlistLoggingRestATParticipant();
        enlistInboundBridgeResource();
        txSupport.rollbackTx();

        JSONArray participantResourceInvocations = getLoggingRestATParticipantInvocations();
        JSONArray xaResourceInvocations = getInboundBridgeResourceInvocations();

        Assert.assertEquals(1, participantResourceInvocations.length());
        Assert.assertEquals("LoggingRestATResource.terminateParticipant(" + TxStatusMediaType.TX_ROLLEDBACK + ")",
                participantResourceInvocations.get(0));

        assertJsonArray(xaResourceInvocations, "LoggingXAResource.start", 1);
        assertJsonArray(xaResourceInvocations, "LoggingXAResource.end", 1);
        assertJsonArray(xaResourceInvocations, "LoggingXAResource.prepare", 0);
        assertJsonArray(xaResourceInvocations, "LoggingXAResource.commit", 0);
        assertJsonArray(xaResourceInvocations, "LoggingXAResource.rollback", 1);
    }

    protected String getBaseUrl() {
        return managementClient.getWebUri().toString();
    }

    private void enlistInboundBridgeResource() {
        final Link participantLink = Link.fromUri(txSupport.getTxnUri()).rel(TxLinkNames.PARTICIPANT).title(TxLinkNames.PARTICIPANT).build();
        final Response response = ClientBuilder.newClient().target(inboundBridgeResourceUrl).request()
                .header("link", participantLink).post(null);

        Assert.assertEquals(200, response.getStatus());
    }

    private void enlistLoggingRestATParticipant() {
        String linkHeader = txSupport.makeTwoPhaseAwareParticipantLinkHeader(loggingRestATParticipantUrl, false, null, null);
        String recoveryUrl = txSupport.enlistParticipant(txSupport.getTxnUri(), linkHeader);

        Assert.assertFalse(recoveryUrl == null);
    }

    private void resetInvocations() {
        Response response = ClientBuilder.newClient().target(inboundBridgeResourceUrl).request().put(null);
        Assert.assertEquals(200, response.getStatus());

        response = ClientBuilder.newClient().target(loggingRestATParticipantInvocationsUrl).request().put(null);
        Assert.assertEquals(200, response.getStatus());
    }

    private JSONArray getInboundBridgeResourceInvocations() throws Exception {
        final String response = ClientBuilder.newClient().target(inboundBridgeResourceUrl).request().get(String.class);
        final JSONArray jsonArray = new JSONArray(response);

        return jsonArray;
    }

    private JSONArray getLoggingRestATParticipantInvocations() throws Exception {
        String response = ClientBuilder.newClient().target(loggingRestATParticipantInvocationsUrl).request().get(String.class);
        JSONArray jsonArray = new JSONArray(response);

        return jsonArray;
    }

    /**
     * Checking if the parameter <code>recordToAssert</code>
     * is placed exactly once in the {@link JSONArray}.
     */
    private void assertJsonArray(JSONArray invocationsJSONArray, String recordToAssert, int expectedRecordFoundCount) throws JSONException {
        if(recordToAssert == null) throw new NullPointerException("recordToAssert");
        int recordFoundCount = 0;
        for(int i = 0; i < invocationsJSONArray.length(); i++) {
            if(recordToAssert.equals(invocationsJSONArray.get(i))) {
                recordFoundCount++;
            }
        }
        if (recordFoundCount != expectedRecordFoundCount) {
            Assert.fail("Invocation result returned as a JSON array '" + invocationsJSONArray + "' "
                    + "expected to contain the record '" + recordToAssert + "' " + expectedRecordFoundCount + " times "
                    + "but the record was " + recordFoundCount + " times in the array");
        }
    }

}
{'fraction_non_alphanumeric': 0.09580411919368975, 'fraction_numerical': 0.005861086765994741, 'mean_word_length': 4.166100735710243, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7492908', 'n_tokens_mistral': 5271, 'n_tokens_neox': 4912, 'n_words': 1002}