Added/fixed tests

This commit is contained in:
Luca Frosini 2021-12-21 13:51:37 +01:00
parent 12275c8e73
commit 4b1be3b2d4
3 changed files with 311 additions and 43 deletions

View File

@ -10,6 +10,8 @@ import org.gcube.common.authorization.utils.secret.GCubeSecret;
import org.gcube.common.authorization.utils.secret.Secret;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
@ -17,6 +19,8 @@ import org.junit.BeforeClass;
*/
public class ContextTest {
private static Logger logger = LoggerFactory.getLogger(ContextTest.class);
protected static Properties properties;
protected static final String PROPERTIES_FILENAME = "token.properties";
@ -24,8 +28,9 @@ public class ContextTest {
public static final String GCAT_URL_PROPERTY = "GCAT_URL_PROPERTY";
public static String GCAT_URL;
public static final String DEFAULT_TEST_CONTEXT_NAME;
public static final String ROOT;
public static final String VO;
public static final String VRE;
static {
properties = new Properties();
@ -37,15 +42,10 @@ public class ContextTest {
} catch(IOException e) {
throw new RuntimeException(e);
}
//DEFAULT_TEST_CONTEXT_NAME = "/pred4s/preprod/preVRE";
DEFAULT_TEST_CONTEXT_NAME = "/gcube/devsec/devVRE";
try {
setContextByName(DEFAULT_TEST_CONTEXT_NAME);
} catch(Exception e) {
throw new RuntimeException(e);
}
ROOT = "/gcube";
VO = ROOT + "/devsec";
VRE = VO + "/devVRE";
Properties gcatProperties = new Properties();
input = ContextTest.class.getClassLoader().getResourceAsStream(GCAT_PROPERTIES_FILENAME);
@ -70,6 +70,9 @@ public class ContextTest {
public static void set(Secret secret) throws Exception {
SecretManager.instance.get().reset();
secret.set();
String username = secret.getUsername();
String context = secret.getContext();
logger.debug("Set authorization for user {} in context {}", username, context);
}
public static void setContext(String token) throws Exception {
@ -94,7 +97,7 @@ public class ContextTest {
@BeforeClass
public static void beforeClass() throws Exception {
setContextByName(DEFAULT_TEST_CONTEXT_NAME);
setContextByName(VRE);
}
@AfterClass

View File

@ -1,6 +1,5 @@
package org.gcube.gcat.client;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
@ -28,7 +27,7 @@ public class ItemTest extends ContextTest {
private static Logger logger = LoggerFactory.getLogger(ItemTest.class);
private static final String NAME_KEY = "name";
private static final String NAME_VALUE = "00";
public static final String NAME_VALUE = "00";
private static final String TITLE_KEY = "title";
private static final String TITLE_VALUE = "00 Title";
@ -58,19 +57,7 @@ public class ItemTest extends ContextTest {
logger.debug("The items published in the organization corresponding to the VRE {} are {}", SecretManager.instance.get().getContext(), count);
}
@Ignore
@Test
public void completeTest() throws IOException {
ObjectMapper mapper = new ObjectMapper();
JavaType listType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, String.class);
JavaType licenseArrayType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, JsonNode.class);
License license = new License();
String licencesStrings = license.list();
logger.debug("Got licenses {}", licencesStrings);
List<JsonNode> licences = mapper.readValue(licencesStrings, licenseArrayType);
JsonNode licenseJsonNode = licences.get(0);
String licenseID = licenseJsonNode.get(ID_KEY).asText();
public String createItem(ObjectMapper mapper, String licenseID) throws Exception {
Map<String,Object> map = new HashMap<>();
map.put(NAME_KEY, NAME_VALUE);
@ -94,7 +81,42 @@ public class ItemTest extends ContextTest {
Item item = new Item();
String json = mapper.writeValueAsString(map);
logger.debug("Going to create {}", json);
item.create(json);
return item.create(json);
}
public String getLicenseID(ObjectMapper mapper) throws Exception {
JavaType licenseArrayType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, JsonNode.class);
License license = new License();
String licencesStrings = license.list();
logger.debug("Got licenses {}", licencesStrings);
List<JsonNode> licences = mapper.readValue(licencesStrings, licenseArrayType);
JsonNode licenseJsonNode = licences.get(0);
String licenseID = licenseJsonNode.get(ID_KEY).asText();
return licenseID;
}
public String createItem() throws Exception {
ObjectMapper mapper = new ObjectMapper();
String licenseID = getLicenseID(mapper);
return createItem(mapper, licenseID);
}
public void deleteItem(boolean purge) throws Exception {
Item item = new Item();
item.delete(NAME_VALUE, purge);
}
// @Ignore
@Test
public void completeTest() throws Exception {
ObjectMapper mapper = new ObjectMapper();
JavaType listType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, String.class);
String licenseID = getLicenseID(mapper);
createItem(mapper, licenseID);
Item item = new Item();
try {
String itemsString = item.list(10, 0);
logger.debug("Got items {}", itemsString);
@ -133,30 +155,34 @@ public class ItemTest extends ContextTest {
String itemsString = item.list(10, 0);
logger.debug("Got list", itemsString);
List<String> items = mapper.readValue(itemsString, listType);
String name = items.get(0);
Assert.assertTrue(name.compareTo(NAME_VALUE) != 0);
if(items.size()>0) {
String name = items.get(0);
Assert.assertTrue(name.compareTo(NAME_VALUE) != 0);
}
}
// @Ignore
@Test
public void listOnVOTest() throws Exception {
ContextTest.setContextByName("/gcube/devNext");
ContextTest.setContextByName(VO);
Item item = new Item();
String itemsString = item.list(10, 0, "dorne");
String itemsString = item.list(10, 0, "devvre");
logger.debug("Got list {}", itemsString);
/*
Map<String,String> queryParams = new HashMap<>();
queryParams.put(GCatConstants.Q_KEY, "organization:dorne OR organization:ckand4scienceharvest");
queryParams.put(GCatConstants.Q_KEY, "organization:org1 OR organization:org2");
itemsString = item.list(queryParams);
logger.debug("Got list {}", itemsString);
queryParams = new HashMap<>();
queryParams.put(GCatConstants.Q_KEY, "organization:ckand4scienceharvest");
queryParams.put(GCatConstants.Q_KEY, "organization:org3");
queryParams.put("rows", "4");
queryParams.put("start", "4");
itemsString = item.list(queryParams);
logger.debug("Got list {}", itemsString);
*/
/*
ObjectMapper mapper = new ObjectMapper();
@ -175,14 +201,16 @@ public class ItemTest extends ContextTest {
logger.debug("Got list {}", itemsString);
List<String> items = mapper.readValue(itemsString, listType);
String name = items.get(0);
String ret = item.read(name);
logger.debug("Got item {}", ret);
JsonNode jsonNode = mapper.readTree(ret);
String gotName = jsonNode.get(NAME_KEY).asText();
Assert.assertTrue(name.compareTo(gotName) == 0);
if(items.size()>0) {
String name = items.get(0);
String ret = item.read(name);
logger.debug("Got item {}", ret);
JsonNode jsonNode = mapper.readTree(ret);
String gotName = jsonNode.get(NAME_KEY).asText();
Assert.assertTrue(name.compareTo(gotName) == 0);
}
}
protected static final String SEARCHABLE_KEY = "searchable";

View File

@ -0,0 +1,237 @@
package org.gcube.gcat.client;
import java.util.concurrent.TimeUnit;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI-CNR)
*/
public class TrashTest extends ContextTest {
private static Logger logger = LoggerFactory.getLogger(TrashTest.class);
protected boolean find(ArrayNode ids, String name) {
boolean found = false;
for (int i = 0; i < ids.size(); i++) {
if (name.compareTo(ids.get(i).asText()) == 0) {
found = true;
break;
}
}
return found;
}
public ItemTest cleanEnv() throws Exception {
// Cleanign the env
ContextTest.setContextByName(VRE);
ItemTest itemTest = new ItemTest();
try {
itemTest.deleteItem(true);
} catch (Exception e) {
// It is expected. the env was clean
}
return itemTest;
}
@Test
public void testList() throws Exception {
ObjectMapper mapper = new ObjectMapper();
ItemTest itemTest = cleanEnv();
ContextTest.setContextByName(VRE);
itemTest.createItem();
itemTest.deleteItem(false);
Trash trash = new Trash();
String jsonArray = trash.list(true);
ArrayNode ids = (ArrayNode) mapper.readTree(jsonArray);
logger.debug("{}", ids);
boolean found = find(ids, ItemTest.NAME_VALUE);
Assert.assertTrue(found);
jsonArray = trash.list(false);
ids = (ArrayNode) mapper.readTree(jsonArray);
logger.debug("{}", ids);
found = find(ids, ItemTest.NAME_VALUE);
Assert.assertTrue(found);
itemTest.deleteItem(true);
jsonArray = trash.list(true);
ids = (ArrayNode) mapper.readTree(jsonArray);
logger.debug("{}", ids);
found = find(ids, ItemTest.NAME_VALUE);
Assert.assertTrue(!found);
}
@Test
public void testListAndEmptyTrash() throws Exception {
ObjectMapper mapper = new ObjectMapper();
ItemTest itemTest = cleanEnv();
ContextTest.setContextByName(VRE);
itemTest.createItem();
itemTest.deleteItem(false);
Trash trash = new Trash();
String jsonArray = trash.list(true);
ArrayNode ids = (ArrayNode) mapper.readTree(jsonArray);
logger.debug("{}", ids);
boolean found = find(ids, ItemTest.NAME_VALUE);
Assert.assertTrue(found);
jsonArray = trash.list(false);
ids = (ArrayNode) mapper.readTree(jsonArray);
logger.debug("{}", ids);
found = find(ids, ItemTest.NAME_VALUE);
Assert.assertTrue(found);
trash.empty(true);
Thread.sleep(TimeUnit.SECONDS.toMillis(1));
jsonArray = trash.list(true);
ids = (ArrayNode) mapper.readTree(jsonArray);
logger.debug("{}", ids);
found = find(ids, ItemTest.NAME_VALUE);
Assert.assertTrue(!found);
trash.empty(false);
Thread.sleep(TimeUnit.SECONDS.toMillis(1));
jsonArray = trash.list(false);
ids = (ArrayNode) mapper.readTree(jsonArray);
logger.debug("{}", ids);
Assert.assertTrue(ids.size()==0);
}
public static final String NON_CATALOGUE_ADMIN_USER = "lucio.lelii";
@Test
public void testListFromAdmin() throws Exception {
ObjectMapper mapper = new ObjectMapper();
ItemTest itemTest = cleanEnv();
ContextTest.setContextByName(NON_CATALOGUE_ADMIN_USER + "_" + VRE);
itemTest = new ItemTest();
String ret = itemTest.createItem();
logger.debug(ret);
itemTest.deleteItem(false);
// I'm admin
ContextTest.setContextByName(VRE);
Trash trash = new Trash();
String jsonArray = trash.list(false);
ArrayNode ids = (ArrayNode) mapper.readTree(jsonArray);
logger.debug("{}", ids);
boolean found = find(ids, ItemTest.NAME_VALUE);
Assert.assertTrue(found);
jsonArray = trash.list(true);
ids = (ArrayNode) mapper.readTree(jsonArray);
logger.debug("{}", ids);
found = find(ids, ItemTest.NAME_VALUE);
Assert.assertTrue(!found);
ContextTest.setContextByName(NON_CATALOGUE_ADMIN_USER + "_" + VRE);
itemTest.deleteItem(true);
// I'm admin
ContextTest.setContextByName(VRE);
jsonArray = trash.list(false);
ids = (ArrayNode) mapper.readTree(jsonArray);
logger.debug("{}", ids);
found = find(ids, ItemTest.NAME_VALUE);
Assert.assertTrue(!found);
}
@Test
public void testListFromNonAdmin() throws Exception {
ObjectMapper mapper = new ObjectMapper();
ItemTest itemTest = cleanEnv();
ContextTest.setContextByName(VRE);
itemTest.createItem();
itemTest.deleteItem(false);
// He is not admin
ContextTest.setContextByName(NON_CATALOGUE_ADMIN_USER + "_" + VRE);
Trash trash = new Trash();
String jsonArray = trash.list(false);
ArrayNode ids = (ArrayNode) mapper.readTree(jsonArray);
logger.debug("{}", ids);
boolean found = find(ids, ItemTest.NAME_VALUE);
Assert.assertTrue(!found);
jsonArray = trash.list(true);
ids = (ArrayNode) mapper.readTree(jsonArray);
logger.debug("{}", ids);
found = find(ids, ItemTest.NAME_VALUE);
Assert.assertTrue(!found);
ContextTest.setContextByName(VRE);
jsonArray = trash.list(false);
ids = (ArrayNode) mapper.readTree(jsonArray);
logger.debug("{}", ids);
found = find(ids, ItemTest.NAME_VALUE);
Assert.assertTrue(found);
jsonArray = trash.list(true);
ids = (ArrayNode) mapper.readTree(jsonArray);
logger.debug("{}", ids);
found = find(ids, ItemTest.NAME_VALUE);
Assert.assertTrue(found);
itemTest.deleteItem(true);
jsonArray = trash.list(false);
ids = (ArrayNode) mapper.readTree(jsonArray);
logger.debug("{}", ids);
found = find(ids, ItemTest.NAME_VALUE);
Assert.assertTrue(!found);
jsonArray = trash.list(true);
ids = (ArrayNode) mapper.readTree(jsonArray);
logger.debug("{}", ids);
found = find(ids, ItemTest.NAME_VALUE);
Assert.assertTrue(!found);
ContextTest.setContextByName(NON_CATALOGUE_ADMIN_USER + "_" + VRE);
jsonArray = trash.list(false);
ids = (ArrayNode) mapper.readTree(jsonArray);
logger.debug("{}", ids);
found = find(ids, ItemTest.NAME_VALUE);
Assert.assertTrue(!found);
jsonArray = trash.list(true);
ids = (ArrayNode) mapper.readTree(jsonArray);
logger.debug("{}", ids);
found = find(ids, ItemTest.NAME_VALUE);
Assert.assertTrue(!found);
}
}