Added test of context state

This commit is contained in:
Luca Frosini 2024-11-08 15:32:30 +01:00
parent f874c01d7a
commit 244cec6854
3 changed files with 108 additions and 4 deletions

View File

@ -0,0 +1,35 @@
package org.gcube.informationsystem.resourceregistry.authorization;
import java.io.File;
import java.net.URL;
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.gcube.common.authorization.utils.secret.Secret;
import org.gcube.informationsystem.resourceregistry.ContextTest;
import org.gcube.informationsystem.resourceregistry.queries.JsonQueryTest;
public class FakeSecretUtility {
public static File getFakeUsersDirectory() throws Exception {
URL logbackFileURL = JsonQueryTest.class.getClassLoader().getResource("logback-test.xml");
File logbackFile = new File(logbackFileURL.toURI());
File resourcesDirectory = logbackFile.getParentFile();
return new File(resourcesDirectory, "fake-users");
}
public static Secret getSecret(String filename, String context) throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
File file = new File(getFakeUsersDirectory(), filename);
JsonNode jsonNode = objectMapper.readTree(file);
String json = objectMapper.writeValueAsString(jsonNode);
FakeSecret secret = new FakeSecret(json, context);
return secret;
}
public static void setFakeSecret(String filename, String context) throws Exception {
Secret secret = getSecret(filename, context);
ContextTest.set(secret);
}
}

View File

@ -482,7 +482,7 @@ public class ContextManagementTest extends ContextTest {
// List<Context> contexts = ServerContextCache.getInstance().getContexts(); // List<Context> contexts = ServerContextCache.getInstance().getContexts();
List<Context> contexts = ServerContextCache.getInstance().getFullInfoContexts(); List<Context> contexts = ServerContextCache.getInstance().getFullInfoContexts();
for(Context context : contexts) { for(Context context : contexts) {
logger.info("{}", context.getName()); logger.info("{} {}", context.getName(), context.getID());
List<IsParentOf> childrenIsParentOf = context.getChildren(); List<IsParentOf> childrenIsParentOf = context.getChildren();
for(IsParentOf childIsParentOf : childrenIsParentOf) { for(IsParentOf childIsParentOf : childrenIsParentOf) {
Assert.assertTrue(childIsParentOf.getSource() == context); Assert.assertTrue(childIsParentOf.getSource() == context);
@ -502,10 +502,10 @@ public class ContextManagementTest extends ContextTest {
logger.debug("{}", context); logger.debug("{}", context);
} }
@Ignore //@Ignore
// @Test @Test
public void deleteContext() throws ResourceRegistryException, IOException { public void deleteContext() throws ResourceRegistryException, IOException {
Context context = read(UUID.fromString("")); Context context = read(UUID.fromString("5866f58d-fff2-4ae6-ae1a-a16727d44407"));
delete(context); delete(context);
} }

View File

@ -0,0 +1,69 @@
package org.gcube.informationsystem.resourceregistry.contexts;
import javax.ws.rs.ForbiddenException;
import org.gcube.informationsystem.contexts.impl.entities.ContextImpl;
import org.gcube.informationsystem.contexts.reference.entities.Context;
import org.gcube.informationsystem.model.reference.entities.Resource;
import org.gcube.informationsystem.resourceregistry.ContextTest;
import org.gcube.informationsystem.resourceregistry.authorization.FakeSecretUtility;
import org.gcube.informationsystem.resourceregistry.instances.ERManagementTest;
import org.gcube.informationsystem.resourceregistry.instances.model.entities.ResourceManagement;
import org.gcube.informationsystem.serialization.ElementMapper;
import org.gcube.informationsystem.tree.Tree;
import org.gcube.resourcemanagement.model.reference.entities.resources.EService;
import org.junit.Test;
public class ContextStateTest extends ContextManagementTest {
public <R extends Resource> ResourceManagement getResourceManagement(R r) throws Exception {
ResourceManagement resourceManagement = new ResourceManagement();
resourceManagement.setElementType(r.getTypeName());
resourceManagement.setJson(ElementMapper.marshal(r));
if (r.getID() != null) {
resourceManagement.setUUID(r.getID());
}
return resourceManagement;
}
@Test(expected = ForbiddenException.class)
public void testNonActiveContext() throws Exception {
ContextTest.setContextByName(GCUBE);
Tree<Context> tree = ServerContextCache.getInstance().getContextsTree();
Context root = tree.getRootNode().getNodeElement();
String rootContext = "/" + root.getName();
ContextTest.setContextByName(rootContext);
Context contextA1 = new ContextImpl(CTX_NAME_A);
contextA1.setParent(root);
try {
contextA1 = create(contextA1);
}catch (Exception e) {
ContextTest.setContextByName(rootContext);
delete(contextA1);
return;
}
FakeSecretUtility.setFakeSecret("noroles.json", rootContext+"/"+contextA1.getName());
EService eService = ERManagementTest.instantiateValidEService();
ResourceManagement resourceManagement = getResourceManagement(eService);
String json = null;
try {
json = resourceManagement.create();
}catch (Exception e) {
throw e;
} finally {
if(json!=null) {
resourceManagement.delete();
}
ContextTest.setContextByName(rootContext);
delete(contextA1);
}
}
}