diff --git a/bitrepository-core/.gitignore b/bitrepository-core/.gitignore index 6d2e69f2a..465645922 100644 --- a/bitrepository-core/.gitignore +++ b/bitrepository-core/.gitignore @@ -5,3 +5,5 @@ /activemq-data /test-output /bin +statistic-* + diff --git a/bitrepository-core/src/main/java/org/bitrepository/common/utils/SettingsUtils.java b/bitrepository-core/src/main/java/org/bitrepository/common/utils/SettingsUtils.java index 5c4a55aed..f46134535 100644 --- a/bitrepository-core/src/main/java/org/bitrepository/common/utils/SettingsUtils.java +++ b/bitrepository-core/src/main/java/org/bitrepository/common/utils/SettingsUtils.java @@ -22,6 +22,8 @@ package org.bitrepository.common.utils; import org.bitrepository.common.settings.Settings; +import org.bitrepository.settings.referencesettings.PillarIntegrityDetails; +import org.bitrepository.settings.referencesettings.PillarType; import org.bitrepository.settings.repositorysettings.Collection; import java.math.BigInteger; @@ -98,6 +100,42 @@ public static String getCollectionName(String collectionID) { return name; } + /** + * Get the hostname for the given pillarID from the ReferenceSettings. + * + * @param pillarID The pillarID for which the hostname is wanted. + * @return Returns the hostname for the given pillar ID. + */ + public static String getHostname(String pillarID) { + PillarIntegrityDetails details = settings.getReferenceSettings().getIntegrityServiceSettings().getPillarIntegrityDetails(); + if (details != null) { + for (PillarIntegrityDetails.PillarDetails d : details.getPillarDetails()) { + if (d.getPillarID().equals(pillarID)) { + return d.getPillarHostname(); + } + } + } + return null; + } + + /** + * Get the {@link PillarType} for the given Pillar ID. + * + * @param pillarID The pillar ID for which the {@link PillarType} is wanted. + * @return Returns the {@link PillarType} for the given pillar ID. + */ + public static PillarType getPillarType(String pillarID) { + PillarIntegrityDetails details = settings.getReferenceSettings().getIntegrityServiceSettings().getPillarIntegrityDetails(); + if (details != null) { + for (PillarIntegrityDetails.PillarDetails d : details.getPillarDetails()) { + if (d.getPillarID().equals(pillarID)) { + return d.getPillarType(); + } + } + } + return null; + } + /** * Get the name of the repository * diff --git a/bitrepository-core/src/main/resources/examples/settings/ReferenceSettings.xml b/bitrepository-core/src/main/resources/examples/settings/ReferenceSettings.xml index 6e2f5111d..97ddb099a 100644 --- a/bitrepository-core/src/main/resources/examples/settings/ReferenceSettings.xml +++ b/bitrepository-core/src/main/resources/examples/settings/ReferenceSettings.xml @@ -425,6 +425,14 @@ Directory must be write and readable by integrity service. --> /var/integrity-reports/ + + + + Pillar1 + test@kb.dk + FILE + + diff --git a/bitrepository-core/src/test/java/org/bitrepository/common/settings/SettingsLoaderTest.java b/bitrepository-core/src/test/java/org/bitrepository/common/settings/SettingsLoaderTest.java index d5c281788..2ca793f24 100644 --- a/bitrepository-core/src/test/java/org/bitrepository/common/settings/SettingsLoaderTest.java +++ b/bitrepository-core/src/test/java/org/bitrepository/common/settings/SettingsLoaderTest.java @@ -1,23 +1,23 @@ /* * #%L * Bitrepository Protocol - * + * * $Id$ * $HeadURL$ * %% * Copyright (C) 2010 - 2011 The State and University Library, The Royal Library and The State Archives, Denmark * %% * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 2.1 of the + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 2.1 of the * License, or (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public + * + * You should have received a copy of the GNU General Lesser Public * License along with this program. If not, see * . * #L% @@ -28,28 +28,27 @@ import org.testng.Assert; import org.testng.annotations.Test; -import java.util.Arrays; import java.util.List; public class SettingsLoaderTest extends ExtendedTestCase { private static final String PATH_TO_SETTINGS = "settings/xml/bitrepository-devel"; private static final String PATH_TO_EXAMPLE_SETTINGS = "examples/settings"; - @Test(groups = { "regressiontest" }) - public void testDevelopmentCollectionSettingsLoading() throws Exception { - SettingsProvider settingsLoader = + @Test(groups = {"regressiontest"}) + public void testDevelopmentCollectionSettingsLoading() { + SettingsProvider settingsLoader = new SettingsProvider(new XMLFileSettingsLoader(PATH_TO_SETTINGS), getClass().getSimpleName()); Settings settings = settingsLoader.getSettings(); - List expectedPillarIDs = Arrays.asList(new String[] {"Pillar1", "Pillar2"}); + List expectedPillarIDs = List.of("Pillar1", "Pillar2"); Assert.assertEquals( settings.getRepositorySettings().getCollections().getCollection().get(0).getPillarIDs().getPillarID(), expectedPillarIDs); } - - @Test(groups = { "regressiontest" }) - public void testExampleSettingsLoading() throws Exception { - SettingsProvider settingsLoader = + + @Test(groups = {"regressiontest"}) + public void testExampleSettingsLoading() { + SettingsProvider settingsLoader = new SettingsProvider(new XMLFileSettingsLoader(PATH_TO_EXAMPLE_SETTINGS), getClass().getSimpleName()); settingsLoader.getSettings(); diff --git a/bitrepository-core/src/test/java/org/bitrepository/protocol/IntegrationTest.java b/bitrepository-core/src/test/java/org/bitrepository/protocol/IntegrationTest.java index 533e8c43c..ece1da863 100644 --- a/bitrepository-core/src/test/java/org/bitrepository/protocol/IntegrationTest.java +++ b/bitrepository-core/src/test/java/org/bitrepository/protocol/IntegrationTest.java @@ -1,23 +1,23 @@ /* * #%L * Bitrepository Common - * + * * $Id$ * $HeadURL$ * %% * Copyright (C) 2010 - 2011 The State and University Library, The Royal Library and The State Archives, Denmark * %% * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 2.1 of the + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 2.1 of the * License, or (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public + * + * You should have received a copy of the GNU General Lesser Public * License along with this program. If not, see * . * #L% @@ -55,36 +55,27 @@ import java.lang.reflect.Method; import java.net.MalformedURLException; import java.net.URL; -import java.util.Arrays; +import java.util.List; -/** - * Contains the generic parts for integration testing. - */ public abstract class IntegrationTest extends ExtendedTestCase { protected static TestEventManager testEventManager = TestEventManager.getInstance(); public static LocalActiveMQBroker broker; public static EmbeddedHttpServer server; public static HttpServerConfiguration httpServerConfiguration; public static MessageBus messageBus; - private MessageReceiverManager receiverManager; protected static String alarmDestinationID; protected static MessageReceiver alarmReceiver; - protected static SecurityManager securityManager; - - /** Settings for the Component-Under-Test */ protected static Settings settingsForCUT; - /** Settings for the Test components */ protected static Settings settingsForTestClient; protected static String collectionID; - protected String NON_DEFAULT_FILE_ID; protected static String DEFAULT_FILE_ID; protected static URL DEFAULT_FILE_URL; protected static String DEFAULT_DOWNLOAD_FILE_ADDRESS; protected static String DEFAULT_UPLOAD_FILE_ADDRESS; - protected String DEFAULT_AUDITINFORMATION; + protected String DEFAULT_AUDIT_INFORMATION; protected String testMethodName; @@ -94,8 +85,7 @@ public void initializeSuite(ITestContext testContext) { settingsForTestClient = loadSettings("TestSuiteInitialiser"); makeUserSpecificSettings(settingsForCUT); makeUserSpecificSettings(settingsForTestClient); - httpServerConfiguration = new HttpServerConfiguration( - settingsForTestClient.getReferenceSettings().getFileExchangeSettings()); + httpServerConfiguration = new HttpServerConfiguration(settingsForTestClient.getReferenceSettings().getFileExchangeSettings()); collectionID = settingsForTestClient.getCollections().get(0).getID(); securityManager = createSecurityManager(); @@ -117,6 +107,7 @@ protected void registerMessageReceivers() { alarmReceiver = new MessageReceiver(settingsForCUT.getAlarmDestination(), testEventManager); addReceiver(alarmReceiver); } + protected void addReceiver(MessageReceiver receiver) { receiverManager.addReceiver(receiver); } @@ -125,7 +116,7 @@ protected void addReceiver(MessageReceiver receiver) { public void initMessagebus() { setupMessageBus(); } - + @AfterSuite(alwaysRun = true) public void shutdownSuite() { teardownMessageBus(); @@ -140,22 +131,21 @@ public final void beforeMethod(Method method) { testMethodName = method.getName(); setupSettings(); NON_DEFAULT_FILE_ID = TestFileHelper.createUniquePrefix(testMethodName); - DEFAULT_AUDITINFORMATION = testMethodName; + DEFAULT_AUDIT_INFORMATION = testMethodName; receiverManager = new MessageReceiverManager(messageBus); registerMessageReceivers(); - messageBus.setCollectionFilter(Arrays.asList(new String[]{})); - messageBus.setComponentFilter(Arrays.asList(new String[]{})); + messageBus.setCollectionFilter(List.of()); + messageBus.setComponentFilter(List.of()); receiverManager.startListeners(); initializeCUT(); } - /** - * To be overridden by concrete tests wishing to do stuff. Remember to call super if this is overridden. - */ + + protected void initializeCUT() {} @AfterMethod(alwaysRun = true) public final void afterMethod(ITestResult testResult) { - if ( receiverManager != null ) { + if (receiverManager != null) { receiverManager.stopListeners(); } if (testResult.isSuccess()) { @@ -165,7 +155,7 @@ public final void afterMethod(ITestResult testResult) { } /** - * May be used by by concrete tests for general verification when the test method has finished. Will only be run + * May be used by specific tests for general verification when the test method has finished. Will only be run * if the test has passed (so far). */ protected void afterMethodVerification() { @@ -178,8 +168,9 @@ protected void afterMethodVerification() { protected void clearReceivers() { receiverManager.clearMessagesInReceivers(); } + /** - * May be overridden by concrete tests wishing to do stuff. Remember to call super if this is overridden. + * May be overridden by specific tests wishing to do stuff. Remember to call super if this is overridden. */ protected void shutdownCUT() {} @@ -197,20 +188,18 @@ protected void setupSettings() { makeUserSpecificSettings(settingsForTestClient); } - /** Can be overloaded by tests needing to load custom settings */ + protected Settings loadSettings(String componentID) { - Settings settings = TestSettingsProvider.reloadSettings(componentID); - return settings; + return TestSettingsProvider.reloadSettings(componentID); } private void makeUserSpecificSettings(Settings settings) { - settings.getRepositorySettings().getProtocolSettings().setCollectionDestination( - settings.getCollectionDestination() + getTopicPostfix()); - settings.getRepositorySettings().getProtocolSettings().setAlarmDestination( - settings.getAlarmDestination() + getTopicPostfix()); + settings.getRepositorySettings().getProtocolSettings() + .setCollectionDestination(settings.getCollectionDestination() + getTopicPostfix()); + settings.getRepositorySettings().getProtocolSettings().setAlarmDestination(settings.getAlarmDestination() + getTopicPostfix()); } - @BeforeTest (alwaysRun = true) + @BeforeTest(alwaysRun = true) public void writeLogStatus() { if (System.getProperty("enableLogStatus", "false").equals("true")) { LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); @@ -218,12 +207,16 @@ public void writeLogStatus() { } } - /** Indicated whether an embedded active MQ should be started and used */ + /** + * Indicated whether an embedded active MQ should be started and used + */ public boolean useEmbeddedMessageBus() { return System.getProperty("useEmbeddedMessageBus", "true").equals("true"); } - /** Indicated whether an embedded http server should be started and used */ + /** + * Indicated whether an embedded http server should be started and used + */ public boolean useEmbeddedHttpServer() { return System.getProperty("useEmbeddedHttpServer", "false").equals("true"); } @@ -232,7 +225,7 @@ public boolean useEmbeddedHttpServer() { * Hooks up the message bus. */ protected void setupMessageBus() { - if(useEmbeddedMessageBus()) { + if (useEmbeddedMessageBus()) { if (messageBus == null) { messageBus = new SimpleMessageBus(); } @@ -253,7 +246,7 @@ private void teardownMessageBus() { } } - if(broker != null) { + if (broker != null) { try { broker.stop(); broker = null; @@ -273,8 +266,9 @@ protected void teardownHttpServer() { } /** - * Returns the postfix string to use when accessing user specific topics, which is the mechanism we use in the + * Returns the postfix string to use when accessing user specific topics, which is the mechanism we use in the * bit repository tests. + * * @return The string to postfix all topix names with. */ protected String getTopicPostfix() { diff --git a/bitrepository-core/src/test/java/org/bitrepository/protocol/MessageCreationTest.java b/bitrepository-core/src/test/java/org/bitrepository/protocol/MessageCreationTest.java index fb97dcaf9..99b2f2b2b 100644 --- a/bitrepository-core/src/test/java/org/bitrepository/protocol/MessageCreationTest.java +++ b/bitrepository-core/src/test/java/org/bitrepository/protocol/MessageCreationTest.java @@ -1,23 +1,23 @@ /* * #%L * Bitrepository Protocol - * + * * $Id$ * $HeadURL$ * %% * Copyright (C) 2010 - 2011 The State and University Library, The Royal Library and The State Archives, Denmark * %% * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 2.1 of the + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 2.1 of the * License, or (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public + * + * You should have received a copy of the GNU General Lesser Public * License along with this program. If not, see * . * #L% @@ -53,26 +53,26 @@ * message-xml, thereby also testing whether this is valid. * */ public class MessageCreationTest extends ExtendedTestCase { - @Test(groups = {"regressiontest"}) public void messageCreationTest() throws Exception { addDescription("Tests if we are able to create message objects from xml. The input XML is the example code " + - "defined in the message-xml, thereby also testing whether this is valid."); + "defined in the message-xml, thereby also testing whether this is valid."); String[] messageNames = getMessageNames(); for (String messageName : messageNames) { - addStep("Creating " + messageName + " message" , - "The test is able to instantiate message based on the example in the message-xml modules"); + addStep("Creating " + messageName + " message", + "The test is able to instantiate message based on the example in the message-xml modules"); ExampleMessageFactory.createMessage( Class.forName(GetChecksumsFinalResponse.class.getPackage().getName() + "." + messageName)); } } - + @Test(groups = {"regressiontest"}, expectedExceptions = SAXException.class) public void badDateMessageTest() throws IOException, SAXException, JAXBException { addDescription("Test to ensure that messages carrying dates must provide offset."); - String messagePath = ExampleMessageFactory.PATH_TO_EXAMPLES + "BadMessages/" + + String messagePath = ExampleMessageFactory.PATH_TO_EXAMPLES + "BadMessages/" + "BadDateAlarmMessage" + ExampleMessageFactory.EXAMPLE_FILE_POSTFIX; InputStream messageIS = Thread.currentThread().getContextClassLoader().getResourceAsStream(messagePath); + assert messageIS != null; String message = IOUtils.toString(messageIS, StandardCharsets.UTF_8); JaxbHelper jaxbHelper = new JaxbHelper(ExampleMessageFactory.PATH_TO_SCHEMA, ExampleMessageFactory.SCHEMA_NAME); jaxbHelper.validate(new ByteArrayInputStream(message.getBytes(StandardCharsets.UTF_8))); @@ -100,24 +100,31 @@ private String[] getMessageNames() throws Exception { String[] messageNames = new String[nodes.getLength()]; for (int i = 0; i < nodes.getLength(); i++) { messageNames[i] = nodes.item(i).getAttributes().getNamedItem("name") - .getNodeValue(); + .getNodeValue(); } return messageNames; } - /** Needed by XPath to handle the namespaces. */ + /** + * Needed by XPath to handle the namespaces. + */ private NamespaceContext getNamespaceContext() { NamespaceContext ctx = new NamespaceContext() { public String getNamespaceURI(String prefix) { String uri; - if (prefix.equals("xs")) { - uri = "http://www.w3.org/2001/XMLSchema"; - } else if (prefix.equals("xsi")) { - uri = "http://www.w3.org/2001/XMLSchema-instance"; - } else if (prefix.equals("bre")) { - uri = "http://bitrepository.org/BitRepositoryElements.xsd"; - } else { - uri = null; + switch (prefix) { + case "xs": + uri = "http://www.w3.org/2001/XMLSchema"; + break; + case "xsi": + uri = "http://www.w3.org/2001/XMLSchema-instance"; + break; + case "bre": + uri = "http://bitrepository.org/BitRepositoryElements.xsd"; + break; + default: + uri = null; + break; } return uri; } @@ -127,7 +134,7 @@ public Iterator getPrefixes(String val) { return null; } - // Dummy implemenation - not used! + // Dummy implementation - not used! public String getPrefix(String uri) { return null; } diff --git a/bitrepository-core/src/test/java/org/bitrepository/protocol/MessageReceiverManager.java b/bitrepository-core/src/test/java/org/bitrepository/protocol/MessageReceiverManager.java index e431414ba..72b8a5c55 100644 --- a/bitrepository-core/src/test/java/org/bitrepository/protocol/MessageReceiverManager.java +++ b/bitrepository-core/src/test/java/org/bitrepository/protocol/MessageReceiverManager.java @@ -5,16 +5,16 @@ * Copyright (C) 2010 - 2012 The State and University Library, The Royal Library and The State Archives, Denmark * %% * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 2.1 of the + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 2.1 of the * License, or (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public + * + * You should have received a copy of the GNU General Lesser Public * License along with this program. If not, see * . * #L% @@ -35,7 +35,7 @@ public class MessageReceiverManager { new LinkedList<>(); private final MessageBus messageBus; - public MessageReceiverManager (MessageBus messageBus) { + public MessageReceiverManager(MessageBus messageBus) { this.messageBus = messageBus; } diff --git a/bitrepository-core/src/test/java/org/bitrepository/protocol/performancetest/MessageBusDelayTest.java b/bitrepository-core/src/test/java/org/bitrepository/protocol/performancetest/MessageBusDelayTest.java index 355328a8d..710914fbd 100644 --- a/bitrepository-core/src/test/java/org/bitrepository/protocol/performancetest/MessageBusDelayTest.java +++ b/bitrepository-core/src/test/java/org/bitrepository/protocol/performancetest/MessageBusDelayTest.java @@ -5,16 +5,16 @@ * Copyright (C) 2010 - 2012 The State and University Library, The Royal Library and The State Archives, Denmark * %% * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 2.1 of the + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 2.1 of the * License, or (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public + * + * You should have received a copy of the GNU General Lesser Public * License along with this program. If not, see * . * #L% @@ -35,7 +35,6 @@ import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; -import java.io.File; import java.io.FileOutputStream; import java.nio.charset.StandardCharsets; import java.util.ArrayList; @@ -46,28 +45,21 @@ public class MessageBusDelayTest extends ExtendedTestCase { private Settings settings; - /** The mocked SecurityManager */ private SecurityManager securityManager; - protected TestEventManager testEventManager = TestEventManager.getInstance(); - private static final int PERFORMANCE_COUNT = 1000; - private static final int NUMBER_OF_TESTS = 100; - private static final boolean WRITE_RESULTS_TO_DISC = true; -// private static final boolean WRITE_RESULTS_TO_DISC = false; - - @BeforeClass (alwaysRun = true) + + @BeforeClass(alwaysRun = true) public void setup() { settings = TestSettingsProvider.reloadSettings(getClass().getSimpleName()); securityManager = new DummySecurityManager(); } - - - @Test( groups = {"StressTest"} ) - public void testManyTimes() throws Exception { - for(int i = 0; i < NUMBER_OF_TESTS; i++) { + + @Test(groups = {"StressTest"}) + public void testManyTimes() { + for (int i = 0; i < NUMBER_OF_TESTS; i++) { try { performStatisticalAnalysisOfMessageDelay(); } catch (Exception e) { @@ -75,64 +67,58 @@ public void testManyTimes() throws Exception { } } } - + public void performStatisticalAnalysisOfMessageDelay() throws Exception { addDescription("This test has the purpose of sending a lot of messages and calculating some statistics " - + "on the delay between the sending and the receival of the message."); + + "on the delay between the sending and the retrieval of the message."); addStep("Setup the variables and connections for the test.", "Should connect to the messagebus."); MessageBus messageBus = MessageBusManager.getMessageBus(settings, securityManager); MessageReceiver destinationReceiver; String destination = "DelayPerformanceTestDestination-" + new Date().getTime(); - destinationReceiver = new MessageReceiver("Performance test topic receiver", null); //testEventManager); + destinationReceiver = new MessageReceiver("Performance test topic receiver", null); messageBus.addListener(destination, destinationReceiver.getMessageListener()); - + List delayList = new ArrayList<>(PERFORMANCE_COUNT); AlarmMessage message = ExampleMessageFactory.createMessage(AlarmMessage.class); message.setDestination(destination); - + addStep("Sending the message and calculating the time.", "Should be done '" + PERFORMANCE_COUNT + "' times."); - for(int i = 0; i < PERFORMANCE_COUNT; i++) { + for (int i = 0; i < PERFORMANCE_COUNT; i++) { Date before = new Date(); messageBus.sendMessage(message); AlarmMessage received = destinationReceiver.waitForMessage(AlarmMessage.class, 100, TimeUnit.SECONDS); Date after = new Date(); - if(received == null) { + if (received == null) { System.err.println("No message received within 100 seconds"); } - + long delay = after.getTime() - before.getTime(); delayList.add(delay); } - + addStep("Perform the statistical analysis on the delay results.", "TODO !!!!"); calculateStatistics(delayList); } - + private void calculateStatistics(List list) throws Exception { Collections.sort(list); - long maximum = list.get(list.size()-1); + long maximum = list.get(list.size() - 1); long minimum = list.get(0); long median = calculateMedian(list); long average = calculateAverage(list); double deviation = calculateStdDeviation(list, average); - - if(WRITE_RESULTS_TO_DISC) { - FileOutputStream fos = null; - try { - fos = new FileOutputStream(new File("statistic-" + new Date().getTime())); - fos.write(new String("Maximum;" + maximum + "\n").getBytes(StandardCharsets.UTF_8)); - fos.write(new String("Minimum;" + minimum + "\n").getBytes(StandardCharsets.UTF_8)); - fos.write(new String("Median;" + median + "\n").getBytes(StandardCharsets.UTF_8)); - fos.write(new String("Average;" + average + "\n").getBytes(StandardCharsets.UTF_8)); - fos.write(new String("StdDeviation;" + deviation + "\n").getBytes(StandardCharsets.UTF_8)); - fos.write(new String("\n").getBytes(StandardCharsets.UTF_8)); - - for(Long l : list) { - fos.write(new String(l + "\n").getBytes(StandardCharsets.UTF_8)); - } - } finally { - if(fos != null) { - fos.close(); + + if (WRITE_RESULTS_TO_DISC) { + try (FileOutputStream fos = new FileOutputStream("statistic-" + new Date().getTime())) { + fos.write(("Maximum;" + maximum + "\n").getBytes(StandardCharsets.UTF_8)); + fos.write(("Minimum;" + minimum + "\n").getBytes(StandardCharsets.UTF_8)); + fos.write(("Median;" + median + "\n").getBytes(StandardCharsets.UTF_8)); + fos.write(("Average;" + average + "\n").getBytes(StandardCharsets.UTF_8)); + fos.write(("StdDeviation;" + deviation + "\n").getBytes(StandardCharsets.UTF_8)); + fos.write("\n".getBytes(StandardCharsets.UTF_8)); + + for (Long l : list) { + fos.write((l + "\n").getBytes(StandardCharsets.UTF_8)); } } } @@ -142,29 +128,27 @@ private void calculateStatistics(List list) throws Exception { System.out.println("Average;" + average); System.out.println("StdDeviation;" + deviation); } - + private long calculateMedian(List list) { - - if(list.size() % 2 == 0) { - return (list.get(list.size() / 2) + list.get(list.size() / 2 - 1)) / 2; + if (list.size() % 2 == 0) { + return (list.get(list.size() / 2) + list.get(list.size() / 2 - 1)) / 2; } else { return list.get(list.size() / 2); } } - + private long calculateAverage(List list) { long total = 0; - for(Long l : list) { + for (Long l : list) { total += l; } - return total / list.size(); } - + private double calculateStdDeviation(List list, Long average) { long deviationSquared = 0; - for(Long l : list) { - deviationSquared += (l-average) * (l-average); + for (Long l : list) { + deviationSquared += (l - average) * (l - average); } return Math.sqrt(deviationSquared) / (double) list.size(); } diff --git a/bitrepository-core/src/test/java/org/bitrepository/protocol/performancetest/MessageBusNumberOfListenersStressTest.java b/bitrepository-core/src/test/java/org/bitrepository/protocol/performancetest/MessageBusNumberOfListenersStressTest.java index 2f5a594ab..066c4eb50 100644 --- a/bitrepository-core/src/test/java/org/bitrepository/protocol/performancetest/MessageBusNumberOfListenersStressTest.java +++ b/bitrepository-core/src/test/java/org/bitrepository/protocol/performancetest/MessageBusNumberOfListenersStressTest.java @@ -1,23 +1,23 @@ /* * #%L * Bitmagasin integrationstest - * + * * $Id$ * $HeadURL$ * %% * Copyright (C) 2010 The State and University Library, The Royal Library and The State Archives, Denmark * %% * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 2.1 of the + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 2.1 of the * License, or (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public + * + * You should have received a copy of the GNU General Lesser Public * License along with this program. If not, see * . * #L% @@ -50,49 +50,63 @@ import java.util.List; /** - * Stress testing of the messagebus. - * + * Stress testing of the messagebus. + *

* TODO Important note for testers: * The number of listeners should be regulated through the 'NUMBER_OF_LISTENERS' constant. - * When using many listeners, the DEFAULT_WAIT_TIME should be increased, e.g. 5000 for 25 listeners - * and 15000 for 100 listeners. + * When using many listeners, the DEFAULT_WAIT_TIME should be increased, e.g. 5000 for 25 listeners + * and 15000 for 100 listeners. * Otherwise it is not ensured, that all the messagelisteners will receive all the messages before the validation. - * + *

* Also, the shutdown of the messagelisteners can generate some noise, which will make it impossible to retrieve the - * output data from the console. Therefore the results can be written to a file after the test. + * output data from the console. Therefore the results can be written to a file after the test. * This is controlled through the variables 'WRITE_RESULTS_TO_FILE', which deternimes whether to write to the file, and * 'OUTPUT_FILE_NAME' which is the name of the file to write the output results. - * */ public class MessageBusNumberOfListenersStressTest extends ExtendedTestCase { - /** The queue name.*/ + /** + * The queue name. + */ private static String QUEUE = "TEST-LISTENERS"; - /** The default time to wait for a simple communication.*/ - private static long DEFAULT_WAIT_TIME = 500; - - /** The time for the whole test.*/ - private static long TIME_FRAME = 60000L; - /** The number of message listeners in the test.*/ - private static int NUMBER_OF_LISTENERS = 10; - - /** Whether the results will be written to a file.*/ + /** + * The default time to wait for a simple communication. + */ + private static final long DEFAULT_WAIT_TIME = 500; + /** + * The time for the whole test. + */ + private static final long TIME_FRAME = 60000L; + /** + * The number of message listeners in the test. + */ + private static final int NUMBER_OF_LISTENERS = 10; + /** + * Whether the results will be written to a file. + */ private static final boolean WRITE_RESULTS_TO_FILE = false; - /** The name of the output file for the results of the tests.*/ + /** + * The name of the output file for the results of the tests. + */ private static final String OUTPUT_FILE_NAME = "NumberOfListeners-results.test"; - - /** The reached correlation ID for the message.*/ + /** + * The reached correlation ID for the message. + */ private static int idReached = -1; - - /** The message to send back and forth over the message bus.*/ + /** + * The message to send back and forth over the message bus. + */ private static AlarmMessage alarmMessage; - - /** The message bus instance for sending the messages.*/ + /** + * The message bus instance for sending the messages. + */ private static MessageBus bus; - - /** The amount of messages received.*/ + /** + * The amount of messages received. + */ private static int messageReceived = 0; - - /** Whether more messages should be send.*/ + /** + * Whether more messages should be sent. + */ private static boolean sendMoreMessages = true; private Settings settings; @@ -100,12 +114,14 @@ public class MessageBusNumberOfListenersStressTest extends ExtendedTestCase { public void initializeSettings() { settings = TestSettingsProvider.getSettings(getClass().getSimpleName()); } + /** - * Tests the amount of messages send over a message bus, which is not placed locally. - * Requires to send at least five per second. - * @throws Exception + * Tests the amount of messages sent over a message bus, which is not placed locally. + * Require sending at least five messages per second. + * + * @throws Exception Can possibly throw an exception. */ - @Test( groups = {"StressTest"} ) + @Test(groups = {"StressTest"}) public void testManyListenersOnLocalMessageBus() throws Exception { addDescription("Tests how many messages can be handled within a given timeframe when a given number of " + "listeners are receiving them."); @@ -123,26 +139,23 @@ public void testManyListenersOnLocalMessageBus() throws Exception { settings.getRepositorySettings().getProtocolSettings().setMessageBusConfiguration( MessageBusConfigurationFactory.createEmbeddedMessageBusConfiguration() ); - /** The mocked SecurityManager */ + /* The mocked SecurityManager */ SecurityManager securityManager = new DummySecurityManager(); LocalActiveMQBroker broker = new LocalActiveMQBroker(settings.getMessageBusConfiguration()); try { - addStep("Start the broker and initialise the listeners.", - "Connections should be established."); + addStep("Start the broker and initialise the listeners.", + "Connections should be established."); broker.start(); bus = new ActiveMQMessageBus(settings, securityManager); testListeners(settings.getMessageBusConfiguration(), securityManager); } finally { - if(broker != null) { - broker.stop(); - broker = null; - } + broker.stop(); } } - @Test( groups = {"StressTest"} ) + @Test(groups = {"StressTest"}) public void testManyListenersOnDistributedMessageBus() throws Exception { addDescription("Tests how many messages can be handled within a given timeframe when a given number of " + "listeners are receiving them."); @@ -158,11 +171,11 @@ public void testManyListenersOnDistributedMessageBus() throws Exception { addStep("Make configuration for the messagebus.", "Both should be created."); MessageBusConfiguration conf = MessageBusConfigurationFactory.createDefaultConfiguration(); - /** The mocked SecurityManager */ + /* The mocked SecurityManager */ SecurityManager securityManager = new DummySecurityManager(); - addStep("Start the broker and initialise the listeners.", - "Connections should be established."); + addStep("Start the broker and initialise the listeners.", + "Connections should be established."); bus = new ActiveMQMessageBus(settings, securityManager); testListeners(conf, securityManager); @@ -174,7 +187,7 @@ public void testListeners(MessageBusConfiguration conf, SecurityManager security try { addStep("Initialise the message listeners.", "Should be created and connected to the message bus."); - for(int i = 0; i < NUMBER_OF_LISTENERS; i++) { + for (int i = 0; i < NUMBER_OF_LISTENERS; i++) { listeners.add(new NotificationMessageListener(settings, securityManager)); } @@ -191,8 +204,8 @@ public void testListeners(MessageBusConfiguration conf, SecurityManager security addStep("Send the first message", "Message should be send."); sendMessageWithId(1); - addStep("Wait for the timeframe on '" + TIME_FRAME + "' milliseconds.", - "We wait!"); + addStep("Wait for the timeframe on '" + TIME_FRAME + "' milliseconds.", + "We wait!"); synchronized (this) { try { wait(TIME_FRAME); @@ -201,8 +214,8 @@ public void testListeners(MessageBusConfiguration conf, SecurityManager security } } - addStep("Stop sending more messages and await all the messages to be received by all the listeners", - "Should be Ok"); + addStep("Stop sending more messages and await all the messages to be received by all the listeners", + "Should be Ok"); sendMoreMessages = false; synchronized (this) { try { @@ -212,37 +225,35 @@ public void testListeners(MessageBusConfiguration conf, SecurityManager security } } - addStep("Verifying the amount of message sent '" + idReached + "' has been received by all '" + addStep("Verifying the amount of message sent '" + idReached + "' has been received by all '" + NUMBER_OF_LISTENERS + "' listeners", "Should be the same amount for each listener, and the same " + "amount as the correlation ID of the message"); - Assert.assertTrue(idReached * NUMBER_OF_LISTENERS == messageReceived, + Assert.assertEquals(messageReceived, idReached * NUMBER_OF_LISTENERS, "Reached message Id " + idReached + " thus each message of the " + NUMBER_OF_LISTENERS + " listener " - + "should have received " + idReached + " message, though they have received " - + messageReceived + " message all together."); - for(NotificationMessageListener listener : listeners) { - Assert.assertTrue((listener.getCount() == idReached), - "Should have received " + idReached + " messages, but has received " - + listener.getCount()); + + "should have received " + idReached + " message, though they have received " + + messageReceived + " message all together."); + for (NotificationMessageListener listener : listeners) { + Assert.assertTrue((listener.getCount() == idReached), + "Should have received " + idReached + " messages, but has received " + + listener.getCount()); } - // If too many messagelisteners, then they will create so much noise, that the results cannot be read from + // If too many message-listeners, then they will create so much noise, that the results cannot be read from // the console output (due to shutdown 'warnings'). Thus write the results in a file. - if(WRITE_RESULTS_TO_FILE) { + if (WRITE_RESULTS_TO_FILE) { FileOutputStream out = new FileOutputStream(new File(OUTPUT_FILE_NAME), true); - out.write(new String("idReached: " + idReached + ", NumberOfListeners: " + NUMBER_OF_LISTENERS - + ", messagesReceived: " + messageReceived + " on bus " + out.write(new String("idReached: " + idReached + ", NumberOfListeners: " + NUMBER_OF_LISTENERS + + ", messagesReceived: " + messageReceived + " on bus " + conf.getURL() + "\n").getBytes()); out.flush(); out.close(); - } + } } finally { - if(listeners != null) { - for(NotificationMessageListener listener : listeners) { - listener.stop(); - } - listeners.clear(); - listeners = null; + for (NotificationMessageListener listener : listeners) { + listener.stop(); } + listeners.clear(); + synchronized (this) { try { wait(DEFAULT_WAIT_TIME); @@ -255,11 +266,11 @@ public void testListeners(MessageBusConfiguration conf, SecurityManager security /** * Method for sending the Alarm message with a specific ID. - * + * * @param id The correlation id for the message to send. */ private static void sendMessageWithId(int id) { - if(sendMoreMessages) { + if (sendMoreMessages) { alarmMessage.setCorrelationID("" + id); bus.sendMessage(alarmMessage); } @@ -267,13 +278,13 @@ private static void sendMessageWithId(int id) { /** * Function for handling the Correlation id of the received messages of the listeners. - * If it is the first time a correlation id is received, then a new message with the subsequent correlation + * If it is the first time a correlation id is received, then a new message with the subsequent correlation * id is sent. This ensures that the message is only sent once per Correlation id. - * + * * @param receivedId The received correlation id. */ public static synchronized void handleMessageDistribution(int receivedId) { - if(receivedId > idReached) { + if (receivedId > idReached) { idReached = receivedId; sendMessageWithId(idReached + 1); } @@ -281,18 +292,23 @@ public static synchronized void handleMessageDistribution(int receivedId) { } /** - * Messagelistener which notifies the 'handleMessageDistribution' method with the correlation id whenever + * Messagelistener which notifies the 'handleMessageDistribution' method with the correlation id whenever * a message it received. * Otherwise counts the amount of received messages. */ - private class NotificationMessageListener implements MessageListener { - /** The message bus.*/ + private static class NotificationMessageListener implements MessageListener { + /** + * The message bus. + */ private final MessageBus bus; - /** The amount of messages received.*/ + /** + * The amount of messages received. + */ private int count; /** * Constructor. + * * @param settings The configuration for defining the message bus. */ public NotificationMessageListener(Settings settings, SecurityManager securityManager) { @@ -311,6 +327,7 @@ public void stop() { /** * Retrieval of the amount of messages caught by the listener. + * * @return The number of message received by this. */ public int getCount() { diff --git a/bitrepository-core/src/test/java/org/bitrepository/protocol/performancetest/MessageBusNumberOfMessagesStressTest.java b/bitrepository-core/src/test/java/org/bitrepository/protocol/performancetest/MessageBusNumberOfMessagesStressTest.java index a21a482c3..3c6742ec0 100644 --- a/bitrepository-core/src/test/java/org/bitrepository/protocol/performancetest/MessageBusNumberOfMessagesStressTest.java +++ b/bitrepository-core/src/test/java/org/bitrepository/protocol/performancetest/MessageBusNumberOfMessagesStressTest.java @@ -58,9 +58,8 @@ public void initializeSettings() { } /** - * Tests the amount of messages send over a message bus, which is not placed locally. - * Requires to send at least five per second. - * @throws Exception + * Tests the amount of messages sent over a message bus, which is not placed locally. + * Require sending at least five messages per second. */ @Test( groups = {"StressTest"} ) public void SendManyMessagesDistributed() throws Exception { @@ -74,7 +73,7 @@ public void SendManyMessagesDistributed() throws Exception { ResendMessageListener listener = null; try { - addStep("Initialise the messagelistener", "Should be allowed."); + addStep("Initialise the message-listener", "Should be allowed."); listener = new ResendMessageListener(settings); addStep("Start sending at '" + new Date() + "'", "Should just be waiting."); @@ -105,7 +104,6 @@ public void SendManyMessagesDistributed() throws Exception { /** * Tests the amount of messages send through a local messagebus. * It should be at least 20 per second. - * @throws Exception */ @Test( groups = {"StressTest"} ) public void SendManyMessagesLocally() throws Exception { @@ -128,7 +126,7 @@ public void SendManyMessagesLocally() throws Exception { addStep("Starting the broker.", "Should be allowed"); broker.start(); - addStep("Initialise the messagelistener", "Should be allowed."); + addStep("Initialise the message-listener", "Should be allowed."); listener = new ResendMessageListener(settings); addStep("Start sending at '" + new Date() + "'", "Should just be waiting."); @@ -151,12 +149,8 @@ public void SendManyMessagesLocally() throws Exception { } finally { if(listener != null) { listener.stop(); - listener = null; - } - if(broker != null) { - broker.stop(); - broker = null; } + broker.stop(); } } @@ -165,11 +159,9 @@ public void SendManyMessagesLocally() throws Exception { * It does not reply, it send to the same destination, thus receiving it again. * It keeps track of the amount of messages received. */ - private class ResendMessageListener implements MessageListener { + private static class ResendMessageListener implements MessageListener { /** The message bus.*/ private final MessageBus bus; - /** The mocked SecurityManager */ - private SecurityManager securityManager = new DummySecurityManager(); /** The amount of messages received.*/ private int count; @@ -178,6 +170,8 @@ private class ResendMessageListener implements MessageListener { * @param conf The configurations for declaring the message bus. */ public ResendMessageListener(Settings conf) { + /* The mocked SecurityManager */ + SecurityManager securityManager = new DummySecurityManager(); this.bus = new ActiveMQMessageBus(conf, securityManager); this.count = 0; @@ -185,7 +179,7 @@ public ResendMessageListener(Settings conf) { } /** - * Method for stopping interaction with the messagelistener. + * Method for stopping interaction with the message-listener. */ public void stop() { bus.removeListener(QUEUE, this); diff --git a/bitrepository-core/src/test/java/org/bitrepository/protocol/performancetest/MessageBusSizeOfMessageStressTest.java b/bitrepository-core/src/test/java/org/bitrepository/protocol/performancetest/MessageBusSizeOfMessageStressTest.java index 8ca722ab9..68be6421c 100644 --- a/bitrepository-core/src/test/java/org/bitrepository/protocol/performancetest/MessageBusSizeOfMessageStressTest.java +++ b/bitrepository-core/src/test/java/org/bitrepository/protocol/performancetest/MessageBusSizeOfMessageStressTest.java @@ -1,23 +1,23 @@ /* * #%L * Bitmagasin integrationstest - * + * * $Id$ * $HeadURL$ * %% * Copyright (C) 2010 The State and University Library, The Royal Library and The State Archives, Denmark * %% * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 2.1 of the + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 2.1 of the * License, or (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public + * + * You should have received a copy of the GNU General Lesser Public * License along with this program. If not, see * . * #L% @@ -49,38 +49,26 @@ import java.util.Date; /** - * Stress testing of the messagebus. - * + * Stress testing of the messagebus. + *

* The size is regulated by the 'BUFFER_TEXT' and the 'NUMBER_OF_REPEATS_OF_BUFFER_TEXT'. - * Currently the buffer text is 100 bytes, and it is repeated 100 times, thus generating a message of size 10 kB. + * Currently, the buffer text is 100 bytes, and it is repeated 100 times, thus generating a message of size 10 kB. */ public class MessageBusSizeOfMessageStressTest extends ExtendedTestCase { - /** The time to wait when sending a message before it definitely should - * have been consumed by a listener.*/ - static final int TIME_FOR_MESSAGE_TRANSFER_WAIT = 500; - /** The name of the queue to send the messages.*/ private static String QUEUE = "TEST-QUEUE"; - /** The timeframe for the test.*/ private final long TIME_FRAME = 60000L; - - /** The text to repeat to make the message large.*/ - private final String BUFFER_TEXT = "098765432109876543210987654321" - + "0987654321098765432109876543210987654321098765432109876543210987654321"; // 100 bytes - /** The number of repeats by the buffer text in the message.*/ - private final int NUMBER_OF_REPEATS_OF_BUFFER_TEXT = 100; - private Settings settings; @BeforeMethod public void initializeSettings() { settings = TestSettingsProvider.getSettings(getClass().getSimpleName()); } + /** - * Tests the amount of messages send over a message bus, which is not placed locally. - * Requires to send at least five per second. - * @throws Exception + * Tests the amount of messages sent over a message bus, which is not placed locally. + * Requires sending at least five per second. */ -// @Test( groups = {"StressTest"} ) + /* @Test( groups = {"StressTest"} ) */ public void SendLargeMessagesDistributed() throws Exception { addDescription("Tests how many messages can be handled within a given timeframe."); addStep("Define constants", "This should not be possible to fail."); @@ -91,8 +79,8 @@ public void SendLargeMessagesDistributed() throws Exception { ResendMessageListener listener = null; try { - addStep("Initialise the messagelistener", "Should be allowed."); - listener = new ResendMessageListener(conf); + addStep("Initialise the message-listener", "Should be allowed."); + listener = new ResendMessageListener(); AlarmMessage message = getTestMessage(); @@ -102,27 +90,26 @@ public void SendLargeMessagesDistributed() throws Exception { try { wait(TIME_FRAME); } catch (InterruptedException e) { -// e.printStackTrace(); + /* e.printStackTrace(); */ } } addStep("Validating messages have been sent.", "Should be OK"); int count = listener.getCount(); Assert.assertTrue(count > 0, "Some message should have been sent."); - System.out.println("Sent '" + count + "' messages in '" + TIME_FRAME/1000 + "' seconds."); + System.out.println("Sent '" + count + "' messages in '" + TIME_FRAME / 1000 + "' seconds."); } finally { - if(listener != null) { + if (listener != null) { listener.stop(); - listener = null; } } } /** - * Tests the amount of messages send through a local messagebus. + * Tests the amount of messages sent through a local messagebus. * It should be at least 20 per second. */ - @Test( groups = {"StressTest"} ) + @Test(groups = {"StressTest"}) public void SendLargeMessagesLocally() throws Exception { addDescription("Tests how many messages can be handled within a given timeframe."); addStep("Define constants", "This should not be possible to fail."); @@ -140,8 +127,8 @@ public void SendLargeMessagesLocally() throws Exception { addStep("Starting the broker.", "Should be allowed"); broker.start(); - addStep("Initialise the messagelistener", "Should be allowed."); - listener = new ResendMessageListener(conf); + addStep("Initialise the message-listener", "Should be allowed."); + listener = new ResendMessageListener(); AlarmMessage message = getTestMessage(); @@ -151,34 +138,30 @@ public void SendLargeMessagesLocally() throws Exception { try { wait(TIME_FRAME); } catch (InterruptedException e) { -// e.printStackTrace(); + /* e.printStackTrace(); */ } } - // addStep("Stopped sending at '" + new Date() + "'", "Should have send more than '" + messagePerSec - // + "' messages per sec."); addStep("Validating the number of messages sent.", "Should be OK"); int count = listener.getCount(); - // Assert.assertTrue(count > (messagePerSec * timeFrame/1000), "There where send '" + count - // + "' messages in '" + timeFrame/1000 + "' seconds, but it is required to handle at least '" - // + messagePerSec + "' per second!"); - System.out.println("Sent '" + count + "' messages in '" + TIME_FRAME/1000 + "' seconds."); + System.out.println("Sent '" + count + "' messages in '" + TIME_FRAME / 1000 + "' seconds."); } finally { - if(listener != null) { + if (listener != null) { listener.stop(); - listener = null; - } - if(broker != null) { - broker.stop(); - broker = null; } + broker.stop(); } } private AlarmMessage getTestMessage() throws Exception { addStep("Creating the payload of the message.", "should be OK."); StringBuilder payload = new StringBuilder(); - for(int i = 0; i < NUMBER_OF_REPEATS_OF_BUFFER_TEXT; i++) { + /* The number of repeats by the buffer text in the message.*/ + int NUMBER_OF_REPEATS_OF_BUFFER_TEXT = 100; + for (int i = 0; i < NUMBER_OF_REPEATS_OF_BUFFER_TEXT; i++) { + /* The text to repeat to make the message large (100 bytes).*/ + String BUFFER_TEXT = "098765432109876543210987654321" + + "0987654321098765432109876543210987654321098765432109876543210987654321"; payload.append(BUFFER_TEXT); } @@ -194,23 +177,17 @@ private AlarmMessage getTestMessage() throws Exception { } /** - * Messagelistener which only resends the messages it receive. - * It does not reply, it send to the same destination, thus receiving it again. + * Message-listener which only resends the messages it receives. + * It does not reply, it sends to the same destination, thus receiving it again. * It keeps track of the amount of messages received. */ private class ResendMessageListener implements MessageListener { - /** The message bus.*/ private final MessageBus bus; - /** The mocked SecurityManager */ - private SecurityManager securityManager = new DummySecurityManager(); - /** The amount of messages received.*/ private int count; - /** - * Constructor. - * @param conf The configurations for declaring the messagebus. - */ - public ResendMessageListener(MessageBusConfiguration conf) { + public ResendMessageListener() { + /* Mocked SecurityManager */ + SecurityManager securityManager = new DummySecurityManager(); this.bus = new ActiveMQMessageBus(settings, securityManager); this.count = 0; @@ -218,7 +195,7 @@ public ResendMessageListener(MessageBusConfiguration conf) { } /** - * Method for stopping interaction with the messagelistener. + * Method for stopping interaction with the message-listener. */ public void stop() { bus.removeListener(QUEUE, this); @@ -226,6 +203,7 @@ public void stop() { /** * Retrieval of the amount of messages caught by the listener. + * * @return The number of message received by this. */ public int getCount() { @@ -234,9 +212,8 @@ public int getCount() { /** * Starts sending messages. - * @throws Exception If a problem with creating the message occurs. */ - public void startSending(AlarmMessage message) throws Exception { + public void startSending(AlarmMessage message) { message.setDestination(QUEUE); bus.sendMessage(message); } diff --git a/bitrepository-core/src/test/java/org/bitrepository/protocol/performancetest/MessageBusTimeToSendMessagesStressTest.java b/bitrepository-core/src/test/java/org/bitrepository/protocol/performancetest/MessageBusTimeToSendMessagesStressTest.java index 63eb13ae1..f5827a9c4 100644 --- a/bitrepository-core/src/test/java/org/bitrepository/protocol/performancetest/MessageBusTimeToSendMessagesStressTest.java +++ b/bitrepository-core/src/test/java/org/bitrepository/protocol/performancetest/MessageBusTimeToSendMessagesStressTest.java @@ -1,23 +1,23 @@ /* * #%L * Bitmagasin integrationstest - * + * * $Id$ * $HeadURL$ * %% * Copyright (C) 2010 The State and University Library, The Royal Library and The State Archives, Denmark * %% * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 2.1 of the + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 2.1 of the * License, or (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public + * + * You should have received a copy of the GNU General Lesser Public * License along with this program. If not, see * . * #L% @@ -46,22 +46,18 @@ import java.util.Date; /** - * Stress testing of the messagebus. + * Stress testing of the messagebus. */ public class MessageBusTimeToSendMessagesStressTest extends ExtendedTestCase { - /** The time to wait when sending a message before it definitely should + /** The time to wait when sending a message before it definitely should * have been consumed by a listener.*/ static final int TIME_FOR_MESSAGE_TRANSFER_WAIT = 500; /** The name of the queue to send the messages.*/ private static String QUEUE = "TEST-QUEUE"; /** The number of messages to send.*/ - private static int NUMBER_OF_MESSAGES = 1000; - /** The number of threads to send the messages. */ - private static int NUMBER_OF_SENDERS = 10; - + private static final int NUMBER_OF_MESSAGES = 1000; /** The date for start sending the messages.*/ private static Date startSending; - private Settings settings; @BeforeMethod @@ -69,12 +65,11 @@ public void initializeSettings() { settings = TestSettingsProvider.getSettings(getClass().getSimpleName()); } /** - * Tests the amount of messages send over a message bus, which is not placed locally. - * Requires to send at least five per second. - * @throws Exception + * Tests the amount of messages sent over a message bus, which is not placed locally. + * Require sending at least five per second. */ - // @Test( groups = {"StressTest"} ) - public void SendManyMessagesDistributed() throws Exception { + /* @Test( groups = {"StressTest"} ) */ + public void SendManyMessagesDistributed() { addDescription("Tests how fast a given number of messages can be handled."); addStep("Define constants", "This should not be possible to fail."); QUEUE += "-" + (new Date()).getTime(); @@ -85,27 +80,26 @@ public void SendManyMessagesDistributed() throws Exception { CountMessagesListener listener = null; try { - addStep("Initialise the messagelistener", "Should be allowed."); - listener = new CountMessagesListener(conf, securityManager); + addStep("Initialise the message-listener", "Should be allowed."); + listener = new CountMessagesListener(securityManager); startSending = new Date(); addStep("Start sending at '" + startSending + "'", "Should just be waiting."); sendAllTheMessages(conf, securityManager); - addStep("Sleept untill the listerner has received all the messages.", "Should be sleeping."); + addStep("Sleep until the listeners have received all the messages.", "Should be sleeping."); while(!listener.isFinished()) { synchronized (this) { try { wait(TIME_FOR_MESSAGE_TRANSFER_WAIT); } catch (InterruptedException e) { - // expected -// e.printStackTrace(); + /* e.printStackTrace(); */ } } } Date endDate = listener.getStopSending(); - addStep("Validating the count. Started at '" + startSending + "' and ended at '" + addStep("Validating the count. Started at '" + startSending + "' and ended at '" + endDate + "'", "Should not be wrong."); int count = listener.getCount(); @@ -114,15 +108,13 @@ public void SendManyMessagesDistributed() throws Exception { } finally { if(listener != null) { listener.stop(); - listener = null; } } } /** - * Tests the amount of messages send through a local messagebus. - * It should be at least 20 per second. - * @throws Exception + * Tests the amount of messages sent through a local messagebus. + * It should be at least 20 per second. */ @Test( groups = {"StressTest"} ) public void SendManyMessagesLocally() throws Exception { @@ -143,14 +135,14 @@ public void SendManyMessagesLocally() throws Exception { addStep("Starting the broker.", "Should be allowed"); broker.start(); - addStep("Initialise the messagelistener", "Should be allowed."); - listener = new CountMessagesListener(conf, securityManager); + addStep("Initialise the message-listener", "Should be allowed."); + listener = new CountMessagesListener(securityManager); startSending = new Date(); addStep("Start sending at '" + startSending + "'", "Should just be waiting."); sendAllTheMessages(conf, securityManager); - addStep("Sleep untill the listerner has received all the messages.", "Should be sleeping."); + addStep("Sleep until the listeners has received all the messages.", "Should be sleeping."); long startTime = new Date().getTime(); long oneMinuteInMillis = 60000; while(!listener.isFinished() && (new Date().getTime() - startTime) < oneMinuteInMillis) { @@ -163,7 +155,7 @@ public void SendManyMessagesLocally() throws Exception { } } - addStep("Validating the count. Started at '" + startSending + "' and ended at '" + addStep("Validating the count. Started at '" + startSending + "' and ended at '" + listener.getStopSending() + "'", "Should not be wrong."); int count = listener.getCount(); long timeFrame = (listener.getStopSending().getTime() - startSending.getTime())/1000; @@ -171,21 +163,18 @@ public void SendManyMessagesLocally() throws Exception { } finally { if(listener != null) { listener.stop(); - listener = null; - } - if(broker != null) { - broker.stop(); - broker = null; } + broker.stop(); } } /** * Sends the wanted amount of messages. * @param conf The configuration for the messagebus, where the messages should be sent. - * @throws Exception */ - private void sendAllTheMessages(MessageBusConfiguration conf, SecurityManager securityManager) throws Exception { + private void sendAllTheMessages(MessageBusConfiguration conf, SecurityManager securityManager) { + /* The number of threads to send the messages. */ + int NUMBER_OF_SENDERS = 10; for(int i = 0; i < NUMBER_OF_SENDERS; i++) { Thread t = new MessageSenderThread(conf, securityManager, NUMBER_OF_MESSAGES / NUMBER_OF_SENDERS, "#" + i); t.start(); @@ -220,25 +209,19 @@ public void run() { } /** - * Messagelistener which only resends the messages it receive. - * It does not reply, it send to the same destination, thus receiving it again. + * Message-listener which only resends the messages it receives. + * It does not reply, it sent to the same destination, thus receiving it again. * It keeps track of the amount of messages received. */ private class CountMessagesListener implements MessageListener { - /** The message bus.*/ private final MessageBus bus; - /** The amount of messages received.*/ private int count; private boolean awaitingMore = true; private Date stopSending; - /** - * Constructor. - * @param conf The configurations for declaring the messagebus. - */ - public CountMessagesListener(MessageBusConfiguration conf, SecurityManager securityManager) { + public CountMessagesListener(SecurityManager securityManager) { this.bus = new ActiveMQMessageBus(settings, securityManager); this.count = 0; @@ -246,7 +229,7 @@ public CountMessagesListener(MessageBusConfiguration conf, SecurityManager secur } /** - * Method for stopping interaction with the messagelistener. + * Method for stopping interaction with the message-listener. */ public void stop() { bus.removeListener(QUEUE, this); @@ -267,7 +250,6 @@ public void onMessage(Message message, MessageContext messageContext) { stopSending = new Date(); awaitingMore = false; } - } public Date getStopSending() { diff --git a/bitrepository-core/src/test/java/org/bitrepository/protocol/security/CertificateIDTest.java b/bitrepository-core/src/test/java/org/bitrepository/protocol/security/CertificateIDTest.java index 48d6b8390..976c11c44 100644 --- a/bitrepository-core/src/test/java/org/bitrepository/protocol/security/CertificateIDTest.java +++ b/bitrepository-core/src/test/java/org/bitrepository/protocol/security/CertificateIDTest.java @@ -5,16 +5,16 @@ * Copyright (C) 2010 - 2012 The State and University Library, The Royal Library and The State Archives, Denmark * %% * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 2.1 of the + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 2.1 of the * License, or (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public + * + * You should have received a copy of the GNU General Lesser Public * License along with this program. If not, see * . * #L% @@ -38,63 +38,62 @@ import java.security.cert.CertificateFactory; import java.security.cert.X509Certificate; -public class CertificateIDTest extends ExtendedTestCase { - +public class CertificateIDTest extends ExtendedTestCase { + @Test(groups = {"regressiontest"}) public void positiveCertificateIdentificationTest() throws Exception { addDescription("Tests that a certificate can be identified based on the correct signature."); addStep("Create CertificateID object based on the certificate used to sign the data", "CertificateID object not null"); Security.addProvider(new BouncyCastleProvider()); - + ByteArrayInputStream bs = new ByteArrayInputStream( SecurityTestConstants.getPositiveCertificate().getBytes(SecurityModuleConstants.defaultEncodingType)); X509Certificate myCertificate = (X509Certificate) CertificateFactory.getInstance( SecurityModuleConstants.CertificateType).generateCertificate(bs); CertificateID certificateIDfromCertificate = new CertificateID(myCertificate.getIssuerX500Principal(), myCertificate.getSerialNumber()); - + addStep("Create CertificateID object based on signature", "Certificate object not null"); byte[] decodeSig = Base64.decode(SecurityTestConstants.getSignature().getBytes(StandardCharsets.UTF_8)); CMSSignedData s = new CMSSignedData(new CMSProcessableByteArray( SecurityTestConstants.getTestData().getBytes(SecurityModuleConstants.defaultEncodingType)), decodeSig); SignerInformation signer = s.getSignerInfos().getSigners().iterator().next(); - CertificateID certificateIDfromSignature = new CertificateID(signer.getSID().getIssuer(), signer.getSID().getSerialNumber()); - + CertificateID certificateIDFromSignature = new CertificateID(signer.getSID().getIssuer(), signer.getSID().getSerialNumber()); + addStep("Assert that the two CertificateID objects are equal", "Assert succeeds"); - Assert.assertEquals(certificateIDfromCertificate, certificateIDfromSignature); + Assert.assertEquals(certificateIDfromCertificate, certificateIDFromSignature); } - + @Test(groups = {"regressiontest"}) public void negativeCertificateIdentificationTest() throws Exception { addDescription("Tests that a certificate is not identified based on a incorrect signature."); addStep("Create CertificateID object based on a certificate not used for signing the data", "CertificateID object not null"); Security.addProvider(new BouncyCastleProvider()); - + ByteArrayInputStream bs = new ByteArrayInputStream( SecurityTestConstants.getNegativeCertificate().getBytes(SecurityModuleConstants.defaultEncodingType)); X509Certificate myCertificate = (X509Certificate) CertificateFactory.getInstance( SecurityModuleConstants.CertificateType).generateCertificate(bs); - CertificateID certificateIDfromCertificate = + CertificateID certificateIDFromCertificate = new CertificateID(myCertificate.getIssuerX500Principal(), myCertificate.getSerialNumber()); - - + addStep("Create CertificateID object based on signature", "Certificate object not null"); byte[] decodeSig = Base64.decode(SecurityTestConstants.getSignature().getBytes(StandardCharsets.UTF_8)); CMSSignedData s = new CMSSignedData(new CMSProcessableByteArray( SecurityTestConstants.getTestData().getBytes(SecurityModuleConstants.defaultEncodingType)), decodeSig); SignerInformation signer = s.getSignerInfos().getSigners().iterator().next(); - CertificateID certificateIDfromSignature = new CertificateID(signer.getSID().getIssuer(), signer.getSID().getSerialNumber()); - + CertificateID certificateIDFromSignature = new CertificateID(signer.getSID().getIssuer(), signer.getSID().getSerialNumber()); + addStep("Assert that the two CertificateID objects are equal", "Assert succeeds"); - Assert.assertNotSame((Object) certificateIDfromCertificate, (Object) certificateIDfromSignature); + Assert.assertNotSame(certificateIDFromCertificate, certificateIDFromSignature); } - + @Test(groups = {"regressiontest"}) public void equalTest() throws Exception { addDescription("Tests the equality of CertificateIDs"); addStep("Setup", ""); Security.addProvider(new BouncyCastleProvider()); - + ByteArrayInputStream bs = new ByteArrayInputStream( SecurityTestConstants.getNegativeCertificate().getBytes(SecurityModuleConstants.defaultEncodingType)); X509Certificate myCertificate = (X509Certificate) CertificateFactory.getInstance( @@ -106,40 +105,40 @@ public void equalTest() throws Exception { addStep("Validate the content of the certificateID", "Should be same as x509Certificate"); Assert.assertEquals(certificateID1.getIssuer(), issuer); Assert.assertEquals(certificateID1.getSerial(), serial); - + addStep("Test whether it equals it self", "should give positive result"); - Assert.assertTrue(certificateID1.equals(certificateID1)); - + Assert.assertEquals(certificateID1, certificateID1); + addStep("Test with a null as argument", "Should give negative result"); - Assert.assertFalse(certificateID1.equals(null)); - + Assert.assertNotEquals(certificateID1, null); + addStep("Test with another class", "Should give negative result"); - Assert.assertFalse(certificateID1.equals(new Object())); - + Assert.assertNotEquals(new Object(), certificateID1); + addStep("Test with same issuer but no serial", "Should give negative result"); - Assert.assertFalse(certificateID1.equals(new CertificateID(issuer, null))); - + Assert.assertNotEquals(new CertificateID(issuer, null), certificateID1); + addStep("Test with same serial but no issuer", "Should give negative result"); - Assert.assertFalse(certificateID1.equals(new CertificateID((X500Principal)null, serial))); - + Assert.assertNotEquals(new CertificateID((X500Principal) null, serial), certificateID1); + addStep("Test the positive case, with both the issuer and serial ", "Should give positive result"); - Assert.assertTrue(certificateID1.equals(new CertificateID(issuer, serial))); - + Assert.assertEquals(new CertificateID(issuer, serial), certificateID1); + addStep("Setup an empty certificate", ""); - CertificateID certificateID2 = new CertificateID((X500Principal)null, null); - + CertificateID certificateID2 = new CertificateID((X500Principal) null, null); + addStep("Test empty certificate against issuer but no serial", "Should give negative result"); - Assert.assertFalse(certificateID2.equals(new CertificateID(issuer, null))); - + Assert.assertNotEquals(new CertificateID(issuer, null), certificateID2); + addStep("Test empty certificate against serial but no issuer", "Should give negative result"); - Assert.assertFalse(certificateID2.equals(new CertificateID((X500Principal)null, serial))); - + Assert.assertNotEquals(new CertificateID((X500Principal) null, serial), certificateID2); + addStep("Test empty certificate against serial and issuer", "Should give negative result"); - Assert.assertFalse(certificateID2.equals(new CertificateID(issuer, serial))); - + Assert.assertNotEquals(new CertificateID(issuer, serial), certificateID2); + addStep("Test the positive case, with neither issuer nor serial", "Should give positive result"); - Assert.assertTrue(certificateID2.equals(new CertificateID((X500Principal)null, null))); - + Assert.assertEquals(new CertificateID((X500Principal) null, null), certificateID2); + addStep("Check the hash codes for the two certificate", "Should not be the same"); Assert.assertTrue(certificateID1.hashCode() != certificateID2.hashCode()); } diff --git a/bitrepository-core/src/test/java/org/bitrepository/protocol/security/DummySecurityManager.java b/bitrepository-core/src/test/java/org/bitrepository/protocol/security/DummySecurityManager.java index 5df7195c1..4824a35a8 100644 --- a/bitrepository-core/src/test/java/org/bitrepository/protocol/security/DummySecurityManager.java +++ b/bitrepository-core/src/test/java/org/bitrepository/protocol/security/DummySecurityManager.java @@ -5,62 +5,55 @@ * Copyright (C) 2010 - 2012 The State and University Library, The Royal Library and The State Archives, Denmark * %% * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 2.1 of the + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 2.1 of the * License, or (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public + * + * You should have received a copy of the GNU General Lesser Public * License along with this program. If not, see * . * #L% */ package org.bitrepository.protocol.security; -import org.bitrepository.protocol.security.exception.CertificateUseException; -import org.bitrepository.protocol.security.exception.MessageAuthenticationException; -import org.bitrepository.protocol.security.exception.MessageSigningException; -import org.bitrepository.protocol.security.exception.OperationAuthorizationException; -import org.bitrepository.protocol.security.exception.UnregisteredPermissionException; import org.bouncycastle.cms.SignerId; /** * Class containing empty / safe implementation of the SecurityManager interface. - * It is intented to be used in tests, or where the functionality of a real SecurityManager implementation is not - * needed. + * It is intended to be used in tests, or where the functionality of a real SecurityManager implementation is not + * needed. */ public class DummySecurityManager implements SecurityManager { @Override - public SignerId authenticateMessage(String message, String signature) throws MessageAuthenticationException { + public SignerId authenticateMessage(String message, String signature) { return null; } @Override - public String signMessage(String message) throws MessageSigningException { + public String signMessage(String message) { // Safe empty implementation return null; } @Override - public void authorizeOperation(String operationType, String messageData, String signature, String collectionID) - throws OperationAuthorizationException { + public void authorizeOperation(String operationType, String messageData, String signature, String collectionID) { // Safe empty implementation } @Override - public void authorizeCertificateUse(String certificateUser, String messageData, String signature) - throws CertificateUseException { + public void authorizeCertificateUse(String certificateUser, String messageData, String signature) { // Safe empty implementation } @Override - public String getCertificateFingerprint(SignerId signer) throws UnregisteredPermissionException { + public String getCertificateFingerprint(SignerId signer) { return null; } } diff --git a/bitrepository-core/src/test/java/org/bitrepository/protocol/security/PermissionStoreTest.java b/bitrepository-core/src/test/java/org/bitrepository/protocol/security/PermissionStoreTest.java index 8ab95f3dc..16e50b030 100644 --- a/bitrepository-core/src/test/java/org/bitrepository/protocol/security/PermissionStoreTest.java +++ b/bitrepository-core/src/test/java/org/bitrepository/protocol/security/PermissionStoreTest.java @@ -39,7 +39,6 @@ import static org.testng.Assert.assertEquals; public class PermissionStoreTest extends ExtendedTestCase { - private static final String componentID = "TEST"; private PermissionStore permissionStore; @@ -90,18 +89,18 @@ public void negativeCertificateRetrievalTest() throws Exception { } //@Test(groups = {"regressiontest"}) - public void certificatePermissionCheckTest() throws Exception { + public void certificatePermissionCheckTest() { addDescription("Tests that a certificate only allows for the expected permission."); } //@Test(groups = {"regressiontest"}) - public void unknownCertificatePermissionCheckTest() throws Exception { + public void unknownCertificatePermissionCheckTest() { addDescription("Tests that a unknown certificate results in expected refusal."); } @Test(groups = {"regressiontest"}) public void certificateFingerprintTest() throws Exception { - addDescription("Tests that a certificate fingerprint can correctly be retrived for a signer."); + addDescription("Tests that a certificate fingerprint can correctly be retrieved for a signer."); addFixture("Create signer to lookup fingerprint"); byte[] decodeSig = Base64.decode(SecurityTestConstants.getSignature().getBytes(SecurityModuleConstants.defaultEncodingType)); diff --git a/bitrepository-core/src/test/java/org/bitrepository/protocol/security/SecurityManagerTest.java b/bitrepository-core/src/test/java/org/bitrepository/protocol/security/SecurityManagerTest.java index 5b50b1511..eb88d0d12 100644 --- a/bitrepository-core/src/test/java/org/bitrepository/protocol/security/SecurityManagerTest.java +++ b/bitrepository-core/src/test/java/org/bitrepository/protocol/security/SecurityManagerTest.java @@ -5,16 +5,16 @@ * Copyright (C) 2010 - 2012 The State and University Library, The Royal Library and The State Archives, Denmark * %% * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 2.1 of the + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 2.1 of the * License, or (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public + * + * You should have received a copy of the GNU General Lesser Public * License along with this program. If not, see * . * #L% @@ -48,12 +48,12 @@ import java.nio.charset.StandardCharsets; import java.util.List; -public class SecurityManagerTest extends ExtendedTestCase { +public class SecurityManagerTest extends ExtendedTestCase { private final Logger log = LoggerFactory.getLogger(getClass()); private org.bitrepository.protocol.security.SecurityManager securityManager; private PermissionStore permissionStore; private Settings settings; - + @BeforeMethod(alwaysRun = true) public void setUp() throws Exception { settings = TestSettingsProvider.reloadSettings(getClass().getSimpleName()); @@ -62,67 +62,66 @@ public void setUp() throws Exception { settings.getRepositorySettings().setPermissionSet(SecurityTestConstants.getDefaultPermissions()); setupSecurityManager(settings); } - + private void setupSecurityManager(Settings settings) { permissionStore = new PermissionStore(); MessageAuthenticator authenticator = new BasicMessageAuthenticator(permissionStore); OperationAuthorizer authorizer = new BasicOperationAuthorizer(permissionStore); MessageSigner messageSigner = new BasicMessageSigner(); securityManager = new BasicSecurityManager(settings.getRepositorySettings(), - SecurityTestConstants.getKeyFile(), - authenticator, - messageSigner, - authorizer, + SecurityTestConstants.getKeyFile(), + authenticator, + messageSigner, + authorizer, permissionStore, SecurityTestConstants.getComponentID()); } - + @Test(groups = {"regressiontest"}) public void operationAuthorizationBehaviourTest() throws Exception { addDescription("Tests that a signature only allows the correct requests."); - - List collections = settings.getRepositorySettings().getCollections().getCollection(); - Assert.assertEquals(collections.size(), 2, + + List collections = settings.getRepositorySettings().getCollections().getCollection(); + Assert.assertEquals(collections.size(), 2, "There should be two collections present to test the collection limited authorization"); settings.getRepositorySettings().setPermissionSet(getCollectionLimitedPermissionSet()); setupSecurityManager(settings); - + String collectionID1 = settings.getRepositorySettings().getCollections().getCollection().get(0).getID(); String collectionID2 = settings.getRepositorySettings().getCollections().getCollection().get(1).getID(); - + addStep("Check that PUT_FILE is allowed for both collections.", "PUT_FILE is allowed."); try { - securityManager.authorizeOperation(PutFileRequest.class.getSimpleName(), + securityManager.authorizeOperation(PutFileRequest.class.getSimpleName(), SecurityTestConstants.getTestData(), SecurityTestConstants.getSignature(), collectionID1); } catch (OperationAuthorizationException e) { Assert.fail(e.getMessage()); } try { - securityManager.authorizeOperation(PutFileRequest.class.getSimpleName(), + securityManager.authorizeOperation(PutFileRequest.class.getSimpleName(), SecurityTestConstants.getTestData(), SecurityTestConstants.getSignature(), collectionID2); } catch (OperationAuthorizationException e) { Assert.fail(e.getMessage()); } - - addStep("Check that GET_FILE is only allowed for the first collection.", + + addStep("Check that GET_FILE is only allowed for the first collection.", "GET_FILE is allowed for first collection, and disallowed for the second collection (exception thrown)."); - - + + try { - securityManager.authorizeOperation(GetFileRequest.class.getSimpleName(), + securityManager.authorizeOperation(GetFileRequest.class.getSimpleName(), SecurityTestConstants.getTestData(), SecurityTestConstants.getSignature(), collectionID1); } catch (OperationAuthorizationException e) { Assert.fail(e.getMessage()); } - + try { - securityManager.authorizeOperation(GetFileRequest.class.getSimpleName(), + securityManager.authorizeOperation(GetFileRequest.class.getSimpleName(), SecurityTestConstants.getTestData(), SecurityTestConstants.getSignature(), collectionID2); Assert.fail("SecurityManager did not throw the expected OperationAuthorizationException"); - } catch (OperationAuthorizationException e) { - - } - } + } catch (OperationAuthorizationException ignored) { + } + } @Test(groups = {"regressiontest"}) public void certificateAuthorizationBehaviourTest() throws Exception { @@ -132,7 +131,7 @@ public void certificateAuthorizationBehaviourTest() throws Exception { permissionStore.loadPermissions(getSigningCertPermission(), SecurityTestConstants.getComponentID()); try { - securityManager.authorizeCertificateUse(SecurityTestConstants.getAllowedCertificateUser(), + securityManager.authorizeCertificateUse(SecurityTestConstants.getAllowedCertificateUser(), SecurityTestConstants.getTestData(), SecurityTestConstants.getSignature()); } catch (CertificateUseException e) { Assert.fail(e.getMessage()); @@ -140,19 +139,18 @@ public void certificateAuthorizationBehaviourTest() throws Exception { Assert.assertNotNull(getSigningCertPermission().getPermission().get(0).getCertificate().getAllowedCertificateUsers()); addStep("Check that an unregistered component is not allowed.", "The unregistered component is not allowed."); try { - securityManager.authorizeCertificateUse(SecurityTestConstants.getDisallowedCertificateUser(), + securityManager.authorizeCertificateUse(SecurityTestConstants.getDisallowedCertificateUser(), SecurityTestConstants.getTestData(), SecurityTestConstants.getSignature()); Assert.fail("SecurityManager did not throw the expected CertificateUseException"); - } catch (CertificateUseException e) { - - } - } + } catch (CertificateUseException ignored) { + } + } + - @Test(groups = {"regressiontest"}) public void positiveSigningAuthenticationRoundtripTest() throws Exception { - addDescription("Tests that a roundtrip of signing a request and afterwards authenticating is succedes."); - addStep("Sign a chunck of data.", "Data is signed succesfully"); + addDescription("Tests that a roundtrip of signing a request and afterwards authenticating is succeeds."); + addStep("Sign a chunk of data.", "Data is signed successfully"); String signature = null; try { signature = securityManager.signMessage(SecurityTestConstants.getTestData()); @@ -160,22 +158,23 @@ public void positiveSigningAuthenticationRoundtripTest() throws Exception { Assert.fail("Failed signing test data!", e); } permissionStore.loadPermissions(getSigningCertPermission(), SecurityTestConstants.getComponentID()); - - String signatureString = new String(Base64.encode(signature.getBytes(SecurityModuleConstants.defaultEncodingType)), StandardCharsets.UTF_8); + + String signatureString = new String(Base64.encode(signature.getBytes(SecurityModuleConstants.defaultEncodingType)), + StandardCharsets.UTF_8); log.info("Signature for testdata is: " + signatureString); - + addStep("Check signature matches the data ", "Signature and data matches"); try { securityManager.authenticateMessage(SecurityTestConstants.getTestData(), signature); } catch (MessageAuthenticationException e) { - Assert.fail("Failed authenticating test data!", e); - } + Assert.fail("Failed authenticating test data!", e); + } } - + @Test(groups = {"regressiontest"}) public void negativeSigningAuthenticationRoundtripUnkonwnCertificateTest() throws Exception { addDescription("Tests that a roundtrip of signing a request and afterwards authenticating it fails due to " + - "a unknown certificate."); + "a unknown certificate."); addStep("Sign a chunck of data.", "Data is signed succesfully"); String signature = null; try { @@ -183,24 +182,25 @@ public void negativeSigningAuthenticationRoundtripUnkonwnCertificateTest() throw } catch (MessageSigningException e) { Assert.fail("Failed signing test data!", e); } - String signatureString = new String(Base64.encode(signature.getBytes(SecurityModuleConstants.defaultEncodingType)), StandardCharsets.UTF_8); + String signatureString = new String(Base64.encode(signature.getBytes(SecurityModuleConstants.defaultEncodingType)), + StandardCharsets.UTF_8); log.info("Signature for testdata is: " + signatureString); - + addStep("Check signature matches the data", "Signature cant be matched as certificate is unknown."); try { securityManager.authenticateMessage(SecurityTestConstants.getTestData(), signature);//signatureString); Assert.fail("Authentication did not fail as expected"); } catch (MessageAuthenticationException e) { log.info(e.getMessage()); - } - } - + } + } + @Test(groups = {"regressiontest"}) public void negativeSigningAuthenticationRoundtripBadDataTest() throws Exception { addDescription("Tests that a roundtrip of signing a request and afterwards authenticating it fails " + - "due to bad data"); - addDescription("Tests that a roundtrip of signing a request and afterwards authenticating is succedes."); - addStep("Sign a chunck of data.", "Data is signed succesfully"); + "due to bad data"); + addDescription("Tests that a roundtrip of signing a request and afterwards authenticating is succeeds."); + addStep("Sign a chunk of data.", "Data is signed successfully"); String signature = null; try { signature = securityManager.signMessage(SecurityTestConstants.getTestData()); @@ -208,10 +208,11 @@ public void negativeSigningAuthenticationRoundtripBadDataTest() throws Exception Assert.fail("Failed signing test data!", e); } permissionStore.loadPermissions(getSigningCertPermission(), SecurityTestConstants.getComponentID()); - - String signatureString = new String(Base64.encode(signature.getBytes(SecurityModuleConstants.defaultEncodingType)), StandardCharsets.UTF_8); + + String signatureString = new String(Base64.encode(signature.getBytes(SecurityModuleConstants.defaultEncodingType)), + StandardCharsets.UTF_8); log.info("Signature for testdata is: " + signatureString); - + addStep("Check signature matches the data ", "Signature and data matches does not match"); String corruptData = SecurityTestConstants.getTestData() + "foobar"; try { @@ -219,48 +220,48 @@ public void negativeSigningAuthenticationRoundtripBadDataTest() throws Exception Assert.fail("Authentication did not fail as expected!"); } catch (MessageAuthenticationException e) { log.info(e.getMessage()); - } + } } private PermissionSet getCollectionLimitedPermissionSet() throws UnsupportedEncodingException { PermissionSet permissions = new PermissionSet(); Permission signingCertPerm = new Permission(); - + Certificate signingCert = new Certificate(); signingCert.setCertificateData(SecurityTestConstants.getPositiveCertificate() .getBytes(SecurityModuleConstants.defaultEncodingType)); - + signingCertPerm.setCertificate(signingCert); OperationPermission opPerm1 = new OperationPermission(); opPerm1.setOperation(Operation.PUT_FILE); signingCertPerm.getOperationPermission().add(opPerm1); - + OperationPermission opPerm2 = new OperationPermission(); opPerm2.setOperation(Operation.GET_FILE); opPerm2.getCollections().add(settings.getCollections().get(0).getID()); - signingCertPerm.getOperationPermission().add(opPerm2); - - permissions.getPermission().add(signingCertPerm); + signingCertPerm.getOperationPermission().add(opPerm2); + + permissions.getPermission().add(signingCertPerm); return permissions; } - + private PermissionSet getSigningCertPermission() throws UnsupportedEncodingException { PermissionSet permissions = new PermissionSet(); ComponentIDs allowedUsers = new ComponentIDs(); allowedUsers.getIDs().add(SecurityTestConstants.getAllowedCertificateUser()); Permission signingCertPerm = new Permission(); - + Certificate signingCert = new Certificate(); signingCert.setCertificateData(SecurityTestConstants.getSigningCertificate() .getBytes(SecurityModuleConstants.defaultEncodingType)); signingCert.setAllowedCertificateUsers(allowedUsers); - + signingCertPerm.setCertificate(signingCert); OperationPermission opPerm = new OperationPermission(); opPerm.setOperation(Operation.ALL); - signingCertPerm.getOperationPermission().add(opPerm); - permissions.getPermission().add(signingCertPerm); + signingCertPerm.getOperationPermission().add(opPerm); + permissions.getPermission().add(signingCertPerm); return permissions; } } diff --git a/bitrepository-core/src/test/java/org/bitrepository/protocol/security/SecurityTestConstants.java b/bitrepository-core/src/test/java/org/bitrepository/protocol/security/SecurityTestConstants.java index f413de482..8893afb57 100644 --- a/bitrepository-core/src/test/java/org/bitrepository/protocol/security/SecurityTestConstants.java +++ b/bitrepository-core/src/test/java/org/bitrepository/protocol/security/SecurityTestConstants.java @@ -5,16 +5,16 @@ * Copyright (C) 2010 - 2012 The State and University Library, The Royal Library and The State Archives, Denmark * %% * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 2.1 of the + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 2.1 of the * License, or (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public + * + * You should have received a copy of the GNU General Lesser Public * License along with this program. If not, see * . * #L% @@ -34,26 +34,26 @@ * Class to hold constants for used with the security module tests. */ public class SecurityTestConstants { - - /* - * Notes about how to update the certificates, signature etc. found - * as comments in the variables below + * Notes about how to update the certificates, signature etc. found + * as comments in the variables below */ private static final String DATA = "Hello world!"; // Use output from SignatureGeneratorTest to make a new signature if certificate changes - private static final String SIGNATURE_JAVA = "MIAGCSqGSIb3DQEHAqCAMIACAQExDzANBglghkgBZQMEAgMFADCABgkqhkiG9w0BBwEAADGCAZcwggGTAgEBMGwwXzELMAkGA1UEBhMCREsxEDAOBgNVBAgMB0Rlbm1hcmsxDzANBgNVBAcMBkFhcmh1czEaMBgGA1UECgwRQml0cmVwb3NpdG9yeS5vcmcxETAPBgNVBAMMCGNsaWVudDgwAgkAnwg/Emx5gB8wDQYJYIZIAWUDBAIDBQAwDQYJKoZIhvcNAQEBBQAEggEABkj3VQR6B5Lc0LZ7YAHvFCvDn2tCObPlLNpq0U1VRkLgIVMFQkZL4PpvG/MS/nEPI+VFz8Cty/UtODd23il6PnLjwwqYHKGCDnbJSg8YkX+UOO0NBr6Th6a6NWyeOElWiKxJ6pysXTV/b1DtY/uFFprLOPAgLVnp4TjZBtNuPFU+EBvvO1aFzKESpNPCs/qzF8Usf1ZqE+OPh49XLMqlKv4h8w1pjWLaZg4yCcqvPbqu1VjkvjNhkGHpc/k1whblBmMZ8JpdLlKCsWvj0+IuoztnClFQ/aDL1dkexnEaTf/CDhI8tFP1CEJZBUp0MjOtyPQbHGMe+1lxRWbsGYzusgAAAAAAAA=="; + private static final String SIGNATURE_JAVA = + "MIAGCSqGSIb3DQEHAqCAMIACAQExDzANBglghkgBZQMEAgMFADCABgkqhkiG9w0BBwEAADGCAZcwggGTAgEBMGwwXzELMAkGA1UEBhMCREsxEDAOBgNVBAgMB0Rlbm1hcmsxDzANBgNVBAcMBkFhcmh1czEaMBgGA1UECgwRQml0cmVwb3NpdG9yeS5vcmcxETAPBgNVBAMMCGNsaWVudDgwAgkAnwg/Emx5gB8wDQYJYIZIAWUDBAIDBQAwDQYJKoZIhvcNAQEBBQAEggEABkj3VQR6B5Lc0LZ7YAHvFCvDn2tCObPlLNpq0U1VRkLgIVMFQkZL4PpvG/MS/nEPI+VFz8Cty/UtODd23il6PnLjwwqYHKGCDnbJSg8YkX+UOO0NBr6Th6a6NWyeOElWiKxJ6pysXTV/b1DtY/uFFprLOPAgLVnp4TjZBtNuPFU+EBvvO1aFzKESpNPCs/qzF8Usf1ZqE+OPh49XLMqlKv4h8w1pjWLaZg4yCcqvPbqu1VjkvjNhkGHpc/k1whblBmMZ8JpdLlKCsWvj0+IuoztnClFQ/aDL1dkexnEaTf/CDhI8tFP1CEJZBUp0MjOtyPQbHGMe+1lxRWbsGYzusgAAAAAAAA=="; // Use openssl to generate signature: // openssl smime -sign -md sha512 -binary -nocerts -noattr -in message -out new.sig -outform der -inkey pkey.pem -signer cert.pem // base64 new.sig > signature.openssl // After which concatenate the lines of signature.openssl to a single line - private static final String SIGNATURE = "MIIBzwYJKoZIhvcNAQcCoIIBwDCCAbwCAQExDzANBglghkgBZQMEAgMFADALBgkqhkiG9w0BBwExggGXMIIBkwIBATBsMF8xCzAJBgNVBAYTAkRLMRAwDgYDVQQIDAdEZW5tYXJrMQ8wDQYDVQQHDAZBYXJodXMxGjAYBgNVBAoMEUJpdHJlcG9zaXRvcnkub3JnMREwDwYDVQQDDAhjbGllbnQ4MAIJAJ8IPxJseYAfMA0GCWCGSAFlAwQCAwUAMA0GCSqGSIb3DQEBAQUABIIBAAZI91UEegeS3NC2e2AB7xQrw59rQjmz5SzaatFNVUZC4CFTBUJGS+D6bxvzEv5xDyPlRc/Arcv1LTg3dt4pej5y48MKmByhgg52yUoPGJF/lDjtDQa+k4emujVsnjhJVoisSeqcrF01f29Q7WP7hRaayzjwIC1Z6eE42QbTbjxVPhAb7ztWhcyhEqTTwrP6sxfFLH9WahPjj4ePVyzKpSr+IfMNaY1i2mYOMgnKrz26rtVY5L4zYZBh6XP5NcIW5QZjGfCaXS5SgrFr49PiLqM7ZwpRUP2gy9XZHsZxGk3/wg4SPLRT9QhCWQVKdDIzrcj0GxxjHvtZcUVm7BmM7rI="; - + private static final String SIGNATURE = + "MIIBzwYJKoZIhvcNAQcCoIIBwDCCAbwCAQExDzANBglghkgBZQMEAgMFADALBgkqhkiG9w0BBwExggGXMIIBkwIBATBsMF8xCzAJBgNVBAYTAkRLMRAwDgYDVQQIDAdEZW5tYXJrMQ8wDQYDVQQHDAZBYXJodXMxGjAYBgNVBAoMEUJpdHJlcG9zaXRvcnkub3JnMREwDwYDVQQDDAhjbGllbnQ4MAIJAJ8IPxJseYAfMA0GCWCGSAFlAwQCAwUAMA0GCSqGSIb3DQEBAQUABIIBAAZI91UEegeS3NC2e2AB7xQrw59rQjmz5SzaatFNVUZC4CFTBUJGS+D6bxvzEv5xDyPlRc/Arcv1LTg3dt4pej5y48MKmByhgg52yUoPGJF/lDjtDQa+k4emujVsnjhJVoisSeqcrF01f29Q7WP7hRaayzjwIC1Z6eE42QbTbjxVPhAb7ztWhcyhEqTTwrP6sxfFLH9WahPjj4ePVyzKpSr+IfMNaY1i2mYOMgnKrz26rtVY5L4zYZBh6XP5NcIW5QZjGfCaXS5SgrFr49PiLqM7ZwpRUP2gy9XZHsZxGk3/wg4SPLRT9QhCWQVKdDIzrcj0GxxjHvtZcUVm7BmM7rI="; + // When certificate POSITIVECERT is changed, use openssl x509 -in -fingerprint to obtain new fingerprint private static final String FINGERPRINT = "D3:CC:F2:AE:36:4C:FB:85:F0:70:9A:59:8F:14:EF:8B:52:D4:A5:30"; - + private static final String POSITIVECERT_KEYFILE = "./target/test-classes/client80-certkey.pem"; - + /* currently client80-certkey.pem */ private static final String POSITIVECERT = "-----BEGIN CERTIFICATE-----\n" + "MIIDkTCCAnmgAwIBAgIJAJ8IPxJseYAfMA0GCSqGSIb3DQEBBQUAMF8xCzAJBgNV\n" + @@ -77,7 +77,7 @@ public class SecurityTestConstants { "YEn++kulWJnt4beI6UruCwCqCtBRKR38cPahK6Ic168h99ztO6JuvSm3v9LpDtXl\n" + "vSBi6TA=\n" + "-----END CERTIFICATE-----\n"; - + /* currently client90-certkey.pem */ private static final String NEGATIVECERT = "-----BEGIN CERTIFICATE-----\n" + "MIIDkTCCAnmgAwIBAgIJAK/RxZXju3LcMA0GCSqGSIb3DQEBBQUAMF8xCzAJBgNV\n" + @@ -101,36 +101,35 @@ public class SecurityTestConstants { "ldNjVbpoXMzQcYxC1kzgjAXgazxis4q1DQaAnCfIp3VstK+ilbeZA3p7Tda3Zh/H\n" + "njr8Biw=\n" + "-----END CERTIFICATE-----\n"; - + /* currently client100-certkey.pem */ private static final String SIGNINGCERT = "-----BEGIN CERTIFICATE-----\n" + - "MIIDkzCCAnugAwIBAgIJALlIlDh730tYMA0GCSqGSIb3DQEBBQUAMGAxCzAJBgNV\n" + - "BAYTAkRLMRAwDgYDVQQIDAdEZW5tYXJrMQ8wDQYDVQQHDAZBYXJodXMxGjAYBgNV\n" + - "BAoMEUJpdHJlcG9zaXRvcnkub3JnMRIwEAYDVQQDDAljbGllbnQxMDAwHhcNMTQw\n" + - "NzI5MTQwMzEyWhcNMjQwNzI2MTQwMzEyWjBgMQswCQYDVQQGEwJESzEQMA4GA1UE\n" + - "CAwHRGVubWFyazEPMA0GA1UEBwwGQWFyaHVzMRowGAYDVQQKDBFCaXRyZXBvc2l0\n" + - "b3J5Lm9yZzESMBAGA1UEAwwJY2xpZW50MTAwMIIBIjANBgkqhkiG9w0BAQEFAAOC\n" + - "AQ8AMIIBCgKCAQEAr+8QNcRWPhyCmDt23K4WIRPLiwcu5jJHnopBWhQMp63K2ySX\n" + - "j4iHXc4Qd9Ug+vGh2Max39I1xPfKJ5WliddAzzwh69R3ICQ2fyESRlaDN5RP9ngC\n" + - "927CHbC2qgruVzM5AcsVWdv6NJi75peui0YkD2mYs8zKpgM4Ys5DeI6mfH9OAyvX\n" + - "nn0QOZW3gTazBQccxWgBAGbMpyKsfsEh4nP8BDJEO82znK61K4qJ2c1+tlTwg2Nt\n" + - "+aWz4mBiLnzZtZ8gJlspDMA0WpVgPlc6MU0kd+cJVCa4gbuWNoOm/ifoYS15RlWt\n" + - "vQhnqFK7d9UwBv/fVM8NfdbJaeooyWdobf24IQIDAQABo1AwTjAdBgNVHQ4EFgQU\n" + - "vFhO3LofBvKUKayjeLO4JCLxYewwHwYDVR0jBBgwFoAUvFhO3LofBvKUKayjeLO4\n" + - "JCLxYewwDAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQUFAAOCAQEABUjBPYK+6zWq\n" + - "IOx/Thhr8ccTHitBLIYlN8mOy5v6odYtJpVo4/EuBNSSrY9w6EZA7fpp8u8X3xhg\n" + - "Sp0Znd25SY6h1wWZztlP2RuPj1SI5pP10wEHD8sEUhimMnGbtxsEi6IaWRL9qki4\n" + - "uLwKg6OyL9lxduaElS1hUFYMU5LXs9HdRzz8JHmlb6SfWLTvi6TRUZjvEUQ11TGJ\n" + - "93qyZ0+1xJ9bKW3xB8yDybd67PkF7UYIyZXhGiMS9vlIeX7h107/4IT+EEe7vEyb\n" + - "XfmVbCmlkuHZwJPLwawmOUACfYUDsk0A7dB5cSaMwcxYi4TNZOYII7rrSZ85UCgP\n" + - "84Eon1U3cw==\n" + - "-----END CERTIFICATE-----\n"; - - + "MIIDkzCCAnugAwIBAgIJALlIlDh730tYMA0GCSqGSIb3DQEBBQUAMGAxCzAJBgNV\n" + + "BAYTAkRLMRAwDgYDVQQIDAdEZW5tYXJrMQ8wDQYDVQQHDAZBYXJodXMxGjAYBgNV\n" + + "BAoMEUJpdHJlcG9zaXRvcnkub3JnMRIwEAYDVQQDDAljbGllbnQxMDAwHhcNMTQw\n" + + "NzI5MTQwMzEyWhcNMjQwNzI2MTQwMzEyWjBgMQswCQYDVQQGEwJESzEQMA4GA1UE\n" + + "CAwHRGVubWFyazEPMA0GA1UEBwwGQWFyaHVzMRowGAYDVQQKDBFCaXRyZXBvc2l0\n" + + "b3J5Lm9yZzESMBAGA1UEAwwJY2xpZW50MTAwMIIBIjANBgkqhkiG9w0BAQEFAAOC\n" + + "AQ8AMIIBCgKCAQEAr+8QNcRWPhyCmDt23K4WIRPLiwcu5jJHnopBWhQMp63K2ySX\n" + + "j4iHXc4Qd9Ug+vGh2Max39I1xPfKJ5WliddAzzwh69R3ICQ2fyESRlaDN5RP9ngC\n" + + "927CHbC2qgruVzM5AcsVWdv6NJi75peui0YkD2mYs8zKpgM4Ys5DeI6mfH9OAyvX\n" + + "nn0QOZW3gTazBQccxWgBAGbMpyKsfsEh4nP8BDJEO82znK61K4qJ2c1+tlTwg2Nt\n" + + "+aWz4mBiLnzZtZ8gJlspDMA0WpVgPlc6MU0kd+cJVCa4gbuWNoOm/ifoYS15RlWt\n" + + "vQhnqFK7d9UwBv/fVM8NfdbJaeooyWdobf24IQIDAQABo1AwTjAdBgNVHQ4EFgQU\n" + + "vFhO3LofBvKUKayjeLO4JCLxYewwHwYDVR0jBBgwFoAUvFhO3LofBvKUKayjeLO4\n" + + "JCLxYewwDAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQUFAAOCAQEABUjBPYK+6zWq\n" + + "IOx/Thhr8ccTHitBLIYlN8mOy5v6odYtJpVo4/EuBNSSrY9w6EZA7fpp8u8X3xhg\n" + + "Sp0Znd25SY6h1wWZztlP2RuPj1SI5pP10wEHD8sEUhimMnGbtxsEi6IaWRL9qki4\n" + + "uLwKg6OyL9lxduaElS1hUFYMU5LXs9HdRzz8JHmlb6SfWLTvi6TRUZjvEUQ11TGJ\n" + + "93qyZ0+1xJ9bKW3xB8yDybd67PkF7UYIyZXhGiMS9vlIeX7h107/4IT+EEe7vEyb\n" + + "XfmVbCmlkuHZwJPLwawmOUACfYUDsk0A7dB5cSaMwcxYi4TNZOYII7rrSZ85UCgP\n" + + "84Eon1U3cw==\n" + + "-----END CERTIFICATE-----\n"; + private static final String KEYFILE = "./target/test-classes/client100-certkey.pem"; - + private static final String ALLOWEDCERTIFICATEUSER = "test-component"; private static final String COMPONENTID = "TEST"; @@ -138,52 +137,52 @@ public class SecurityTestConstants { public static String getPositiveCertKeyFile() { return POSITIVECERT_KEYFILE; } - + public static String getKeyFile() { return KEYFILE; } - + public static String getTestData() { return DATA; } - + public static String getFingerprintForSignatureCert() { return FINGERPRINT; } - + public static String getSignature() { return SIGNATURE; } - + public static String getPositiveCertificate() { return POSITIVECERT; } - + public static String getNegativeCertificate() { return NEGATIVECERT; } - + public static String getSigningCertificate() { return SIGNINGCERT; } - + public static String getAllowedCertificateUser() { return ALLOWEDCERTIFICATEUSER; } - + public static String getDisallowedCertificateUser() { return ALLOWEDCERTIFICATEUSER + "-bad"; } - + public static String getComponentID() { return COMPONENTID; } - + public static PermissionSet getDefaultPermissions() { PermissionSet permissions = new PermissionSet(); ComponentIDs allowedUsers = new ComponentIDs(); allowedUsers.getIDs().add(ALLOWEDCERTIFICATEUSER); - + Permission perm1 = new Permission(); Certificate cert1 = new Certificate(); cert1.setCertificateData(POSITIVECERT.getBytes(StandardCharsets.UTF_8)); @@ -192,17 +191,17 @@ public static PermissionSet getDefaultPermissions() { OperationPermission opPerm = new OperationPermission(); opPerm.setOperation(Operation.GET_FILE); perm1.getOperationPermission().add(opPerm); - + Permission perm2 = new Permission(); Certificate cert2 = new Certificate(); cert2.setCertificateData(NEGATIVECERT.getBytes(StandardCharsets.UTF_8)); cert2.setAllowedCertificateUsers(allowedUsers); perm2.setCertificate(cert2); - + permissions.getPermission().add(perm1); permissions.getPermission().add(perm2); return permissions; } - + } diff --git a/bitrepository-core/src/test/java/org/bitrepository/protocol/security/SignatureGenetatorTest.java b/bitrepository-core/src/test/java/org/bitrepository/protocol/security/SignatureGeneratorTest.java similarity index 97% rename from bitrepository-core/src/test/java/org/bitrepository/protocol/security/SignatureGenetatorTest.java rename to bitrepository-core/src/test/java/org/bitrepository/protocol/security/SignatureGeneratorTest.java index 519e1b782..f1b39900f 100644 --- a/bitrepository-core/src/test/java/org/bitrepository/protocol/security/SignatureGenetatorTest.java +++ b/bitrepository-core/src/test/java/org/bitrepository/protocol/security/SignatureGeneratorTest.java @@ -5,8 +5,7 @@ import org.bitrepository.protocol.security.exception.MessageSigningException; import org.testng.annotations.Test; -public class SignatureGenetatorTest { - +public class SignatureGeneratorTest { /* * Test to generate new signature for SecurityTestConstants */ diff --git a/bitrepository-core/src/test/java/org/bitrepository/protocol/utils/ConfigLoaderTest.java b/bitrepository-core/src/test/java/org/bitrepository/protocol/utils/ConfigLoaderTest.java index 22b14c908..63f61a3c9 100644 --- a/bitrepository-core/src/test/java/org/bitrepository/protocol/utils/ConfigLoaderTest.java +++ b/bitrepository-core/src/test/java/org/bitrepository/protocol/utils/ConfigLoaderTest.java @@ -5,16 +5,16 @@ * Copyright (C) 2010 - 2012 The State and University Library, The Royal Library and The State Archives, Denmark * %% * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 2.1 of the + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 2.1 of the * License, or (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public + * + * You should have received a copy of the GNU General Lesser Public * License along with this program. If not, see * . * #L% @@ -31,27 +31,26 @@ import java.io.File; public class ConfigLoaderTest extends ExtendedTestCase { - String GOOD_FILE_PATH = "logback-test.xml"; - - @BeforeMethod (alwaysRun = true) + + @BeforeMethod(alwaysRun = true) public void setup() { FileUtils.copyFile(new File("src/test/resources/logback-test.xml"), new File(GOOD_FILE_PATH)); } - - @AfterMethod (alwaysRun = true) + + @AfterMethod(alwaysRun = true) public void teardown() { FileUtils.delete(new File(GOOD_FILE_PATH)); } - - @Test(groups = { "regressiontest" }) + + @Test(groups = {"regressiontest"}) public void testLoadingConfig() throws Exception { addDescription("Test the loading of a configuration file for the config loader."); addStep("Setup variables", ""); String badFilePath = "iDoNotExist.xml"; Assert.assertFalse(new File(badFilePath).exists()); Assert.assertTrue(new File(GOOD_FILE_PATH).exists()); - + addStep("Test with a invalid file path", "Should throw an exception"); try { new LogbackConfigLoader(badFilePath); @@ -59,12 +58,12 @@ public void testLoadingConfig() throws Exception { } catch (IllegalArgumentException e) { // expected } - + addStep("Test when the file is unreadable", "Should throw an exception"); File goodFile = new File(GOOD_FILE_PATH); try { goodFile.setReadable(false); - + try { new LogbackConfigLoader(GOOD_FILE_PATH); Assert.fail("Should throw an exception"); @@ -76,9 +75,9 @@ public void testLoadingConfig() throws Exception { goodFile.setExecutable(true); goodFile.setWritable(true); } - + addStep("success case", "Should not throw an exception"); - new LogbackConfigLoader(GOOD_FILE_PATH); + new LogbackConfigLoader(GOOD_FILE_PATH); } - + } diff --git a/bitrepository-core/src/test/java/org/bitrepository/protocol/utils/MessageDataTypeValidatorTest.java b/bitrepository-core/src/test/java/org/bitrepository/protocol/utils/MessageDataTypeValidatorTest.java index 9fb69880d..b17abd7ad 100644 --- a/bitrepository-core/src/test/java/org/bitrepository/protocol/utils/MessageDataTypeValidatorTest.java +++ b/bitrepository-core/src/test/java/org/bitrepository/protocol/utils/MessageDataTypeValidatorTest.java @@ -8,53 +8,53 @@ import org.testng.annotations.Test; public class MessageDataTypeValidatorTest { - - @Test(expectedExceptions = {IllegalArgumentException.class}) - public void validateChecksumSpecTest() { - ChecksumSpecTYPE noChecksumTypeSpec = new ChecksumSpecTYPE(); - MessageDataTypeValidator.validate(noChecksumTypeSpec, "noChecksumTypeSpec"); - } - - @Test(expectedExceptions = {IllegalArgumentException.class}) - public void validateChecksumDataForFileNoChecksumTest() { - ChecksumDataForFileTYPE noChecksumSpec = new ChecksumDataForFileTYPE(); - ChecksumSpecTYPE checksumTypeSpec = new ChecksumSpecTYPE(); - checksumTypeSpec.setChecksumType(ChecksumType.MD5); - noChecksumSpec.setChecksumSpec(checksumTypeSpec); - noChecksumSpec.setCalculationTimestamp(CalendarUtils.getNow()); - - MessageDataTypeValidator.validate(noChecksumSpec, "noChecksumSpec"); - } - - @Test(expectedExceptions = {IllegalArgumentException.class}) - public void validateChecksumDataForFileNoTimestampTest() { - ChecksumDataForFileTYPE noChecksumSpec = new ChecksumDataForFileTYPE(); - ChecksumSpecTYPE checksumTypeSpec = new ChecksumSpecTYPE(); - checksumTypeSpec.setChecksumType(ChecksumType.MD5); - noChecksumSpec.setChecksumSpec(checksumTypeSpec); - noChecksumSpec.setChecksumValue(Base16Utils.encodeBase16("abab")); - - MessageDataTypeValidator.validate(noChecksumSpec, "noChecksumSpec"); - } - - @Test(expectedExceptions = {IllegalArgumentException.class}) - public void validateChecksumDataForFileNoChecksumSpecTest() { - ChecksumDataForFileTYPE noChecksumSpec = new ChecksumDataForFileTYPE(); - noChecksumSpec.setChecksumValue(Base16Utils.encodeBase16("abab")); - noChecksumSpec.setCalculationTimestamp(CalendarUtils.getNow()); - - MessageDataTypeValidator.validate(noChecksumSpec, "noChecksumSpec"); - } - - //@Test(expectedExceptions = {IllegalArgumentException.class}) - public void validateChecksumDataForFileInvalidChecksumSpecTest() { - ChecksumDataForFileTYPE noChecksumSpec = new ChecksumDataForFileTYPE(); - ChecksumSpecTYPE checksumTypeSpec = new ChecksumSpecTYPE(); - noChecksumSpec.setChecksumSpec(checksumTypeSpec); - noChecksumSpec.setChecksumValue(Base16Utils.encodeBase16("abab")); - noChecksumSpec.setCalculationTimestamp(CalendarUtils.getNow()); - - MessageDataTypeValidator.validate(noChecksumSpec, "noChecksumSpec"); - } + + @Test(expectedExceptions = {IllegalArgumentException.class}) + public void validateChecksumSpecTest() { + ChecksumSpecTYPE noChecksumTypeSpec = new ChecksumSpecTYPE(); + MessageDataTypeValidator.validate(noChecksumTypeSpec, "noChecksumTypeSpec"); + } + + @Test(expectedExceptions = {IllegalArgumentException.class}) + public void validateChecksumDataForFileNoChecksumTest() { + ChecksumDataForFileTYPE noChecksumSpec = new ChecksumDataForFileTYPE(); + ChecksumSpecTYPE checksumTypeSpec = new ChecksumSpecTYPE(); + checksumTypeSpec.setChecksumType(ChecksumType.MD5); + noChecksumSpec.setChecksumSpec(checksumTypeSpec); + noChecksumSpec.setCalculationTimestamp(CalendarUtils.getNow()); + + MessageDataTypeValidator.validate(noChecksumSpec, "noChecksumSpec"); + } + + @Test(expectedExceptions = {IllegalArgumentException.class}) + public void validateChecksumDataForFileNoTimestampTest() { + ChecksumDataForFileTYPE noChecksumSpec = new ChecksumDataForFileTYPE(); + ChecksumSpecTYPE checksumTypeSpec = new ChecksumSpecTYPE(); + checksumTypeSpec.setChecksumType(ChecksumType.MD5); + noChecksumSpec.setChecksumSpec(checksumTypeSpec); + noChecksumSpec.setChecksumValue(Base16Utils.encodeBase16("abab")); + + MessageDataTypeValidator.validate(noChecksumSpec, "noChecksumSpec"); + } + + @Test(expectedExceptions = {IllegalArgumentException.class}) + public void validateChecksumDataForFileNoChecksumSpecTest() { + ChecksumDataForFileTYPE noChecksumSpec = new ChecksumDataForFileTYPE(); + noChecksumSpec.setChecksumValue(Base16Utils.encodeBase16("abab")); + noChecksumSpec.setCalculationTimestamp(CalendarUtils.getNow()); + + MessageDataTypeValidator.validate(noChecksumSpec, "noChecksumSpec"); + } + + //@Test(expectedExceptions = {IllegalArgumentException.class}) + public void validateChecksumDataForFileInvalidChecksumSpecTest() { + ChecksumDataForFileTYPE noChecksumSpec = new ChecksumDataForFileTYPE(); + ChecksumSpecTYPE checksumTypeSpec = new ChecksumSpecTYPE(); + noChecksumSpec.setChecksumSpec(checksumTypeSpec); + noChecksumSpec.setChecksumValue(Base16Utils.encodeBase16("abab")); + noChecksumSpec.setCalculationTimestamp(CalendarUtils.getNow()); + + MessageDataTypeValidator.validate(noChecksumSpec, "noChecksumSpec"); + } } diff --git a/bitrepository-core/src/test/java/org/bitrepository/protocol/utils/MessageUtilsTest.java b/bitrepository-core/src/test/java/org/bitrepository/protocol/utils/MessageUtilsTest.java index 4d6590410..368cbe233 100644 --- a/bitrepository-core/src/test/java/org/bitrepository/protocol/utils/MessageUtilsTest.java +++ b/bitrepository-core/src/test/java/org/bitrepository/protocol/utils/MessageUtilsTest.java @@ -5,16 +5,16 @@ * Copyright (C) 2010 - 2012 The State and University Library, The Royal Library and The State Archives, Denmark * %% * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 2.1 of the + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 2.1 of the * License, or (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public + * + * You should have received a copy of the GNU General Lesser Public * License along with this program. If not, see * . * #L% @@ -29,8 +29,8 @@ import org.testng.annotations.Test; public class MessageUtilsTest extends ExtendedTestCase { - @Test(groups = { "regressiontest" }) - public void testPositiveIdentification() throws Exception { + @Test(groups = {"regressiontest"}) + public void testPositiveIdentification() { addDescription("Tests isPositiveIdentifyResponse method in the message utility class."); MessageResponse response = new MessageResponse(); ResponseInfo ri = new ResponseInfo(); @@ -49,13 +49,13 @@ public void testPositiveIdentification() throws Exception { Assert.assertFalse(MessageUtils.isPositiveIdentifyResponse(response)); } - @Test(groups = { "regressiontest" }) - public void testIdentificationResponse() throws Exception { + @Test(groups = {"regressiontest"}) + public void testIdentificationResponse() { addDescription("Tests isIdentifyResponse method in the message utility class."); MessageResponse response = new MessageResponse(); ResponseInfo ri = new ResponseInfo(); response.setResponseInfo(ri); - + addStep("validate that it can see a identify response", "Should only return true for identify responses."); response.getResponseInfo().setResponseCode(ResponseCode.IDENTIFICATION_NEGATIVE); Assert.assertTrue(MessageUtils.isIdentifyResponse(response)); @@ -63,16 +63,16 @@ public void testIdentificationResponse() throws Exception { Assert.assertTrue(MessageUtils.isIdentifyResponse(response)); response.getResponseInfo().setResponseCode(ResponseCode.FAILURE); Assert.assertFalse(MessageUtils.isIdentifyResponse(response)); - + } - - @Test(groups = { "regressiontest" }) - public void testProgressResponse() throws Exception { + + @Test(groups = {"regressiontest"}) + public void testProgressResponse() { addDescription("Tests isPositiveProgressResponse method in the message utility class."); MessageResponse response = new MessageResponse(); ResponseInfo ri = new ResponseInfo(); response.setResponseInfo(ri); - + addStep("validate progress response", "Should only return true for 'operation_progress', " + "'operation_accepted_progress' and 'identification_positive'."); response.getResponseInfo().setResponseCode(ResponseCode.IDENTIFICATION_NEGATIVE); @@ -88,5 +88,5 @@ public void testProgressResponse() throws Exception { response.getResponseInfo().setResponseCode(ResponseCode.FAILURE); Assert.assertFalse(MessageUtils.isIdentifyResponse(response)); } - + } diff --git a/bitrepository-core/src/test/java/org/bitrepository/settings/SettingsProviderTest.java b/bitrepository-core/src/test/java/org/bitrepository/settings/SettingsProviderTest.java index ae9b5dfde..f202465da 100644 --- a/bitrepository-core/src/test/java/org/bitrepository/settings/SettingsProviderTest.java +++ b/bitrepository-core/src/test/java/org/bitrepository/settings/SettingsProviderTest.java @@ -5,16 +5,16 @@ * Copyright (C) 2010 - 2012 The State and University Library, The Royal Library and The State Archives, Denmark * %% * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 2.1 of the + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 2.1 of the * License, or (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public + * + * You should have received a copy of the GNU General Lesser Public * License along with this program. If not, see * . * #L% diff --git a/bitrepository-core/src/test/resources/settings/xml/bitrepository-devel/ReferenceSettings.xml b/bitrepository-core/src/test/resources/settings/xml/bitrepository-devel/ReferenceSettings.xml index ecd78635a..c184487b5 100644 --- a/bitrepository-core/src/test/resources/settings/xml/bitrepository-devel/ReferenceSettings.xml +++ b/bitrepository-core/src/test/resources/settings/xml/bitrepository-devel/ReferenceSettings.xml @@ -97,6 +97,7 @@ jdbc:derby:target/test/integrityservice/auditcontributerdb target/ + monitoring-service diff --git a/bitrepository-integration/src/main/resources/quickstart/conf/integrityservice/ReferenceSettings.xml b/bitrepository-integration/src/main/resources/quickstart/conf/integrityservice/ReferenceSettings.xml index fb37bbb30..5394c862b 100644 --- a/bitrepository-integration/src/main/resources/quickstart/conf/integrityservice/ReferenceSettings.xml +++ b/bitrepository-integration/src/main/resources/quickstart/conf/integrityservice/ReferenceSettings.xml @@ -25,7 +25,6 @@ org.bitrepository.protocol.messagebus.destination.DefaultReceiverDestinationIDFactory - 1000 3600000 @@ -95,5 +94,22 @@ jdbc:derby:conf/integrityservice/auditcontributerdb conf/integrityservice/reportdir + + + checksum-pillar + test1@kb.dk + CHECKSUM + + + file1-pillar + test2@kb.dk + FILE + + + file2-pillar + test3@kb.dk + FILE + + diff --git a/bitrepository-integrity-service/src/main/java/org/bitrepository/integrityservice/cache/PillarCollectionStat.java b/bitrepository-integrity-service/src/main/java/org/bitrepository/integrityservice/cache/PillarCollectionStat.java index b1b921c62..fac5d8714 100644 --- a/bitrepository-integrity-service/src/main/java/org/bitrepository/integrityservice/cache/PillarCollectionStat.java +++ b/bitrepository-integrity-service/src/main/java/org/bitrepository/integrityservice/cache/PillarCollectionStat.java @@ -29,6 +29,8 @@ public class PillarCollectionStat { private final String pillarID; private final String collectionID; + private final String pillarHostname; + private final String pillarType; private Long fileCount = 0L; private Long dataSize = 0L; private Long missingFiles = 0L; @@ -38,15 +40,20 @@ public class PillarCollectionStat { private Date statsTime; private Date updateTime; - public PillarCollectionStat(String pillarID, String collectionID) { + public PillarCollectionStat(String pillarID, String collectionID, String pillarHostname, String pillarType) { this.pillarID = pillarID; this.collectionID = collectionID; + this.pillarHostname = pillarHostname; + this.pillarType = pillarType; } - public PillarCollectionStat(String pillarID, String collectionID, Long fileCount, Long dataSize, Long missingFiles, - Long checksumErrors, Long missingChecksums, Long obsoleteChecksum, Date statsTime, Date updateTime) { + public PillarCollectionStat(String pillarID, String collectionID, String pillarHostname, String pillarType, Long fileCount, + Long dataSize, Long missingFiles, Long checksumErrors, Long missingChecksums, Long obsoleteChecksum, + Date statsTime, Date updateTime) { this.pillarID = pillarID; this.collectionID = collectionID; + this.pillarHostname = pillarHostname; + this.pillarType = pillarType; this.fileCount = fileCount; this.dataSize = dataSize; this.missingFiles = missingFiles; @@ -65,6 +72,14 @@ public String getCollectionID() { return collectionID; } + public String getPillarHostname() { + return pillarHostname; + } + + public String getPillarType() { + return pillarType; + } + public Long getFileCount() { return fileCount; } diff --git a/bitrepository-integrity-service/src/main/java/org/bitrepository/integrityservice/cache/database/IntegrityDAO.java b/bitrepository-integrity-service/src/main/java/org/bitrepository/integrityservice/cache/database/IntegrityDAO.java index 1456c323c..51e9d96b5 100644 --- a/bitrepository-integrity-service/src/main/java/org/bitrepository/integrityservice/cache/database/IntegrityDAO.java +++ b/bitrepository-integrity-service/src/main/java/org/bitrepository/integrityservice/cache/database/IntegrityDAO.java @@ -25,6 +25,7 @@ import org.bitrepository.bitrepositoryelements.FileIDsData; import org.bitrepository.common.ArgumentValidator; import org.bitrepository.common.utils.CalendarUtils; +import org.bitrepository.common.utils.SettingsUtils; import org.bitrepository.integrityservice.cache.CollectionStat; import org.bitrepository.integrityservice.cache.FileInfo; import org.bitrepository.integrityservice.cache.PillarCollectionMetric; @@ -45,6 +46,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Optional; /** @@ -345,11 +347,7 @@ public IntegrityIssueIterator getAllFileIDsOnPillar(String collectionID, String ArgumentValidator.checkNotNullOrEmpty(collectionID, "String collectionID"); ArgumentValidator.checkNotNullOrEmpty(pillarID, "String pillarID"); long first; - if (firstIndex == null) { - first = 0; - } else { - first = firstIndex; - } + first = Objects.requireNonNullElse(firstIndex, 0L); String getAllFileIDsSql = getAllFileIDsSql(); return makeIntegrityIssueIterator(getAllFileIDsSql, collectionID, pillarID, first, maxResults); } @@ -448,11 +446,11 @@ public Map getPillarCollectionMetrics(String col PreparedStatement ps = DatabaseUtils.createPreparedStatement(conn, selectSql, collectionID); ResultSet dbResult = ps.executeQuery()) { while (dbResult.next()) { - String pillarid = dbResult.getString("pillarID"); + String pillarID = dbResult.getString("pillarID"); Long fileCount = dbResult.getLong("filecount"); Long fileSize = dbResult.getLong("sizesum"); PillarCollectionMetric metric = new PillarCollectionMetric(fileSize, fileCount); - metrics.put(pillarid, metric); + metrics.put(pillarID, metric); } } catch (SQLException e) { throw new IllegalStateException( @@ -519,10 +517,11 @@ public List getLatestPillarStats(String collectionID) { Long obsoleteChecksums = dbResult.getLong("obsolete_checksums_count"); Date statsTime = null; Date updateTime = null; - - - PillarCollectionStat p = new PillarCollectionStat(pillarID, collectionID, fileCount, dataSize, missingFiles, - checksumErrors, missingChecksums, obsoleteChecksums, statsTime, updateTime); + String pillarHostname = Objects.requireNonNullElse(SettingsUtils.getHostname(pillarID), "N/A"); + String pillarType = (SettingsUtils.getPillarType(pillarID) != null) ? + Objects.requireNonNull(SettingsUtils.getPillarType(pillarID)).value() : "Unknown"; + PillarCollectionStat p = new PillarCollectionStat(pillarID, collectionID, pillarHostname, pillarType, fileCount, + dataSize, missingFiles, checksumErrors, missingChecksums, obsoleteChecksums, statsTime, updateTime); stats.add(p); } } diff --git a/bitrepository-integrity-service/src/main/java/org/bitrepository/integrityservice/statistics/StatisticsCollector.java b/bitrepository-integrity-service/src/main/java/org/bitrepository/integrityservice/statistics/StatisticsCollector.java index 49b3dfa61..9d674eebb 100644 --- a/bitrepository-integrity-service/src/main/java/org/bitrepository/integrityservice/statistics/StatisticsCollector.java +++ b/bitrepository-integrity-service/src/main/java/org/bitrepository/integrityservice/statistics/StatisticsCollector.java @@ -24,13 +24,14 @@ import org.bitrepository.common.utils.SettingsUtils; import org.bitrepository.integrityservice.cache.CollectionStat; import org.bitrepository.integrityservice.cache.PillarCollectionStat; +import org.bitrepository.settings.referencesettings.PillarType; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; public class StatisticsCollector { - final CollectionStat collectionStat; final Map pillarCollectionStats; @@ -39,7 +40,10 @@ public StatisticsCollector(String collectionID) { pillarCollectionStats = new HashMap<>(); List pillars = SettingsUtils.getPillarIDsForCollection(collectionID); for (String pillar : pillars) { - PillarCollectionStat ps = new PillarCollectionStat(pillar, collectionID); + String pillarHostname = Objects.requireNonNullElse(SettingsUtils.getHostname(pillar), "N/A"); + PillarType pillarTypeObject = SettingsUtils.getPillarType(pillar); + String pillarType = (pillarTypeObject != null) ? pillarTypeObject.value() : "Unknown"; + PillarCollectionStat ps = new PillarCollectionStat(pillar, collectionID, pillarHostname, pillarType); pillarCollectionStats.put(pillar, ps); } } diff --git a/bitrepository-integrity-service/src/main/java/org/bitrepository/integrityservice/web/RestIntegrityService.java b/bitrepository-integrity-service/src/main/java/org/bitrepository/integrityservice/web/RestIntegrityService.java index 895507a1a..1b1e32293 100644 --- a/bitrepository-integrity-service/src/main/java/org/bitrepository/integrityservice/web/RestIntegrityService.java +++ b/bitrepository-integrity-service/src/main/java/org/bitrepository/integrityservice/web/RestIntegrityService.java @@ -41,6 +41,7 @@ import org.bitrepository.service.workflow.Workflow; import org.bitrepository.service.workflow.WorkflowManager; import org.bitrepository.service.workflow.WorkflowStatistic; +import org.bitrepository.settings.referencesettings.PillarType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -66,6 +67,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; @Path("/IntegrityService") public class RestIntegrityService { @@ -244,15 +246,18 @@ public String getIntegrityStatus( } for (String pillar : pillars) { if (!stats.containsKey(pillar)) { - PillarCollectionStat emptyStat = new PillarCollectionStat(pillar, collectionID, 0L, 0L, 0L, 0L, 0L, 0L, new Date(0), - new Date(0)); - ; + String pillarHostname = Objects.requireNonNullElse(SettingsUtils.getHostname(pillar), "N/A"); + PillarType pillarTypeObject = SettingsUtils.getPillarType(pillar); + String pillarType = pillarTypeObject != null ? pillarTypeObject.value() : null; + PillarCollectionStat emptyStat = new PillarCollectionStat(pillar, collectionID, pillarHostname, pillarType, + 0L, 0L, 0L, 0L, 0L, 0L, new Date(0), new Date(0)); stats.put(pillar, emptyStat); } } jg.writeStartArray(); for (PillarCollectionStat stat : stats.values()) { writeIntegrityStatusObject(stat, jg); + log.debug("IntegrityStatus: Wrote hostname: " + stat.getPillarHostname() + " to pillar" + stat.getPillarID()); } jg.writeEndArray(); jg.flush(); @@ -410,6 +415,8 @@ private StreamingOutput streamPartFromLatestReport(ReportPart part, String colle private void writeIntegrityStatusObject(PillarCollectionStat stat, JsonGenerator jg) throws IOException { jg.writeStartObject(); jg.writeObjectField("pillarID", stat.getPillarID()); + jg.writeObjectField("pillarHostname", stat.getPillarHostname()); + jg.writeObjectField("pillarType", stat.getPillarType()); jg.writeObjectField("totalFileCount", stat.getFileCount()); jg.writeObjectField("missingFilesCount", stat.getMissingFiles()); jg.writeObjectField("checksumErrorCount", stat.getChecksumErrors()); diff --git a/bitrepository-integrity-service/src/test/java/org/bitrepository/integrityservice/integrationtest/MissingChecksumTests.java b/bitrepository-integrity-service/src/test/java/org/bitrepository/integrityservice/integrationtest/MissingChecksumTests.java index 5e6697d05..c4cd51421 100644 --- a/bitrepository-integrity-service/src/test/java/org/bitrepository/integrityservice/integrationtest/MissingChecksumTests.java +++ b/bitrepository-integrity-service/src/test/java/org/bitrepository/integrityservice/integrationtest/MissingChecksumTests.java @@ -54,8 +54,6 @@ import org.bitrepository.service.database.DerbyDatabaseDestroyer; import org.bitrepository.service.exception.WorkflowAbortedException; import org.jaccept.structure.ExtendedTestCase; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; @@ -77,17 +75,13 @@ import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertTrue; -@SuppressWarnings("rawtypes") public class MissingChecksumTests extends ExtendedTestCase { - private static final String PILLAR_1 = "pillar1"; private static final String PILLAR_2 = "pillar2"; private static final String DEFAULT_CHECKSUM = "0123456789"; private static final String TEST_FILE_1 = "test-file-1"; - private static final String TEST_FILE_2 = "test-file-2"; private String TEST_COLLECTION; protected Settings settings; @@ -134,17 +128,13 @@ public void testMissingChecksumAndStep() throws Exception { populateDatabase(model, TEST_FILE_1); addStep("Run missing checksum step.", "The file should be marked as missing at all pillars."); - doAnswer(new Answer() { - public String answer(InvocationOnMock invocation) { - return TEST_COLLECTION; - } - }).when(reporter).getCollectionID(); + doAnswer(invocation -> TEST_COLLECTION).when(reporter).getCollectionID(); StatisticsCollector cs = new StatisticsCollector(TEST_COLLECTION); HandleMissingChecksumsStep missingChecksumStep = new HandleMissingChecksumsStep(model, reporter, cs, new Date(0)); missingChecksumStep.performStep(); for(String pillar : SettingsUtils.getPillarIDsForCollection(TEST_COLLECTION)) { - assertTrue(cs.getPillarCollectionStat(pillar).getMissingChecksums() == 1); + assertEquals((long) cs.getPillarCollectionStat(pillar).getMissingChecksums(), 1); } } @@ -157,15 +147,13 @@ public void testMissingChecksumForFirstGetChecksums() throws WorkflowAbortedExce addStep("Add checksum results for only one pillar.", ""); final ResultingChecksums resultingChecksums = createResultingChecksums(DEFAULT_CHECKSUM, TEST_FILE_1); - doAnswer(new Answer() { - public Void answer(InvocationOnMock invocation) { - EventHandler eventHandler = (EventHandler) invocation.getArguments()[6]; - eventHandler.handleEvent(new IdentificationCompleteEvent(TEST_COLLECTION, Arrays.asList(PILLAR_1, PILLAR_2))); - eventHandler.handleEvent(new ChecksumsCompletePillarEvent(PILLAR_1, TEST_COLLECTION, - resultingChecksums, createChecksumSpecTYPE(), false)); - eventHandler.handleEvent(new CompleteEvent(TEST_COLLECTION, null)); - return null; - } + doAnswer(invocation -> { + EventHandler eventHandler = (EventHandler) invocation.getArguments()[6]; + eventHandler.handleEvent(new IdentificationCompleteEvent(TEST_COLLECTION, Arrays.asList(PILLAR_1, PILLAR_2))); + eventHandler.handleEvent(new ChecksumsCompletePillarEvent(PILLAR_1, TEST_COLLECTION, + resultingChecksums, createChecksumSpecTYPE(), false)); + eventHandler.handleEvent(new CompleteEvent(TEST_COLLECTION, null)); + return null; }).when(collector).getChecksums( eq(TEST_COLLECTION), any(), any(ChecksumSpecTYPE.class), any(), anyString(), any(ContributorQuery[].class), any(EventHandler.class)); @@ -205,17 +193,15 @@ public void testMissingChecksumDuringSecondIngest() throws WorkflowAbortedExcept addStep("Add checksum results for both pillar.", ""); final ResultingChecksums resultingChecksums = createResultingChecksums(DEFAULT_CHECKSUM, TEST_FILE_1); - doAnswer(new Answer() { - public Void answer(InvocationOnMock invocation) { - EventHandler eventHandler = (EventHandler) invocation.getArguments()[6]; - eventHandler.handleEvent(new IdentificationCompleteEvent(TEST_COLLECTION, Arrays.asList(PILLAR_1, PILLAR_2))); - eventHandler.handleEvent(new ChecksumsCompletePillarEvent(PILLAR_1, TEST_COLLECTION, - resultingChecksums, createChecksumSpecTYPE(), false)); - eventHandler.handleEvent(new ChecksumsCompletePillarEvent(PILLAR_2, TEST_COLLECTION, - resultingChecksums, createChecksumSpecTYPE(), false)); - eventHandler.handleEvent(new CompleteEvent(TEST_COLLECTION, null)); - return null; - } + doAnswer(invocation -> { + EventHandler eventHandler = (EventHandler) invocation.getArguments()[6]; + eventHandler.handleEvent(new IdentificationCompleteEvent(TEST_COLLECTION, Arrays.asList(PILLAR_1, PILLAR_2))); + eventHandler.handleEvent(new ChecksumsCompletePillarEvent(PILLAR_1, TEST_COLLECTION, + resultingChecksums, createChecksumSpecTYPE(), false)); + eventHandler.handleEvent(new ChecksumsCompletePillarEvent(PILLAR_2, TEST_COLLECTION, + resultingChecksums, createChecksumSpecTYPE(), false)); + eventHandler.handleEvent(new CompleteEvent(TEST_COLLECTION, null)); + return null; }).when(collector).getChecksums( eq(TEST_COLLECTION), any(), any(ChecksumSpecTYPE.class), any(), anyString(), any(ContributorQuery[].class), any(EventHandler.class)); @@ -242,15 +228,13 @@ public Void answer(InvocationOnMock invocation) { } addStep("Add checksum results for only the second pillar.", ""); - doAnswer(new Answer() { - public Void answer(InvocationOnMock invocation) { - EventHandler eventHandler = (EventHandler) invocation.getArguments()[6]; - eventHandler.handleEvent(new IdentificationCompleteEvent(TEST_COLLECTION, Arrays.asList(PILLAR_1, PILLAR_2))); - eventHandler.handleEvent(new ChecksumsCompletePillarEvent(PILLAR_2, TEST_COLLECTION, - resultingChecksums, createChecksumSpecTYPE(), false)); - eventHandler.handleEvent(new CompleteEvent(TEST_COLLECTION, null)); - return null; - } + doAnswer(invocation -> { + EventHandler eventHandler = (EventHandler) invocation.getArguments()[6]; + eventHandler.handleEvent(new IdentificationCompleteEvent(TEST_COLLECTION, Arrays.asList(PILLAR_1, PILLAR_2))); + eventHandler.handleEvent(new ChecksumsCompletePillarEvent(PILLAR_2, TEST_COLLECTION, + resultingChecksums, createChecksumSpecTYPE(), false)); + eventHandler.handleEvent(new CompleteEvent(TEST_COLLECTION, null)); + return null; }).when(collector).getChecksums( eq(TEST_COLLECTION), any(), any(ChecksumSpecTYPE.class), any(), anyString(), any(ContributorQuery[].class), any(EventHandler.class)); @@ -294,15 +278,15 @@ protected void populateDatabase(IntegrityModel model, String ... files) { model.addFileIDs(data, PILLAR_2, collectionID); } - private ResultingChecksums createResultingChecksums(String checksum, String ... fileids) { + private ResultingChecksums createResultingChecksums(String checksum, String ... fileIDs) { ResultingChecksums res = new ResultingChecksums(); - res.getChecksumDataItems().addAll(createChecksumData(checksum, fileids)); + res.getChecksumDataItems().addAll(createChecksumData(checksum, fileIDs)); return res; } - private List createChecksumData(String checksum, String ... fileids) { + private List createChecksumData(String checksum, String ... fileIDs) { List res = new ArrayList<>(); - for(String fileID : fileids) { + for(String fileID : fileIDs) { ChecksumDataForChecksumSpecTYPE csData = new ChecksumDataForChecksumSpecTYPE(); csData.setCalculationTimestamp(CalendarUtils.getNow()); csData.setChecksumValue(Base16Utils.encodeBase16(checksum)); diff --git a/bitrepository-reference-settings/src/main/resources/xsd/ReferenceSettings.xsd b/bitrepository-reference-settings/src/main/resources/xsd/ReferenceSettings.xsd index 6aab9a6db..f6f2f2ead 100644 --- a/bitrepository-reference-settings/src/main/resources/xsd/ReferenceSettings.xsd +++ b/bitrepository-reference-settings/src/main/resources/xsd/ReferenceSettings.xsd @@ -21,7 +21,7 @@ #L% --> @@ -33,21 +33,21 @@ - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + @@ -59,7 +59,7 @@ General settings for a client - + @@ -72,7 +72,7 @@ - Defines the the thread pools to handle message. + Defines the thread pools to handle message. A received message is check against the different message thread pools in the following order: 1. Is there defined pools specific to collection for the received message. @@ -83,7 +83,7 @@ - + @@ -92,16 +92,18 @@ Defines a thread pool to handle a set of message types. The pool can be restricted to only - be used on specific collections, message categories and specific messages + be used on specific collections, message categories and specific messages + - + The number of threads to use for processing. If undefined the pool thread limit is - unbound, but threads will be reused. + unbound, but threads will be reused. + - + The collections filter to use the pool for. If undefined all collections are process via this pool. @@ -116,7 +118,7 @@ - + The specific messages to use the pool for. Overrides the MessageCategory setting. @@ -192,7 +194,7 @@ Class for overriding the standard generated ComponentID - + @@ -200,7 +202,7 @@ The maximum number of results that the client wants per page. If MaxPageSize is not set, the clients default to 10000 results per page. This setting is not automatically used when using the reference client libraries. The user - of the client is however free to utilize the setting. + of the client is however free to utilize the setting. The setting applies to: Integrity service, Audittrail service and reference commandline client @@ -310,11 +312,11 @@ - Whether or not verify the consistency between files and the data in the checksum database, when handling + Whether or not verify the consistency between files and the data in the checksum database, when handling messages for operations involving the given files. - The consistency is already being checked by the scheduled job, so it setting is only for further + The consistency is already being checked by the scheduled job, so it setting is only for further verifying the consistency when handling messages. - Default value is false. + Default value is false. This setting is only releavent for the FilePillar (since ChecksumPillars do not have the actual files). @@ -323,6 +325,27 @@ + + + + The details of all the pillars used by integrity service to map pillar IDs to hostnames and pillar types. + + + + + + + + + + + + + + + + + @@ -390,7 +413,7 @@ - Decides which type of pillar it is. + Decides which type of pillar it is. @@ -405,9 +428,9 @@ - The FilePillar is a full ReferencePillar, both containing the checksum database and archives with + The FilePillar is a full ReferencePillar, both containing the checksum database and archives with the actual files. - The directories defined in settings will automatically be instantiated (if possible). + The directories defined in settings will automatically be instantiated (if possible). @@ -548,10 +571,10 @@ - - - - + + + + @@ -563,7 +586,7 @@ - + @@ -590,7 +613,7 @@ - + @@ -611,7 +634,7 @@ - + @@ -694,7 +717,7 @@ - + @@ -714,9 +737,9 @@ - Control how the integrity service should behave in case of a contributor failing to deliver + Control how the integrity service should behave in case of a contributor failing to deliver FileIDs or Checksum data. - If set to true, stop the integrity check and send an alarm, if false, continue the check on + If set to true, stop the integrity check and send an alarm, if false, continue the check on a incomplete set of data, and alert by sending an alarm. The service defaults to true. @@ -759,7 +782,7 @@ - + The directory where to write and store integrity reports. Directory must exists, and be read- and writable @@ -767,6 +790,13 @@ + + + + The Integrity details of all the pillars. This includes the Pillar hostnames and Pillar types. + + + @@ -776,7 +806,7 @@ List of Collections present in the repository. - + @@ -785,10 +815,11 @@ Defines a specific workflow. - + The class for the workflow to run. - The class most implement the workflow interface + The class most implement the workflow interface + @@ -807,7 +838,7 @@ List of Collections present in the repository. - + @@ -821,7 +852,7 @@ - The time in milliseconds between the workflow is run. + The time in milliseconds between the workflow is run. If it is a non-positive number, then the workflow will not be scheduled, but it will be possible to run manually from the UI. Also workflows with a positive interval will be run at start-up, whereas workflows with a non-positive interval will not. @@ -845,7 +876,7 @@ Defines a set of collections. - + @@ -888,7 +919,7 @@ The max checksum age for the pillar. - If set to 0, then the pillar is ignored for obsolete checksums check, e.g. for ChecksumPillars + If set to 0, then the pillar is ignored for obsolete checksums check, e.g. for ChecksumPillars who cannot recalculate the checksum. @@ -1003,7 +1034,7 @@ - + The port used by the protocol. diff --git a/bitrepository-webclient/src/main/java/org/bitrepository/BasicClient.java b/bitrepository-webclient/src/main/java/org/bitrepository/BasicClient.java index 673511aef..40d43e922 100644 --- a/bitrepository-webclient/src/main/java/org/bitrepository/BasicClient.java +++ b/bitrepository-webclient/src/main/java/org/bitrepository/BasicClient.java @@ -5,16 +5,16 @@ * Copyright (C) 2010 - 2012 The State and University Library, The Royal Library and The State Archives, Denmark * %% * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 2.1 of the + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 2.1 of the * License, or (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public + * + * You should have received a copy of the GNU General Lesser Public * License along with this program. If not, see * . * #L% @@ -31,46 +31,49 @@ import java.util.List; public class BasicClient { - private Settings settings; + private final Settings settings; private final Logger log = LoggerFactory.getLogger(getClass()); + public BasicClient(Settings settings) { log.debug("---- Basic client instantiating ----"); this.settings = settings; log.debug("---- Basic client instantiated ----"); - } - + public List getCollectionIDs() { + log.trace("Fetching collectionIDs"); List collections = new ArrayList<>(); - for(Collection collection : settings.getRepositorySettings().getCollections().getCollection()) { + for (Collection collection : settings.getRepositorySettings().getCollections().getCollection()) { collections.add(collection.getID()); } return collections; } public void shutdown() { - // currently nothing to do here + // Currently, there's nothing to do here } public String getSettingsSummary() { + log.trace("Fetching Settings Summary"); StringBuilder sb = new StringBuilder(); RepositorySettings repositorySettings = settings.getRepositorySettings(); sb.append("Collections:
"); - for (Collection collection: settings.getCollections()) { - sb.append("ID:" + collection.getID()); + for (Collection collection : settings.getCollections()) { + sb.append("ID:").append(collection.getID()); sb.append("Pillars:"); - for (String pillarID: collection.getPillarIDs().getPillarID()) { - sb.append("  " + pillarID); + for (String pillarID : collection.getPillarIDs().getPillarID()) { + sb.append("  ").append(pillarID); } } sb.append("
"); sb.append("
"); - sb.append("Messagebus URL:
    "); - sb.append(repositorySettings.getProtocolSettings().getMessageBusConfiguration().getURL() + "
"); + sb.append("Messagebus URL:
    "); + sb.append(repositorySettings.getProtocolSettings().getMessageBusConfiguration().getURL()).append("
"); return sb.toString(); } - + public Settings getSettings() { + log.trace("Fetching Settings"); return settings; } } diff --git a/bitrepository-webclient/src/main/webapp/integrity-service.html b/bitrepository-webclient/src/main/webapp/integrity-service.html index 3d20476af..6d97edd61 100644 --- a/bitrepository-webclient/src/main/webapp/integrity-service.html +++ b/bitrepository-webclient/src/main/webapp/integrity-service.html @@ -20,342 +20,364 @@ #L% --> - - - Bitrepository integrity service - - - - -

-
-
-
-
-

Integrity service

- + + + Bitrepository integrity service + + + + + +
+
+
+
+

Integrity service

+ Integrity information - + Change collection: - -
-
-
- Workflows status - - - - - - - - - - - -
Workflow nameNext runLast runIntervalCurrent state
-
-
-
- - -
-
-
-
- + +
+
+
+ Workflows status + + + + + + + + + + + +
Workflow nameNext runLast runIntervalCurrent state
+
+
+
+ + +
+
+
+
+ Integrity status - - - - - - - - - - - - - -
Pillar IDTotal number of filesNumber of missing filesNumber of missing checksumsNumber of obsolete checksumsNumber of inconsistent checksums
-
+ + + + + + + + + + + + + + + +
Pillar IDPillar HostnamePillar TypeTotal number of filesNumber of missing filesNumber of missing checksumsNumber of obsolete checksumsNumber of inconsistent checksums
+
- + - - - - - - - + + + + + + + - - - + + + \ No newline at end of file