You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
4.5 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.common.authorization.client.Constants;
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
import org.gcube.common.authorization.library.AuthorizationEntry;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.scope.api.ScopeProvider;
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.AfterClass;
import org.junit.BeforeClass;
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";
private final static String RR = "";
public static final String DEFAULT_TEST_SCOPE ="";
@BeforeClass
public static void beforeClass() throws Exception{
setContext(DEFAULT_TEST_SCOPE);
}
public static void setContext(String token) throws ObjectNotFound, Exception{
SecurityTokenProvider.instance.set(token);
ScopeProvider.instance.set(getCurrentScope(token));
}
public static String getCurrentScope(String token) throws ObjectNotFound, Exception{
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token);
String context = authorizationEntry.getContext();
return context;
}
@AfterClass
public static void afterClass() throws Exception{
SecurityTokenProvider.instance.reset();
ScopeProvider.instance.reset();
}
@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, RR)
.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);
}
}
}