code formatted

This commit is contained in:
Sandro La Bruzzo 2021-10-19 15:24:59 +02:00
parent f8329bc110
commit c9870c5122
3 changed files with 216 additions and 216 deletions

View File

@ -25,102 +25,102 @@ import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
@ExtendWith(MockitoExtension.class) @ExtendWith(MockitoExtension.class)
public class SolrConfigExploreTest extends SolrExploreTest { public class SolrConfigExploreTest extends SolrExploreTest {
protected static SparkSession spark; protected static SparkSession spark;
private static final Integer batchSize = 100; private static final Integer batchSize = 100;
@Mock @Mock
private ISLookUpService isLookUpService; private ISLookUpService isLookUpService;
@Mock @Mock
private ISLookupClient isLookupClient; private ISLookupClient isLookupClient;
@BeforeEach @BeforeEach
public void prepareMocks() throws ISLookUpException, IOException { public void prepareMocks() throws ISLookUpException, IOException {
isLookupClient.setIsLookup(isLookUpService); isLookupClient.setIsLookup(isLookUpService);
int solrPort = URI.create("http://" + miniCluster.getZkClient().getZkServerAddress()).getPort(); int solrPort = URI.create("http://" + miniCluster.getZkClient().getZkServerAddress()).getPort();
Mockito Mockito
.when(isLookupClient.getDsId(Mockito.anyString())) .when(isLookupClient.getDsId(Mockito.anyString()))
.thenReturn("313f0381-23b6-466f-a0b8-c72a9679ac4b_SW5kZXhEU1Jlc291cmNlcy9JbmRleERTUmVzb3VyY2VUeXBl"); .thenReturn("313f0381-23b6-466f-a0b8-c72a9679ac4b_SW5kZXhEU1Jlc291cmNlcy9JbmRleERTUmVzb3VyY2VUeXBl");
Mockito.when(isLookupClient.getZkHost()).thenReturn(String.format("127.0.0.1:%s/solr", solrPort)); Mockito.when(isLookupClient.getZkHost()).thenReturn(String.format("127.0.0.1:%s/solr", solrPort));
Mockito Mockito
.when(isLookupClient.getLayoutSource(Mockito.anyString())) .when(isLookupClient.getLayoutSource(Mockito.anyString()))
.thenReturn(IOUtils.toString(getClass().getResourceAsStream("fields.xml"))); .thenReturn(IOUtils.toString(getClass().getResourceAsStream("fields.xml")));
Mockito Mockito
.when(isLookupClient.getLayoutTransformer()) .when(isLookupClient.getLayoutTransformer())
.thenReturn(IOUtils.toString(getClass().getResourceAsStream("layoutToRecordTransformer.xsl"))); .thenReturn(IOUtils.toString(getClass().getResourceAsStream("layoutToRecordTransformer.xsl")));
} }
@BeforeAll @BeforeAll
public static void before() { public static void before() {
SparkConf conf = new SparkConf(); SparkConf conf = new SparkConf();
conf.setAppName(XmlIndexingJobTest.class.getSimpleName()); conf.setAppName(XmlIndexingJobTest.class.getSimpleName());
conf.registerKryoClasses(new Class[] { conf.registerKryoClasses(new Class[] {
SerializableSolrInputDocument.class SerializableSolrInputDocument.class
}); });
conf.setMaster("local[1]"); conf.setMaster("local[1]");
conf.set("spark.driver.host", "localhost"); conf.set("spark.driver.host", "localhost");
conf.set("hive.metastore.local", "true"); conf.set("hive.metastore.local", "true");
conf.set("spark.ui.enabled", "false"); conf.set("spark.ui.enabled", "false");
conf.set("spark.sql.warehouse.dir", workingDir.resolve("spark").toString()); conf.set("spark.sql.warehouse.dir", workingDir.resolve("spark").toString());
spark = SparkSession spark = SparkSession
.builder() .builder()
.appName(XmlIndexingJobTest.class.getSimpleName()) .appName(XmlIndexingJobTest.class.getSimpleName())
.config(conf) .config(conf)
.getOrCreate(); .getOrCreate();
}
} @AfterAll
public static void tearDown() {
spark.stop();
}
@AfterAll @Test
public static void tearDown() { public void testSolrConfig() throws Exception {
spark.stop();
}
@Test String inputPath = "src/test/resources/eu/dnetlib/dhp/oa/provision/xml";
public void testSolrConfig() throws Exception {
String inputPath = "src/test/resources/eu/dnetlib/dhp/oa/provision/xml"; new XmlIndexingJob(spark, inputPath, FORMAT, batchSize, XmlIndexingJob.OutputFormat.SOLR, null)
.run(isLookupClient);
Assertions.assertEquals(0, miniCluster.getSolrClient().commit().getStatus());
new XmlIndexingJob(spark, inputPath, FORMAT, batchSize, XmlIndexingJob.OutputFormat.SOLR, null).run(isLookupClient); String[] queryStrings = {
Assertions.assertEquals(0, miniCluster.getSolrClient().commit().getStatus()); "cancer",
"graph",
"graphs"
};
String[] queryStrings = { for (String q : queryStrings) {
"cancer", SolrQuery query = new SolrQuery();
"graph", query.setRequestHandler("/exploreSearch");
"graphs" query.add(CommonParams.Q, q);
}; query.set("debugQuery", "on");
for (String q : queryStrings) { log.info("Submit query to Solr with params: {}", query.toString());
SolrQuery query = new SolrQuery(); QueryResponse rsp = miniCluster.getSolrClient().query(query);
query.setRequestHandler("/exploreSearch");
query.add(CommonParams.Q, q);
query.set("debugQuery", "on");
log.info("Submit query to Solr with params: {}", query.toString());
QueryResponse rsp = miniCluster.getSolrClient().query(query);
// System.out.println(rsp.getHighlighting()); // System.out.println(rsp.getHighlighting());
// System.out.println(rsp.getExplainMap()); // System.out.println(rsp.getExplainMap());
for (SolrDocument doc : rsp.getResults()) { for (SolrDocument doc : rsp.getResults()) {
System.out.println( System.out
doc.get("score") + "\t" + .println(
doc.get("__indexrecordidentifier") + "\t" + doc.get("score") + "\t" +
doc.get("resultidentifier") + "\t" + doc.get("__indexrecordidentifier") + "\t" +
doc.get("resultauthor") + "\t" + doc.get("resultidentifier") + "\t" +
doc.get("resultacceptanceyear") + "\t" + doc.get("resultauthor") + "\t" +
doc.get("resultsubject") + "\t" + doc.get("resultacceptanceyear") + "\t" +
doc.get("resulttitle") + "\t" + doc.get("resultsubject") + "\t" +
doc.get("relprojectname") + "\t" + doc.get("resulttitle") + "\t" +
doc.get("resultdescription") + "\t" + doc.get("relprojectname") + "\t" +
doc.get("__all") + "\t" doc.get("resultdescription") + "\t" +
); doc.get("__all") + "\t");
} }
} }
} }
} }

