295 lines
11 KiB
Java
295 lines
11 KiB
Java
package org.gcube.resourcemanagement.manager.webapp.context;
|
|
|
|
import static org.junit.Assert.*;
|
|
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.util.Objects;
|
|
import java.util.Properties;
|
|
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.informationsystem.resourceregistry.context.ResourceRegistryContextClient;
|
|
import org.gcube.informationsystem.resourceregistry.context.ResourceRegistryContextClientImpl;
|
|
import org.gcube.resourcemanagement.manager.io.codeexceptions.CodeFinder;
|
|
import org.gcube.resourcemanagement.manager.io.codeexceptions.SerializableErrorEntity;
|
|
import org.gcube.resourcemanagement.manager.io.codeexceptions.deserializer.ErrorEntityManager;
|
|
import org.gcube.resourcemanagement.manager.io.rs.RMCreateContextCode;
|
|
import org.gcube.resourcemanagement.manager.io.rs.RMContextDoesNotExistException;
|
|
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 org.omg.IOP.ExceptionDetailMessage;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
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 context1 = "ContextA";
|
|
|
|
private final static String context2 = "ContextB";
|
|
|
|
private final static String context3 = "ContextC";
|
|
|
|
public static final UUID context1UUID = UUID.fromString("5f86dc81-2f59-486b-8aa9-3ab5486313c4");
|
|
|
|
public static final UUID context2UUID = UUID.fromString("6f86dc81-2f59-486b-8aa9-3ab5486313c4");
|
|
|
|
public static final UUID context3UUID = UUID.fromString("7f86dc81-2f59-486b-8aa9-3ab5486313c4");
|
|
|
|
private final static String RR = "http://manuele-registry.dev.d4science.org/resource-registry";
|
|
|
|
public static String DEFAULT_TEST_SCOPE ="";
|
|
|
|
private static boolean skipTest = false;
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(RMContextTest.class);
|
|
|
|
static {
|
|
Properties properties = new Properties();
|
|
try (InputStream input = RMContextTest.class.getClassLoader().getResourceAsStream("token.properties")) {
|
|
// load the properties file
|
|
properties.load(input);
|
|
} catch (IOException e) {
|
|
skipTest = true;
|
|
//throw new RuntimeException(e);
|
|
}
|
|
|
|
DEFAULT_TEST_SCOPE = properties.getProperty("DEFAULT_SCOPE_TOKEN");
|
|
if (DEFAULT_TEST_SCOPE.isEmpty())
|
|
skipTest = true;
|
|
}
|
|
|
|
@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(context1);
|
|
newContext.getHeader().setUUID(context1UUID);
|
|
try {
|
|
Context uContext = ISMapper.unmarshal(Context.class, ISMapper.marshal(newContext));
|
|
assertNotNull(uContext);
|
|
assertEquals("Invalid context name", context1, uContext.getName());
|
|
assertEquals("Invalid context UUID", context1UUID, uContext.getHeader().getUUID());
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
assertFalse("Failed to unmarshal the context.", false);
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* Test method for
|
|
* {@link org.gcube.resourcemanagement.manager.webapp.rs.RMContext#create(String)}.
|
|
* @throws Exception
|
|
*/
|
|
@Test(expected = RMContextDoesNotExistException.class)
|
|
public void step1_Create() throws Exception {
|
|
if (skipTest)
|
|
return;
|
|
Context newContext = new ContextImpl(context1, context1UUID);
|
|
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);
|
|
logger.info("RM returned status: " + create.getStatusInfo() );
|
|
if (create.getStatusInfo().getStatusCode() == Status.CREATED.getStatusCode()) {
|
|
String message = create.readEntity(String.class);
|
|
logger.info("RM says: " + message);
|
|
}else {
|
|
assertEquals("Unexpected returned code. Reason: " + create.getStatusInfo().getReasonPhrase(),
|
|
Status.NOT_ACCEPTABLE.getStatusCode(), create.getStatus());
|
|
SerializableErrorEntity entity = create.readEntity(SerializableErrorEntity.class);
|
|
//try {
|
|
logger.info("about to load " + entity.getExceptionClass());
|
|
ErrorEntityManager response = new ErrorEntityManager(entity);
|
|
if (response.hasException()) {
|
|
try {
|
|
throw response.getException();
|
|
} catch (Exception e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
throw e;
|
|
}
|
|
}
|
|
}
|
|
|
|
} 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 step2_CreateWithInvalidParent() {
|
|
if (skipTest)
|
|
return;
|
|
Context newContext = new ContextImpl(context2, context2UUID);
|
|
newContext.setParent(new ContextImpl("DoNotExist"));
|
|
try {
|
|
Response create = target(RMContextPath.CONTEXT_ROOT).queryParam(RMContextPath.FORCE_RRURL_PARAM, RR).request()
|
|
.post(Entity.entity(ISMapper.marshal(newContext), MediaType.APPLICATION_JSON + ";charset=UTF-8"));
|
|
assertEquals("Unexpected returned code. Reason: " + create.getStatusInfo().getReasonPhrase(),
|
|
Status.NOT_ACCEPTABLE.getStatusCode(), create.getStatus());
|
|
SerializableErrorEntity code = create.readEntity(SerializableErrorEntity.class);
|
|
//RMCreateContextCode realCode = CodeFinder.findAndConvert(code.getErrorCode(), RMCreateContextCode.values());
|
|
//assertEquals(RMCreateContextCode.CONTEXT_PARENT_DOES_NOT_EXIST, 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 step3_CreateWithParent() {
|
|
if (skipTest)
|
|
return;
|
|
ResourceRegistryContextClient client = new ResourceRegistryContextClientImpl(RR);
|
|
Context newContext = new ContextImpl(context2, context2UUID);
|
|
Queries queries = new Queries();
|
|
newContext.setParent(context1UUID);
|
|
try {
|
|
Response create = target(RMContextPath.CONTEXT_ROOT).queryParam(RMContextPath.FORCE_RRURL_PARAM, RR).request()
|
|
.post(Entity.entity(ISMapper.marshal(newContext), MediaType.APPLICATION_JSON + ";charset=UTF-8"));
|
|
assertNotNull(create);
|
|
if (create.getStatusInfo().getStatusCode() == Status.CREATED.getStatusCode()) {
|
|
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());
|
|
SerializableErrorEntity code = create.readEntity(SerializableErrorEntity.class);
|
|
//RMCreateContextCode realCode = CodeFinder.findAndConvert(code.getErrorCode(), 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#delete(String, String)}
|
|
* @throws Exception
|
|
*/
|
|
@Test
|
|
public void step4_Delete() throws Exception {
|
|
if (skipTest)
|
|
return;
|
|
|
|
Response delete = target(RMContextPath.CONTEXT_ROOT).path(context2UUID.toString())
|
|
.queryParam(RMContextPath.FORCE_RRURL_PARAM, RR)
|
|
.request().delete();
|
|
assertNotNull(delete);
|
|
if (delete.getStatusInfo().getStatusCode() == Status.OK.getStatusCode()) {
|
|
|
|
}else {
|
|
assertEquals("Unexpected returned code. Reason: " + delete.getStatusInfo().getReasonPhrase(),
|
|
Status.NOT_ACCEPTABLE.getStatusCode(), delete.getStatus());
|
|
SerializableErrorEntity entity = delete.readEntity(SerializableErrorEntity.class);
|
|
ErrorEntityManager response = new ErrorEntityManager(entity);
|
|
if (response.hasException()) {
|
|
try {
|
|
throw response.getException();
|
|
} catch (Exception e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
throw e;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* Test method for
|
|
* {@link org.gcube.resourcemanagement.manager.webapp.rs.RMContext#delete(String, String)}
|
|
*/
|
|
@Test
|
|
public void step5_DeleteParent() {
|
|
if (skipTest)
|
|
return;
|
|
try {
|
|
Response delete = target(RMContextPath.CONTEXT_ROOT).path(context1UUID.toString())
|
|
.queryParam(RMContextPath.FORCE_RRURL_PARAM, RR)
|
|
.request().delete();
|
|
assertNotNull(delete);
|
|
assertEquals("Unexpected returned code. Reason: " + delete.getStatusInfo().getReasonPhrase(),
|
|
Status.OK.getStatusCode(), delete.getStatus());
|
|
} catch (Exception e) {
|
|
assertFalse("Failed to delete the context.", false);
|
|
}
|
|
}
|
|
}
|