Skip to content
This repository has been archived by the owner on Feb 23, 2018. It is now read-only.

Commit

Permalink
#15 unit tests of specific query
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-conway committed Nov 2, 2017
1 parent b08149c commit a588f72
Show file tree
Hide file tree
Showing 4 changed files with 293 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -344,4 +344,19 @@ public IRODSServices getIrodsServices() {
public void setIrodsServices(IRODSServices irodsServices) {
this.irodsServices = irodsServices;
}

/**
* @return the adminServices
*/
public AdminServices getAdminServices() {
return adminServices;
}

/**
* @param adminServices
* the adminServices to set
*/
public void setAdminServices(AdminServices adminServices) {
this.adminServices = adminServices;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
package com.emc.metalnx.services.irods;

import static org.junit.Assert.fail;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.irods.jargon.core.connection.IRODSAccount;
import org.irods.jargon.core.pub.CollectionAO;
import org.irods.jargon.core.pub.DataObjectAO;
import org.irods.jargon.core.pub.DataTransferOperations;
import org.irods.jargon.core.pub.EnvironmentalInfoAO;
import org.irods.jargon.core.pub.IRODSFileSystem;
import org.irods.jargon.core.pub.SpecificQueryAO;
import org.irods.jargon.core.pub.domain.AvuData;
import org.irods.jargon.core.pub.io.IRODSFile;
import org.irods.jargon.core.pub.io.IRODSFileFactory;
import org.irods.jargon.core.query.SpecificQueryResultSet;
import org.irods.jargon.core.utils.MiscIRODSUtils;
import org.irods.jargon.testutils.IRODSTestSetupUtilities;
import org.irods.jargon.testutils.TestingPropertiesHelper;
import org.irods.jargon.testutils.filemanip.FileGenerator;
import org.irods.jargon.testutils.filemanip.ScratchFileUtils;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.Mockito;

import com.emc.metalnx.core.domain.entity.DataGridMetadataSearch;
import com.emc.metalnx.core.domain.entity.enums.DataGridSearchOperatorEnum;
import com.emc.metalnx.services.interfaces.AdminServices;
import com.emc.metalnx.services.interfaces.IRODSServices;

import junit.framework.Assert;

public class SpecQueryServiceImplTest {

private static Properties testingProperties = new Properties();
private static TestingPropertiesHelper testingPropertiesHelper = new TestingPropertiesHelper();
private static ScratchFileUtils scratchFileUtils = null;
public static final String IRODS_TEST_SUBDIR_PATH = "SpecQueryServiceImplTest";
private static IRODSTestSetupUtilities irodsTestSetupUtilities = null;
private static IRODSFileSystem irodsFileSystem;
public static String rootCollPathInIrods;

public static final String COLL_AVU_ATTR1 = "specQueryColl1";
public static final String COLL_AVU_VAL1 = "specQueryCollVal1";

public static final String COLL_AVU_ATTR2 = "specQueryColl2";
public static final String COLL_AVU_VAL2 = "specQueryCollVal2";

public static final String DATA_AVU_ATTR1 = "specQueryData1";
public static final String DATA_AVU_VAL1 = "specQueryDataVal1";

public static final String DATA_AVU_ATTR2 = "specQueryData2";
public static final String DATA_AVU_VAL2 = "specQueryDataVal2";

@BeforeClass
public static void setUpBeforeClass() throws Exception {
TestingPropertiesHelper testingPropertiesLoader = new TestingPropertiesHelper();
testingProperties = testingPropertiesLoader.getTestProperties();
scratchFileUtils = new ScratchFileUtils(testingProperties);
scratchFileUtils.clearAndReinitializeScratchDirectory(IRODS_TEST_SUBDIR_PATH);
irodsTestSetupUtilities = new IRODSTestSetupUtilities();
irodsTestSetupUtilities.initializeIrodsScratchDirectory();
irodsTestSetupUtilities.initializeDirectoryForTest(IRODS_TEST_SUBDIR_PATH);
irodsFileSystem = IRODSFileSystem.instance();

// set up test structure

String rootCollection = "SpecQueryServiceImplTestRoot";
String localCollectionAbsolutePath = scratchFileUtils
.createAndReturnAbsoluteScratchPath(IRODS_TEST_SUBDIR_PATH + '/' + rootCollection);

IRODSAccount irodsAccount = testingPropertiesHelper.buildIRODSAccountFromTestProperties(testingProperties);
String irodsCollectionRootAbsolutePath = (MiscIRODSUtils.computeHomeDirectoryForIRODSAccount(irodsAccount) + '/'
+ rootCollection);
rootCollPathInIrods = irodsCollectionRootAbsolutePath;

FileGenerator.generateManyFilesAndCollectionsInParentCollectionByAbsolutePath(localCollectionAbsolutePath,
"specQueryTset", 1, 2, 3, "testFile", ".txt", 4, 3, 2, 30000);

IRODSFileFactory irodsFileFactory = irodsFileSystem.getIRODSFileFactory(irodsAccount);
IRODSFile destFile = irodsFileFactory.instanceIRODSFile(irodsCollectionRootAbsolutePath);
destFile.deleteWithForceOption();
destFile.mkdirs();
DataTransferOperations dataTransferOperationsAO = irodsFileSystem.getIRODSAccessObjectFactory()
.getDataTransferOperations(irodsAccount);
DataObjectAO dataObjectAO = irodsFileSystem.getIRODSAccessObjectFactory().getDataObjectAO(irodsAccount);
CollectionAO collectionAO = irodsFileSystem.getIRODSAccessObjectFactory().getCollectionAO(irodsAccount);
File localFile = new File(localCollectionAbsolutePath);

dataTransferOperationsAO.putOperation(localFile, destFile, null, null);

// Decorate with various AVUs to use in tests

decorateChildren(destFile, collectionAO, dataObjectAO);
}

public static void decorateChildren(final IRODSFile file, final CollectionAO collectionAO,
final DataObjectAO dataObjectAO) throws Exception {
IRODSFile childIrods;
int i = 0;
AvuData dataToAdd;
for (File child : file.listFiles()) {
childIrods = (IRODSFile) child;
if (childIrods.isDirectory()) {
if (i % 2 == 0) {
dataToAdd = AvuData.instance(COLL_AVU_ATTR1, COLL_AVU_VAL1, "");
} else {
dataToAdd = AvuData.instance(COLL_AVU_ATTR2, COLL_AVU_VAL2, "");
}
collectionAO.addAVUMetadata(childIrods.getAbsolutePath(), dataToAdd);
decorateChildren(childIrods, collectionAO, dataObjectAO);
} else {
if (i % 2 == 0) {
dataToAdd = AvuData.instance(DATA_AVU_ATTR1, DATA_AVU_VAL1, "");
} else {
dataToAdd = AvuData.instance(DATA_AVU_ATTR2, DATA_AVU_VAL2, "");
}
dataObjectAO.addAVUMetadata(childIrods.getAbsolutePath(), dataToAdd);
}
}
}

@AfterClass
public static void tearDownAfterClass() throws Exception {
irodsFileSystem.closeAndEatExceptions();
}

@After
public void afterEach() throws Exception {
irodsFileSystem.closeAndEatExceptions();
}

@Test
public void testCountCollectionsMatchingMetadata() throws Exception {

SpecQueryServiceImpl specQueryService = new SpecQueryServiceImpl();
IRODSServices irodsService = Mockito.mock(IRODSServices.class);
AdminServices adminServices = Mockito.mock(AdminServices.class);

IRODSAccount irodsAccount = testingPropertiesHelper.buildIRODSAccountFromTestProperties(testingProperties);

EnvironmentalInfoAO environmentalInfoAO = irodsFileSystem.getIRODSAccessObjectFactory()
.getEnvironmentalInfoAO(irodsAccount);
SpecificQueryAO specificQueryAO = irodsFileSystem.getIRODSAccessObjectFactory()
.getSpecificQueryAO(irodsAccount);

Mockito.when(irodsService.getEnvironmentalInfoAO()).thenReturn(environmentalInfoAO);
Mockito.when(adminServices.getSpecificQueryAO()).thenReturn(specificQueryAO);

specQueryService.setIrodsServices(irodsService);
specQueryService.setAdminServices(adminServices);
List<DataGridMetadataSearch> metadataSearch = new ArrayList<DataGridMetadataSearch>();
DataGridMetadataSearch search = new DataGridMetadataSearch(COLL_AVU_ATTR1, COLL_AVU_VAL1, "",
DataGridSearchOperatorEnum.EQUAL);
metadataSearch.add(search);
int count = specQueryService.countCollectionsMatchingMetadata(metadataSearch, irodsAccount.getZone());
Assert.assertTrue("no recs returned", count > 1);

}

@Test
public void testCountDataObjectsMatchingMetadata() throws Exception {
SpecQueryServiceImpl specQueryService = new SpecQueryServiceImpl();
IRODSServices irodsService = Mockito.mock(IRODSServices.class);
AdminServices adminServices = Mockito.mock(AdminServices.class);

IRODSAccount irodsAccount = testingPropertiesHelper.buildIRODSAccountFromTestProperties(testingProperties);

EnvironmentalInfoAO environmentalInfoAO = irodsFileSystem.getIRODSAccessObjectFactory()
.getEnvironmentalInfoAO(irodsAccount);
SpecificQueryAO specificQueryAO = irodsFileSystem.getIRODSAccessObjectFactory()
.getSpecificQueryAO(irodsAccount);

Mockito.when(irodsService.getEnvironmentalInfoAO()).thenReturn(environmentalInfoAO);
Mockito.when(adminServices.getSpecificQueryAO()).thenReturn(specificQueryAO);

specQueryService.setIrodsServices(irodsService);
specQueryService.setAdminServices(adminServices);
List<DataGridMetadataSearch> metadataSearch = new ArrayList<DataGridMetadataSearch>();
DataGridMetadataSearch search = new DataGridMetadataSearch(DATA_AVU_ATTR1, DATA_AVU_VAL1, "",
DataGridSearchOperatorEnum.EQUAL);
metadataSearch.add(search);
int count = specQueryService.countDataObjectsMatchingMetadata(metadataSearch, irodsAccount.getZone());
Assert.assertTrue("no recs returned", count > 1);
}

@Test
public void testSearchByMetadataDataObjects() throws Exception {
SpecQueryServiceImpl specQueryService = new SpecQueryServiceImpl();
IRODSServices irodsService = Mockito.mock(IRODSServices.class);
AdminServices adminServices = Mockito.mock(AdminServices.class);

IRODSAccount irodsAccount = testingPropertiesHelper.buildIRODSAccountFromTestProperties(testingProperties);

EnvironmentalInfoAO environmentalInfoAO = irodsFileSystem.getIRODSAccessObjectFactory()
.getEnvironmentalInfoAO(irodsAccount);
SpecificQueryAO specificQueryAO = irodsFileSystem.getIRODSAccessObjectFactory()
.getSpecificQueryAO(irodsAccount);

Mockito.when(irodsService.getEnvironmentalInfoAO()).thenReturn(environmentalInfoAO);
Mockito.when(adminServices.getSpecificQueryAO()).thenReturn(specificQueryAO);

specQueryService.setIrodsServices(irodsService);
specQueryService.setAdminServices(adminServices);
List<DataGridMetadataSearch> metadataSearch = new ArrayList<DataGridMetadataSearch>();
DataGridMetadataSearch search = new DataGridMetadataSearch(DATA_AVU_ATTR1, DATA_AVU_VAL1, "",
DataGridSearchOperatorEnum.EQUAL);
metadataSearch.add(search);
SpecificQueryResultSet result = specQueryService.searchByMetadata(metadataSearch, irodsAccount.getZone(), false,
null, 0, 0);
Assert.assertFalse("no result", result.getResults().isEmpty());
}

@Test
public void testSearchByMetadataCollections() throws Exception {
SpecQueryServiceImpl specQueryService = new SpecQueryServiceImpl();
IRODSServices irodsService = Mockito.mock(IRODSServices.class);
AdminServices adminServices = Mockito.mock(AdminServices.class);

IRODSAccount irodsAccount = testingPropertiesHelper.buildIRODSAccountFromTestProperties(testingProperties);

EnvironmentalInfoAO environmentalInfoAO = irodsFileSystem.getIRODSAccessObjectFactory()
.getEnvironmentalInfoAO(irodsAccount);
SpecificQueryAO specificQueryAO = irodsFileSystem.getIRODSAccessObjectFactory()
.getSpecificQueryAO(irodsAccount);

Mockito.when(irodsService.getEnvironmentalInfoAO()).thenReturn(environmentalInfoAO);
Mockito.when(adminServices.getSpecificQueryAO()).thenReturn(specificQueryAO);

specQueryService.setIrodsServices(irodsService);
specQueryService.setAdminServices(adminServices);
List<DataGridMetadataSearch> metadataSearch = new ArrayList<DataGridMetadataSearch>();
DataGridMetadataSearch search = new DataGridMetadataSearch(COLL_AVU_ATTR1, COLL_AVU_VAL1, "",
DataGridSearchOperatorEnum.EQUAL);
metadataSearch.add(search);
SpecificQueryResultSet result = specQueryService.searchByMetadata(metadataSearch, irodsAccount.getZone(), true,
null, 0, 0);
Assert.assertFalse("no result", result.getResults().isEmpty());
}

@Test
public void testSearchByFileProperties() {
fail("Not yet implemented");
}

@Test
public void testCountCollectionsMatchingFileProperties() {
fail("Not yet implemented");
}

@Test
public void testCountDataObjectsMatchingFileProperties() {
fail("Not yet implemented");
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.emc.metalnx.controller;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
Expand Down Expand Up @@ -116,7 +117,7 @@ public String index(Model model, HttpServletRequest request,
public String search(
@RequestParam(value = "jsonFilePropertySearch", required = false) String jsonFilePropertySearch,
@RequestParam("draw") int draw, @RequestParam("start") int start, @RequestParam("length") int length)
throws DataGridConnectionRefusedException {
throws DataGridConnectionRefusedException, JargonException {

if (jsonFilePropertySearch != null) {
currentPage = (int) (Math.floor(start / length) + 1);
Expand Down Expand Up @@ -156,15 +157,28 @@ public String search(
jsonResponse.put("data", dataGridCollectionAndDataObjects);

} catch (DataGridConnectionRefusedException e) {
logger.error("data grid error in search", e);
throw e;
} catch (Exception e) {
} catch (JargonException e) {
logger.error("Could not search by metadata: ", e.getMessage());
throw e;
} catch (ParseException e) {
logger.error("Could not search by metadata: ", e.getMessage());
throw new JargonException(e);
} catch (JsonProcessingException e) {
logger.error("Could not search by metadata: ", e.getMessage());
throw new JargonException(e);
} catch (IOException e) {
logger.error("Could not search by metadata: ", e.getMessage());
throw new JargonException(e);
}

try {
jsonString = mapper.writeValueAsString(jsonResponse);
} catch (JsonProcessingException e) {
logger.error("Could not parse hashmap in file properties search to json: {}", e.getMessage());
throw new JargonException(e);

}

return jsonString;
Expand Down

0 comments on commit a588f72

Please sign in to comment.