View File

@ -34,98 +34,98 @@ import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
@ExtendWith(MockitoExtension.class) @ExtendWith(MockitoExtension.class)
public class SolrConfigTest extends SolrTest { public class SolrConfigTest extends SolrTest {
protected static SparkSession spark; protected static SparkSession spark;
private static final Integer batchSize = 100; private static final Integer batchSize = 100;
@Mock @Mock
private ISLookUpService isLookUpService; private ISLookUpService isLookUpService;
@Mock @Mock
private ISLookupClient isLookupClient; private ISLookupClient isLookupClient;
@BeforeEach @BeforeEach
public void prepareMocks() throws ISLookUpException, IOException { public void prepareMocks() throws ISLookUpException, IOException {
isLookupClient.setIsLookup(isLookUpService); isLookupClient.setIsLookup(isLookUpService);
int solrPort = URI.create("http://" + miniCluster.getZkClient().getZkServerAddress()).getPort(); int solrPort = URI.create("http://" + miniCluster.getZkClient().getZkServerAddress()).getPort();
Mockito Mockito
.when(isLookupClient.getDsId(Mockito.anyString())) .when(isLookupClient.getDsId(Mockito.anyString()))
.thenReturn("313f0381-23b6-466f-a0b8-c72a9679ac4b_SW5kZXhEU1Jlc291cmNlcy9JbmRleERTUmVzb3VyY2VUeXBl"); .thenReturn("313f0381-23b6-466f-a0b8-c72a9679ac4b_SW5kZXhEU1Jlc291cmNlcy9JbmRleERTUmVzb3VyY2VUeXBl");
Mockito.when(isLookupClient.getZkHost()).thenReturn(String.format("127.0.0.1:%s/solr", solrPort)); Mockito.when(isLookupClient.getZkHost()).thenReturn(String.format("127.0.0.1:%s/solr", solrPort));
Mockito Mockito
.when(isLookupClient.getLayoutSource(Mockito.anyString())) .when(isLookupClient.getLayoutSource(Mockito.anyString()))
.thenReturn(IOUtils.toString(getClass().getResourceAsStream("fields.xml"))); .thenReturn(IOUtils.toString(getClass().getResourceAsStream("fields.xml")));
Mockito Mockito
.when(isLookupClient.getLayoutTransformer()) .when(isLookupClient.getLayoutTransformer())
.thenReturn(IOUtils.toString(getClass().getResourceAsStream("layoutToRecordTransformer.xsl"))); .thenReturn(IOUtils.toString(getClass().getResourceAsStream("layoutToRecordTransformer.xsl")));
} }
@BeforeAll @BeforeAll
public static void before() { public static void before() {
SparkConf conf = new SparkConf(); SparkConf conf = new SparkConf();
conf.setAppName(XmlIndexingJobTest.class.getSimpleName()); conf.setAppName(XmlIndexingJobTest.class.getSimpleName());
conf.registerKryoClasses(new Class[] { conf.registerKryoClasses(new Class[] {
SerializableSolrInputDocument.class SerializableSolrInputDocument.class
}); });
conf.setMaster("local[1]"); conf.setMaster("local[1]");
conf.set("spark.driver.host", "localhost"); conf.set("spark.driver.host", "localhost");
conf.set("hive.metastore.local", "true"); conf.set("hive.metastore.local", "true");
conf.set("spark.ui.enabled", "false"); conf.set("spark.ui.enabled", "false");
conf.set("spark.sql.warehouse.dir", workingDir.resolve("spark").toString()); conf.set("spark.sql.warehouse.dir", workingDir.resolve("spark").toString());
spark = SparkSession spark = SparkSession
.builder() .builder()
.appName(XmlIndexingJobTest.class.getSimpleName()) .appName(XmlIndexingJobTest.class.getSimpleName())
.config(conf) .config(conf)
.getOrCreate(); .getOrCreate();
}
} @AfterAll
public static void tearDown() {
spark.stop();
}
@AfterAll @Test
public static void tearDown() { public void testSolrConfig() throws Exception {
spark.stop();
}
@Test String inputPath = "src/test/resources/eu/dnetlib/dhp/oa/provision/xml";
public void testSolrConfig() throws Exception {
String inputPath = "src/test/resources/eu/dnetlib/dhp/oa/provision/xml"; new XmlIndexingJob(spark, inputPath, FORMAT, batchSize, XmlIndexingJob.OutputFormat.SOLR, null)
.run(isLookupClient);
Assertions.assertEquals(0, miniCluster.getSolrClient().commit().getStatus());
new XmlIndexingJob(spark, inputPath, FORMAT, batchSize, XmlIndexingJob.OutputFormat.SOLR, null).run(isLookupClient); String[] queryStrings = {
Assertions.assertEquals(0, miniCluster.getSolrClient().commit().getStatus()); "cancer",
"graph",
"graphs"
};
String[] queryStrings = { for (String q : queryStrings) {
"cancer", SolrQuery query = new SolrQuery();
"graph", query.add(CommonParams.Q, q);
"graphs"
};
for (String q : queryStrings) { log.info("Submit query to Solr with params: {}", query.toString());
SolrQuery query = new SolrQuery(); QueryResponse rsp = miniCluster.getSolrClient().query(query);
query.add(CommonParams.Q, q);
log.info("Submit query to Solr with params: {}", query.toString()); for (SolrDocument doc : rsp.getResults()) {
QueryResponse rsp = miniCluster.getSolrClient().query(query); System.out
.println(
for (SolrDocument doc : rsp.getResults()) { doc.get("score") + "\t" +
System.out.println( doc.get("__indexrecordidentifier") + "\t" +
doc.get("score") + "\t" + doc.get("resultidentifier") + "\t" +
doc.get("__indexrecordidentifier") + "\t" + doc.get("resultauthor") + "\t" +
doc.get("resultidentifier") + "\t" + doc.get("resultacceptanceyear") + "\t" +
doc.get("resultauthor") + "\t" + doc.get("resultsubject") + "\t" +
doc.get("resultacceptanceyear") + "\t" + doc.get("resulttitle") + "\t" +
doc.get("resultsubject") + "\t" + doc.get("relprojectname") + "\t" +
doc.get("resulttitle") + "\t" + doc.get("resultdescription") + "\t" +
doc.get("relprojectname") + "\t" + doc.get("__all") + "\t");
doc.get("resultdescription") + "\t" + }
doc.get("__all") + "\t" }
); }
}
}
}
} }

