Skip to content

Commit

Permalink
dao. create attachment
Browse files Browse the repository at this point in the history
  • Loading branch information
okean committed Jul 9, 2016
1 parent 7f81e47 commit b851807
Show file tree
Hide file tree
Showing 5 changed files with 269 additions and 12 deletions.
55 changes: 52 additions & 3 deletions src/java/org/alm/Dao.java
Expand Up @@ -3,12 +3,12 @@
import java.net.URI;

import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import org.alm.model.Test;
import org.alm.model.TestInstance;
import org.alm.model.TestSet;
import org.alm.model.*;
import org.apache.commons.lang.StringUtils;

public final class Dao
Expand Down Expand Up @@ -141,6 +141,37 @@ public static TestInstance readTestInstance(String id) throws Exception
return connector().get(testInstanceUrl, TestInstance.class, null, null);
}

/**
* Create an attachment for run entity
*
* @param runId
* @param fileName to use on serverside
* @param fileData content of file
* @return the xml of the metadata on the created attachment
* @throws Exception
*/
public static Attachment createRunAttachment(String runId, String fileName, byte[] fileData) throws Exception
{
String attachmentsUrl = connector().buildEntityUrl("run", runId) + "/attachments";

return createAttachment(attachmentsUrl, fileName, fileData);
}

/**
* Create an attachment for run step entity
*
* @param runId
* @param fileName to use on serverside
* @param fileData content of file
* @return the xml of the metadata on the created attachment
* @throws Exception
*/
public static Attachment createRunStepAttachment(String runStepId, String fileName, byte[] fileData) throws Exception
{
String attachmentsUrl = connector().buildEntityUrl("run-step", runStepId) + "/attachments";

return createAttachment(attachmentsUrl, fileName, fileData);
}
/**
* Gets an instance of RestConnector
*
Expand All @@ -150,4 +181,22 @@ private static RestConnector connector()
{
return RestConnector.instance();
}

/**
* Create attachment
*
* @param entityUrl url of entity to attach the file to
* @param fileName to use on serverside
* @param payload content of file
* @return the xml of the metadata on the created attachment
* @throws Exception
*/
private static Attachment createAttachment(String entityUrl, String fileName, byte[] fileData) throws Exception
{
MultivaluedMap<String, Object> headers = new MultivaluedHashMap<String, Object>();
headers.add("Accept", "application/xml");
headers.add("Slug", fileName);

return connector().post(entityUrl, Attachment.class, headers, null, fileData, "application/octet-stream");
}
}
54 changes: 45 additions & 9 deletions src/java/org/alm/RestConnector.java
Expand Up @@ -141,6 +141,19 @@ public <T> T post(
return call(HttpMethod.POST, path, headers, queryParams, payload, entityType);
}

public <T> T post(
String path,
Class<T> entityType,
MultivaluedMap<String, Object> headers,
Map<String, String> queryParams,
Object payload,
String contentType) throws Exception
{
Log.debug("POST: {}", path);

return call(HttpMethod.POST, path, headers, queryParams, payload, contentType, entityType);
}

public <T> T put(
String path,
Class<T> entityType,
Expand Down Expand Up @@ -199,23 +212,46 @@ private <T> T call(
Map<String, String> queryParams,
Object payload,
Class<T> entityType) throws Exception
{
Response res = call(methodName, path, headers, queryParams, payload);
{
return call(methodName, path, headers, queryParams, payload, MediaType.APPLICATION_XML, entityType);
}

if(!res.hasEntity())
{
return null;
}
private <T> T call(
String methodName,
String path,
MultivaluedMap<String, Object> headers,
Map<String, String> queryParams,
Object payload,
String contentType,
Class<T> entityType) throws Exception
{
Response res = call(methodName, path, headers, queryParams, payload, contentType);

return (T) res.readEntity(entityType);
}
if(!res.hasEntity())
{
return null;
}

return (T) res.readEntity(entityType);
}

private Response call(
String methodName,
String path,
MultivaluedMap<String, Object> headers,
Map<String, String> queryParams,
Object payload) throws Exception
{
return call(methodName, path, headers, queryParams, payload, MediaType.APPLICATION_XML);
}

private Response call(
String methodName,
String path,
MultivaluedMap<String, Object> headers,
Map<String, String> queryParams,
Object payload,
String contentType) throws Exception
{
WebTarget webTarget = createWebTarget(buildUrl(path), queryParams);

Expand All @@ -230,7 +266,7 @@ private Response call(
}

Response res = result.method(
methodName, Entity.entity(payload, MediaType.APPLICATION_XML), Response.class);
methodName, Entity.entity(payload, contentType), Response.class);

int statusCode = res.getStatus();

Expand Down
57 changes: 57 additions & 0 deletions src/java/org/alm/model/Attachment.java
@@ -0,0 +1,57 @@
package org.alm.model;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Attachment extends Entity
{
public Attachment(Entity entity)
{
super(entity);
}

public Attachment()
{
type("attachment");
}

public String description()
{
return fieldValue("description");
}

public void description(String value)
{
fieldValue("description", value);
}

public String fileSize()
{
return fieldValue("file-size");
}

public void fileSize(String value)
{
fieldValue("file-size", value);
}

public String refType()
{
return fieldValue("ref-type");
}

public void refType(String value)
{
fieldValue("ref-type", value);
}

public String parentType()
{
return fieldValue("parent-type");
}

public void parentType(String value)
{
fieldValue("parent-type", value);
}
}
61 changes: 61 additions & 0 deletions src/test/java/org/alm/TestDao.java
@@ -1,5 +1,6 @@
package org.alm;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
Expand All @@ -8,20 +9,24 @@
import java.util.Properties;
import java.util.UUID;

import javax.ws.rs.Consumes;
import javax.ws.rs.CookieParam;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Cookie;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.NewCookie;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.UriInfo;

import org.alm.model.Attachment;
import org.alm.model.TestInstance;
import org.alm.model.TestSet;
import org.glassfish.grizzly.http.server.HttpServer;
Expand Down Expand Up @@ -133,6 +138,34 @@ public void readTestInstanceEntity() throws Exception
EntityAssert.assertEquals(actual, expected);
}

