105 lines
3.4 KiB
Java
105 lines
3.4 KiB
Java
package org.gcube.resourcemanagement.manager.webapp.context;
|
|
|
|
import static org.junit.Assert.*;
|
|
|
|
import java.io.IOException;
|
|
|
|
import javax.ws.rs.client.Entity;
|
|
import javax.ws.rs.core.Application;
|
|
import javax.ws.rs.core.MediaType;
|
|
import javax.ws.rs.core.Response;
|
|
import javax.ws.rs.core.Response.Status;
|
|
|
|
import org.gcube.informationsystem.impl.entity.ContextImpl;
|
|
import org.gcube.informationsystem.impl.utils.ISMapper;
|
|
import org.gcube.informationsystem.model.entity.Context;
|
|
import org.gcube.resourcemanagement.manager.io.rs.RMContextPath;
|
|
import org.gcube.resourcemanagement.manager.webapp.rs.RMContext;
|
|
import org.glassfish.jersey.server.ResourceConfig;
|
|
import org.glassfish.jersey.test.JerseyTest;
|
|
import org.junit.FixMethodOrder;
|
|
import org.junit.Test;
|
|
import org.junit.runner.RunWith;
|
|
import org.junit.runners.BlockJUnit4ClassRunner;
|
|
import org.junit.runners.MethodSorters;
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
|
|
|
/**
|
|
* Test cases for the Context methods.
|
|
*
|
|
* @author Manuele Simi (ISTI-CNR)
|
|
*/
|
|
@RunWith(BlockJUnit4ClassRunner.class)
|
|
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
|
public class RMContextTest extends JerseyTest {
|
|
|
|
private final static String contextName = "firstContext";
|
|
|
|
@Override
|
|
protected Application configure() {
|
|
return new ResourceConfig(RMContext.class);
|
|
}
|
|
|
|
|
|
/**
|
|
* Test method for marshal/unmarshal contexts.
|
|
*/
|
|
@Test
|
|
public void step0_Context() {
|
|
Context newContext = new ContextImpl(contextName);
|
|
//newContext.setParent(new ContextImpl("parent"));
|
|
try {
|
|
ISMapper.unmarshal(Context.class, ISMapper.marshal(newContext));
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
assertFalse("Failed to unmarshal the context.", false);
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* Test method for
|
|
* {@link org.gcube.resourcemanagement.manager.webapp.rs.RMContext#create(java.lang.String)}.
|
|
*/
|
|
@Test
|
|
public void step1_Create() {
|
|
Context newContext = new ContextImpl(contextName);
|
|
try {
|
|
System.out.print(ISMapper.marshal(newContext));
|
|
Response create = target("context")
|
|
.queryParam(RMContextPath.FORCE_RRURL_PARAM, "http://manuele-registry.dev.d4science.org/resource-registry")
|
|
.request()
|
|
.post(Entity.entity(ISMapper.marshal(newContext), MediaType.APPLICATION_JSON + ";charset=UTF-8"));
|
|
|
|
assertNotNull(create);
|
|
//TODO: to restore an expected success when create is fully functional
|
|
assertEquals("Unexpected returned code. Reason: " + create.getStatusInfo().getReasonPhrase(),
|
|
Status.CREATED.getStatusCode(), create.getStatus());
|
|
assertEquals("Context successfully created.", (String) create.readEntity(String.class));
|
|
} catch (JsonProcessingException e) {
|
|
assertFalse("Failed to marshal the context.", false);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Test method for
|
|
* {@link org.gcube.resourcemanagement.manager.webapp.rs.RMContext#create(java.lang.String)} when the parent does not exist.
|
|
*/
|
|
@Test
|
|
public void step1_CreateWithInvalidParent() {
|
|
Context newContext = new ContextImpl(contextName);
|
|
newContext.setParent(new ContextImpl("DoNotExist"));
|
|
try {
|
|
Response create = target("context").request()
|
|
.post(Entity.entity(ISMapper.marshal(newContext), MediaType.APPLICATION_JSON + ";charset=UTF-8"));
|
|
assertEquals("Unexpected returned code. Reason: " + create.getStatusInfo().getReasonPhrase(),
|
|
Status.BAD_REQUEST.getStatusCode(), create.getStatus());
|
|
} catch (JsonProcessingException e) {
|
|
assertFalse("Failed to marshal the context.", false);
|
|
}
|
|
}
|
|
|
|
}
|