View File

@ -23,87 +23,87 @@ import org.slf4j.LoggerFactory;
public abstract class SolrExploreTest { public abstract class SolrExploreTest {
protected static final Logger log = LoggerFactory.getLogger(SolrTest.class); protected static final Logger log = LoggerFactory.getLogger(SolrTest.class);
protected static final String FORMAT = "test"; protected static final String FORMAT = "test";
protected static final String DEFAULT_COLLECTION = FORMAT + "-index-openaire"; protected static final String DEFAULT_COLLECTION = FORMAT + "-index-openaire";
protected static final String CONFIG_NAME = "testConfig"; protected static final String CONFIG_NAME = "testConfig";
protected static MiniSolrCloudCluster miniCluster; protected static MiniSolrCloudCluster miniCluster;
@TempDir @TempDir
public static Path workingDir; public static Path workingDir;
@BeforeAll @BeforeAll
public static void setup() throws Exception { public static void setup() throws Exception {
// random unassigned HTTP port // random unassigned HTTP port
final int jettyPort = 0; final int jettyPort = 0;
final JettyConfig jettyConfig = JettyConfig.builder().setPort(jettyPort).build(); final JettyConfig jettyConfig = JettyConfig.builder().setPort(jettyPort).build();
log.info(String.format("working directory: %s", workingDir.toString())); log.info(String.format("working directory: %s", workingDir.toString()));
System.setProperty("solr.log.dir", workingDir.resolve("logs").toString()); System.setProperty("solr.log.dir", workingDir.resolve("logs").toString());
// create a MiniSolrCloudCluster instance // create a MiniSolrCloudCluster instance
miniCluster = new MiniSolrCloudCluster(2, workingDir.resolve("solr"), jettyConfig); miniCluster = new MiniSolrCloudCluster(2, workingDir.resolve("solr"), jettyConfig);
// Upload Solr configuration directory to ZooKeeper // Upload Solr configuration directory to ZooKeeper
String solrZKConfigDir = "src/test/resources/eu/dnetlib/dhp/oa/provision/solr/conf/exploreTestConfig"; String solrZKConfigDir = "src/test/resources/eu/dnetlib/dhp/oa/provision/solr/conf/exploreTestConfig";
File configDir = new File(solrZKConfigDir); File configDir = new File(solrZKConfigDir);
miniCluster.uploadConfigSet(configDir.toPath(), CONFIG_NAME); miniCluster.uploadConfigSet(configDir.toPath(), CONFIG_NAME);
// override settings in the solrconfig include // override settings in the solrconfig include
System.setProperty("solr.tests.maxBufferedDocs", "100000"); System.setProperty("solr.tests.maxBufferedDocs", "100000");
System.setProperty("solr.tests.maxIndexingThreads", "-1"); System.setProperty("solr.tests.maxIndexingThreads", "-1");
System.setProperty("solr.tests.ramBufferSizeMB", "100"); System.setProperty("solr.tests.ramBufferSizeMB", "100");
// use non-test classes so RandomizedRunner isn't necessary // use non-test classes so RandomizedRunner isn't necessary
System.setProperty("solr.tests.mergeScheduler", "org.apache.lucene.index.ConcurrentMergeScheduler"); System.setProperty("solr.tests.mergeScheduler", "org.apache.lucene.index.ConcurrentMergeScheduler");
System.setProperty("solr.directoryFactory", "solr.RAMDirectoryFactory"); System.setProperty("solr.directoryFactory", "solr.RAMDirectoryFactory");
System.setProperty("solr.lock.type", "single"); System.setProperty("solr.lock.type", "single");
log.info(new ConfigSetAdminRequest.List().process(miniCluster.getSolrClient()).toString()); log.info(new ConfigSetAdminRequest.List().process(miniCluster.getSolrClient()).toString());
log log
.info( .info(
CollectionAdminRequest.ClusterStatus CollectionAdminRequest.ClusterStatus
.getClusterStatus() .getClusterStatus()
.process(miniCluster.getSolrClient()) .process(miniCluster.getSolrClient())
.toString()); .toString());
NamedList<Object> res = createCollection( NamedList<Object> res = createCollection(
miniCluster.getSolrClient(), DEFAULT_COLLECTION, 4, 2, 20, CONFIG_NAME); miniCluster.getSolrClient(), DEFAULT_COLLECTION, 4, 2, 20, CONFIG_NAME);
res.forEach(o -> log.info(o.toString())); res.forEach(o -> log.info(o.toString()));
miniCluster.getSolrClient().setDefaultCollection(DEFAULT_COLLECTION); miniCluster.getSolrClient().setDefaultCollection(DEFAULT_COLLECTION);
log log
.info( .info(
CollectionAdminRequest.ClusterStatus CollectionAdminRequest.ClusterStatus
.getClusterStatus() .getClusterStatus()
.process(miniCluster.getSolrClient()) .process(miniCluster.getSolrClient())
.toString()); .toString());
} }
@AfterAll @AfterAll
public static void shutDown() throws Exception { public static void shutDown() throws Exception {
miniCluster.shutdown(); miniCluster.shutdown();
FileUtils.deleteDirectory(workingDir.toFile()); FileUtils.deleteDirectory(workingDir.toFile());
} }
protected static NamedList<Object> createCollection(CloudSolrClient client, String name, int numShards, protected static NamedList<Object> createCollection(CloudSolrClient client, String name, int numShards,
int replicationFactor, int maxShardsPerNode, String configName) throws Exception { int replicationFactor, int maxShardsPerNode, String configName) throws Exception {
ModifiableSolrParams modParams = new ModifiableSolrParams(); ModifiableSolrParams modParams = new ModifiableSolrParams();
modParams.set(CoreAdminParams.ACTION, CollectionParams.CollectionAction.CREATE.name()); modParams.set(CoreAdminParams.ACTION, CollectionParams.CollectionAction.CREATE.name());
modParams.set("name", name); modParams.set("name", name);
modParams.set("numShards", numShards); modParams.set("numShards", numShards);
modParams.set("replicationFactor", replicationFactor); modParams.set("replicationFactor", replicationFactor);
modParams.set("collection.configName", configName); modParams.set("collection.configName", configName);
modParams.set("maxShardsPerNode", maxShardsPerNode); modParams.set("maxShardsPerNode", maxShardsPerNode);
QueryRequest request = new QueryRequest(modParams); QueryRequest request = new QueryRequest(modParams);
request.setPath("/admin/collections"); request.setPath("/admin/collections");
return client.request(request); return client.request(request);
} }
} }