Skip to content

Commit e9075bb

Browse files
committed
Merge pull request 'sonar_qf' (#6) from sonar_qf into develop
Reviewed-on: https://gitea.intranda.com/goobi-viewer/goobi-viewer-connector/pulls/6
2 parents 6576034 + c8c12fa commit e9075bb

File tree

6 files changed

+95
-22
lines changed

6 files changed

+95
-22
lines changed

goobi-viewer-connector/src/main/java/io/goobi/viewer/connector/utils/Configuration.java

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
import org.apache.commons.configuration2.builder.ReloadingFileBasedConfigurationBuilder;
3030
import org.apache.commons.configuration2.builder.fluent.Parameters;
3131
import org.apache.commons.configuration2.convert.DefaultListDelimiterHandler;
32-
import org.apache.commons.configuration2.event.Event;
33-
import org.apache.commons.configuration2.event.EventListener;
3432
import org.apache.commons.configuration2.ex.ConfigurationException;
3533
import org.apache.commons.configuration2.ex.ConversionException;
3634
import org.apache.commons.configuration2.tree.ImmutableNode;
@@ -70,7 +68,6 @@ public final class Configuration {
7068
*
7169
* @param configPath a {@link java.lang.String} object.
7270
*/
73-
@SuppressWarnings({ "unchecked", "rawtypes" })
7471
public Configuration(String configPath) {
7572
// Load default configuration
7673
builder =
@@ -88,13 +85,7 @@ public Configuration(String configPath) {
8885
logger.error(e.getMessage(), e);
8986
}
9087
builder.addEventListener(ConfigurationBuilderEvent.CONFIGURATION_REQUEST,
91-
new EventListener() {
92-
93-
@Override
94-
public void onEvent(Event event) {
95-
builder.getReloadingController().checkForReloading(null);
96-
}
97-
});
88+
event -> builder.getReloadingController().checkForReloading(null));
9889
} else {
9990
logger.error("Default Connector configuration file not found: {}; Base path is {}",
10091
builder.getFileHandler().getFile().getAbsoluteFile(),
@@ -117,13 +108,7 @@ public void onEvent(Event event) {
117108
logger.error("{} ({})", e.getMessage(), fileLocal.getAbsolutePath(), e);
118109
}
119110
builderLocal.addEventListener(ConfigurationBuilderEvent.CONFIGURATION_REQUEST,
120-
new EventListener() {
121-
122-
@Override
123-
public void onEvent(Event event) {
124-
builderLocal.getReloadingController().checkForReloading(null);
125-
}
126-
});
111+
event -> builderLocal.getReloadingController().checkForReloading(null));
127112
}
128113
}
129114

@@ -820,4 +805,14 @@ private static List<Metadata> getMetadataForTemplate(HierarchicalConfiguration u
820805

821806
return ret;
822807
}
808+
809+
/**
810+
* Overrides values in the config file (for unit test purposes).
811+
*
812+
* @param property Property path (e.g. "accessConditions.fullAccessForLocalhost")
813+
* @param value New value to set
814+
*/
815+
public void overrideValue(String property, Object value) {
816+
getConfig().setProperty(property, value);
817+
}
823818
}

