package solutions.digamma.damas.rs;


import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import solutions.digamma.damas.common.WorkspaceException;
import solutions.digamma.damas.content.Document;
import solutions.digamma.damas.content.Folder;
import solutions.digamma.damas.login.Token;
import solutions.digamma.damas.rs.auth.Credentials;
import solutions.digamma.damas.rs.content.DocumentSerialization;
import solutions.digamma.damas.rs.content.FolderSerialization;

import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.MediaType;

/**
 * Unit test for REST API.
 *
 * @author Ahmad Shahwan
 */
@RunWith(Parameterized.class)
public class RestTest extends JerseyTest {

    private static WeldContainer container;

    private MediaType ct;

    public RestTest(MediaType parameter) {
        super();
        this.ct = parameter;
    }

    @Parameterized.Parameters
    public static MediaType[] data() {
        return new MediaType[]{
                MediaType.APPLICATION_JSON_TYPE,
                MediaType.APPLICATION_XML_TYPE
        };
    }

    @BeforeClass
    public static void setUpClass() {
        container = new Weld().initialize();
    }

    @AfterClass
    public static void tearDownClass() {
        container.close();
    }

    @Override
    protected Application configure() {
        return container.select(Application.class).get();
    }

    @Override
    protected void configureClient(ClientConfig clientConfig) {
    }

    @Test
    public void testAuth() {
        Credentials cred = new Credentials("admin", "admin");
        Token auth = target("login").request().post(
                Entity.entity(cred, this.ct),
                Authentication.class);
        String token = auth.getSecret();
        assert StubProviders.TOKEN.equals(token) :
                "Different token from what expected.";
    }

    @Test
    public void testDocument() {
        Document file;
        file = target("documents/" + StubProviders.DOCUMENT_ID)
                .request()
                .accept(this.ct)
                .get(DocumentSerialization.class);
        assert file != null : "Error GETting document.";
        file = target("documents")
            .request()
            .post(Entity.entity(file, this.ct), DocumentSerialization.class);
        assert file != null : "Error POSTing document.";
        file = target("documents/" + StubProviders.DOCUMENT_ID)
            .request()
            .put(Entity.entity(file, this.ct), DocumentSerialization.class);
        assert file != null : "Error PUTing document.";
        target("documents/" + StubProviders.DOCUMENT_ID)
                .request()
                .delete();
    }

    @Test
    public void testFolder() {
        Folder file;
        file = target("folders/" + StubProviders.FOLDER_ID)
                .request()
                .accept(this.ct)
                .get(FolderSerialization.class);
        assert file != null : "Error GETting folder.";
        file = target("folders")
                .request()
                .post(Entity.entity(file, this.ct), FolderSerialization.class);
        assert file != null : "Error POSTing folder.";
        file = target("folders/" + StubProviders.FOLDER_ID)
                .request()
                .put(Entity.entity(file, this.ct), FolderSerialization.class);
        assert file != null : "Error PUTing folder.";
        target("folders/" + StubProviders.FOLDER_ID)
                .request()
                .delete();
    }

    @Test
    public void testFullDocumentResponseContainsMetadata() {
        String response = target(String
                    .format("documents/%s", StubProviders.DOCUMENT_ID))
                .queryParam("full", true)
                .request()
                .accept(this.ct)
                .get(String.class);
        assert response.contains("metadata") : "Expecting metadata in details.";
    }

    @Test
    public void testShortDocumentResponseContainsMetadata() {
        String response = target(String
                .format("documents/%s", StubProviders.DOCUMENT_ID))
                .queryParam("full", false)
                .request()
                .accept(this.ct)
                .get(String.class);
        assert !response.contains("metadata") : "Metadata in short version.";
    }

    @Test
    public void testFullDocumentEntityContainsMetadata()
            throws WorkspaceException {
        Document file = target(String
                .format("documents/%s", StubProviders.DOCUMENT_ID))
                .queryParam("full", true)
                .request()
                .accept(this.ct)
                .get(DocumentSerialization.class);
        assert file.getMetadata() != null : "Expecting metadata in details.";
    }

    @Test
    public void testShortDocumentEntityContainsMetadata()
            throws WorkspaceException {
        Document file = target(String
                .format("documents/%s", StubProviders.DOCUMENT_ID))
                .queryParam("full", false)
                .request()
                .accept(this.ct)
                .get(DocumentSerialization.class);
        assert file.getMetadata() == null : "Metadata in short version.";
    }

}

@@ -788,16 +788,53 @@
 Entity;%0A
+import javax.ws.rs.client.WebTarget;%0A
 import j
@@ -858,24 +858,24 @@
 pplication;%0A
-
 import javax
@@ -2304,35 +2304,36 @@
 rget(%22documents/
-%22 +
+%25s%22,
  StubProviders.D
@@ -2730,35 +2730,36 @@
 rget(%22documents/
-%22 +
+%25s%22,
  StubProviders.D
@@ -2952,19 +2952,20 @@
 cuments/
-%22 +
+%25s%22,
  StubPro
@@ -3130,35 +3130,36 @@
 target(%22folders/
-%22 +
+%25s%22,
  StubProviders.F
@@ -3550,35 +3550,36 @@
 target(%22folders/
-%22 +
+%25s%22,
  StubProviders.F
@@ -3776,11 +3776,12 @@
 ers/
-%22 +
+%25s%22,
  Stu
@@ -3971,43 +3971,8 @@
 get(
-String%0A                    .format(
 %22doc
@@ -4001,33 +4001,32 @@
 ers.DOCUMENT_ID)
-)
 %0A               
@@ -4345,39 +4345,8 @@
 get(
-String%0A                .format(
 %22doc
@@ -4375,33 +4375,32 @@
 ers.DOCUMENT_ID)
-)
 %0A               
@@ -4750,39 +4750,8 @@
 get(
-String%0A                .format(
 %22doc
@@ -4780,33 +4780,32 @@
 ers.DOCUMENT_ID)
-)
 %0A               
@@ -5170,39 +5170,8 @@
 get(
-String%0A                .format(
 %22doc
@@ -5208,17 +5208,16 @@
 MENT_ID)
-)
 %0A       
@@ -5355,32 +5355,32 @@
 ization.class);%0A
-
         assert f
@@ -5444,10 +5444,133 @@
 %0A    %7D%0A%0A
+    private WebTarget target(String pattern, Object... args) %7B%0A        return target(String.format(pattern, args));%0A    %7D%0A%0A
 %7D%0A

Replace concat with format string in test