@Test(groups = { "crud", "enity", "attachment" })
public void createRunAttachment() throws Exception
{
String fileName = "test.txt";
String fileContent = "content of test file";

Attachment attachment = Dao.createRunAttachment("3", fileName, fileContent.getBytes());

Assert.assertEquals(attachment.name(), fileName);
Assert.assertEquals(attachment.fileSize(), String.valueOf(fileContent.length()));
Assert.assertEquals(attachment.parentId(), "3");
Assert.assertEquals(attachment.parentType(), "runs");
}

@Test(groups = { "crud", "enity", "attachment" })
public void createRunStepAttachment() throws Exception
{
String fileName = "run-step.txt";
String fileContent = "content of run step file";

Attachment attachment = Dao.createRunStepAttachment("4", fileName, fileContent.getBytes());

Assert.assertEquals(attachment.name(), fileName);
Assert.assertEquals(attachment.fileSize(), String.valueOf(fileContent.length()));
Assert.assertEquals(attachment.parentId(), "4");
Assert.assertEquals(attachment.parentType(), "run-steps");
}

private static String authenticationPoint(String host, String port)
{
return String.format("http://%s:%s/qcbin/authentication-point", host, port);
Expand Down Expand Up @@ -196,6 +229,18 @@ private static TestInstance createTestInstanceEntity(String id)
return testInstance;
}

private static Attachment createAttachment(String name, String fileSize, String parentId, String parentType)
{
Attachment attachment = new Attachment();

attachment.name(name);
attachment.fileSize(fileSize);
attachment.parentId(parentId);
attachment.parentType(parentType);

return attachment;
}

@Path("/qcbin")
public static class RestApiStub
{
Expand Down Expand Up @@ -285,6 +330,22 @@ public TestInstance testInstance(@PathParam("domain") String domain,
return testInstance;
}

@POST
@Path("/rest/domains/{domain}/projects/{project}/{entityCollection}/{enityId}/attachments")
@Consumes("application/octet-stream")
@Produces("application/xml")
public Attachment createAttachments(
@HeaderParam("Slug") String fileName,
@PathParam("enityId") String enityId,
@PathParam("entityCollection") String entityCollection,
File file) throws Exception
{
Attachment attachment = createAttachment(
fileName, String.valueOf(file.length()), enityId, entityCollection);

return attachment;
}

private static Response unauthorizedResponse(UriInfo uriInfo)
{
URI baseUri = uriInfo.getBaseUri();
Expand Down
54 changes: 54 additions & 0 deletions src/test/java/org/alm/model/TestAttachment.java
@@ -0,0 +1,54 @@
package org.alm.model;

import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class TestAttachment
{
Attachment attachment;

@BeforeMethod
public void initAttachmentEntity()
{
attachment = new Attachment();
}

@Test(groups = { "attachment" })
public void type()
{
Assert.assertEquals(attachment.type(), "attachment");
}

@Test(groups = { "attachment" })
public void description()
{
attachment.description("raw results");

Assert.assertEquals(attachment.description(), "raw results");
}

@Test(groups = { "attachment" })
public void fileSize()
{
attachment.fileSize("412");

Assert.assertEquals(attachment.fileSize(), "412");
}

@Test(groups = { "attachment" })
public void refType()
{
attachment.refType("File");

Assert.assertEquals(attachment.refType(), "File");
}

@Test(groups = { "attachment" })
public void parentType()
{
attachment.parentType("defect");

Assert.assertEquals(attachment.parentType(), "defect");
}
}

0 comments on commit b851807

Please sign in to comment.