158 lines
5.3 KiB
Java
158 lines
5.3 KiB
Java
package org.gcube.resourcemanagement.manager.webapp.context;
|
|
|
|
import static org.junit.Assert.*;
|
|
|
|
import java.io.IOException;
|
|
import java.util.UUID;
|
|
|
|
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.codeexceptions.CodeFinder;
|
|
import org.gcube.resourcemanagement.manager.io.codeexceptions.SerializableErrorCode;
|
|
import org.gcube.resourcemanagement.manager.io.rs.RMCreateContextCode;
|
|
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 = "firstContext7";
|
|
|
|
private final static String RR = "http://manuele-registry.dev.d4science.org/resource-registry";
|
|
|
|
public static final String DEFAULT_TEST_SCOPE ="";
|
|
|
|
public static final UUID uuid = UUID.fromString("5f86dc81-2f59-486b-8aa9-3ab5486313c4");
|
|
|
|
private static boolean skipTest = false;
|
|
|
|
@BeforeClass
|
|
public static void beforeClass() throws Exception {
|
|
setContext(DEFAULT_TEST_SCOPE);
|
|
}
|
|
|
|
public static void setContext(String token) throws ObjectNotFound, Exception {
|
|
if (DEFAULT_TEST_SCOPE.isEmpty()) {
|
|
skipTest = true;
|
|
return;
|
|
}
|
|
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() {
|
|
if (skipTest)
|
|
return;
|
|
Context newContext = new ContextImpl(contextName, uuid);
|
|
System.out.println(newContext.getHeader().getUUID().toString());
|
|
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);
|
|
if (create.getStatusInfo() == Status.OK) {
|
|
String message = create.readEntity(String.class);
|
|
System.out.println("RM says: " + message);
|
|
}else {
|
|
assertEquals("Unexpected returned code. Reason: " + create.getStatusInfo().getReasonPhrase(),
|
|
Status.NOT_ACCEPTABLE.getStatusCode(), create.getStatus());
|
|
SerializableErrorCode code = create.readEntity(SerializableErrorCode.class);
|
|
RMCreateContextCode realCode = CodeFinder.findAndConvert(code, RMCreateContextCode.values());
|
|
assertEquals(RMCreateContextCode.CONTEXT_ALREADY_EXISTS, realCode);
|
|
}
|
|
|
|
} 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() {
|
|
if (skipTest)
|
|
return;
|
|
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);
|
|
}
|
|
}
|
|
|
|
}
|