goobi-viewer-connector/src/main/java/io/goobi/viewer/connector/utils/SolrSearchIndex.java

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public class SolrSearchIndex {
6969
private long lastPing = 0;
7070

7171
private SolrClient client;
72-
private final boolean testMode;
72+
private boolean testMode;
7373

7474
/**
7575
* <p>
@@ -94,6 +94,9 @@ public SolrSearchIndex(SolrClient client, boolean testMode) {
9494

9595
/**
9696
* Checks whether the server's configured URL matches that in the config file. If not, a new server instance is created.
97+
*
98+
* @should create new client if solr url changed
99+
* @should ping server if last ping too old
97100
*/
98101
public void checkReloadNeeded() {
99102
if (testMode || !(client instanceof Http2SolrClient)) {
@@ -648,4 +651,49 @@ public Map<Integer, String> getFulltextFileNames(String pi) throws SolrServerExc
648651

649652
return Collections.emptyMap();
650653
}
654+
655+
/**
656+
* Getter for tests.
657+
*
658+
* @return the lastPing
659+
*/
660+
long getLastPing() {
661+
return lastPing;
662+
}
663+
664+
/**
665+
* Setter for tests.
666+
*
667+
* @param lastPing the lastPing to set
668+
*/
669+
void setLastPing(long lastPing) {
670+
this.lastPing = lastPing;
671+
}
672+
673+
/**
674+
* Getter for tests.
675+
*
676+
* @return the client
677+
*/
678+
SolrClient getClient() {
679+
return client;
680+
}
681+
682+
/**
683+
* Getter for tests.
684+
*
685+
* @return the testMode
686+
*/
687+
boolean isTestMode() {
688+
return testMode;
689+
}
690+
691+
/**
692+
* Setter for tests.
693+
*
694+
* @param testMode the testMode to set
695+
*/
696+
void setTestMode(boolean testMode) {
697+
this.testMode = testMode;
698+
}
651699
}

goobi-viewer-connector/src/test/java/io/goobi/viewer/connector/AbstractSolrEnabledTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@
2626
*/
2727
public abstract class AbstractSolrEnabledTest extends AbstractTest {
2828

29-
private static final String SOLR_TEST_URL = "https://viewer-testing-index.goobi.io/solr/collection1";
29+
public static final String SOLR_TEST_URL = "https://viewer-testing-index.goobi.io/solr/collection1";
3030

3131
@BeforeAll
3232
public static void setUpClass() throws Exception {
3333
AbstractTest.setUpClass();
3434
}
3535

3636
@BeforeEach
37-
public void setUp() throws Exception {
37+
public void setUp() {
3838
SolrClient client = SolrSearchIndex.getNewSolrClient(SOLR_TEST_URL);
3939
DataManager.getInstance().injectSearchIndex(new SolrSearchIndex(client, true));
4040
}

goobi-viewer-connector/src/test/java/io/goobi/viewer/connector/utils/ConfigurationTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.junit.jupiter.api.Assertions;
2323
import org.junit.jupiter.api.Test;
2424

25+
import io.goobi.viewer.connector.AbstractSolrEnabledTest;
2526
import io.goobi.viewer.connector.AbstractTest;
2627
import io.goobi.viewer.connector.DataManager;
2728
import io.goobi.viewer.connector.oai.enums.Metadata;
@@ -131,7 +132,7 @@ void getEseTypes_shouldReturnAllValues() {
131132
*/
132133
@Test
133134
void getIndexUrl_shouldReturnCorrectValue() {
134-
Assertions.assertEquals("http://localhost:8080/solr", DataManager.getInstance().getConfiguration().getIndexUrl());
135+
Assertions.assertEquals(AbstractSolrEnabledTest.SOLR_TEST_URL, DataManager.getInstance().getConfiguration().getIndexUrl());
135136
}
136137

137138
/**

goobi-viewer-connector/src/test/java/io/goobi/viewer/connector/utils/SolrSearchIndexTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.apache.logging.log4j.LogManager;
2323
import org.apache.logging.log4j.Logger;
24+
import org.apache.solr.client.solrj.SolrClient;
2425
import org.apache.solr.client.solrj.response.QueryResponse;
2526
import org.apache.solr.common.SolrDocument;
2627
import org.junit.jupiter.api.Assertions;
@@ -36,6 +37,34 @@ class SolrSearchIndexTest extends AbstractSolrEnabledTest {
3637
/** Logger for this class. */
3738
private static final Logger logger = LogManager.getLogger(SolrSearchIndexTest.class);
3839

40+
/**
41+
* @see SolrSearchIndex#checkReloadNeeded()
42+
* @verifies create new client if solr url changed
43+
*/
44+
@Test
45+
void checkReloadNeeded_shouldCreateNewClientIfSolrUrlChanged() {
46+
SolrSearchIndex index = DataManager.getInstance().getSearchIndex();
47+
index.setTestMode(false);
48+
SolrClient oldClient = index.getClient();
49+
Assertions.assertEquals(oldClient, index.getClient());
50+
DataManager.getInstance().getConfiguration().overrideValue("solr.solrUrl", "http://localhost:8080/solr");
51+
index.checkReloadNeeded();
52+
Assertions.assertNotEquals(oldClient, index.getClient());
53+
}
54+
55+
/**
56+
* @see SolrSearchIndex#checkReloadNeeded()
57+
* @verifies ping server if last ping too old
58+
*/
59+
@Test
60+
void checkReloadNeeded_shouldPingServerIfLastPingTooOld() {
61+
SolrSearchIndex index = DataManager.getInstance().getSearchIndex();
62+
index.setTestMode(false);
63+
Assertions.assertEquals(0, index.getLastPing());
64+
index.checkReloadNeeded();
65+
Assertions.assertNotEquals(0, index.getLastPing());
66+
}
67+
3968
/**
4069
* @see SolrSearchIndex#getSets(String)
4170
* @verifies return all values

goobi-viewer-connector/src/test/resources/config_oai.test.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
<!-- Anzahl der Treffer pro Seite -->
4444
<hitsPerToken>23</hitsPerToken>
4545
<!-- URL zum Index -->
46-
<solrUrl>http://localhost:8080/solr</solrUrl>
46+
<solrUrl>https://viewer-testing-index.goobi.io/solr/collection1</solrUrl>
4747
</solr>
4848
<oaiFolder>src/test/resources/oai/</oaiFolder>
4949
<!-- Verzeichnis in dem die resumtionToken gespeichert werden -->

0 commit comments

Comments
 (0)