http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/2564ac0a/extras/shell/src/test/java/org/apache/rya/shell/RyaAdminCommandsTest.java ---------------------------------------------------------------------- diff --git a/extras/shell/src/test/java/org/apache/rya/shell/RyaAdminCommandsTest.java b/extras/shell/src/test/java/org/apache/rya/shell/RyaAdminCommandsTest.java new file mode 100644 index 0000000..e3e8d98 --- /dev/null +++ b/extras/shell/src/test/java/org/apache/rya/shell/RyaAdminCommandsTest.java @@ -0,0 +1,474 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.rya.shell; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.io.IOException; +import java.util.Date; +import java.util.List; +import java.util.TimeZone; + +import org.apache.rya.api.client.AddUser; +import org.apache.rya.api.client.CreatePCJ; +import org.apache.rya.api.client.DeletePCJ; +import org.apache.rya.api.client.GetInstanceDetails; +import org.apache.rya.api.client.Install; +import org.apache.rya.api.client.Install.DuplicateInstanceNameException; +import org.apache.rya.api.client.Install.InstallConfiguration; +import org.apache.rya.api.client.InstanceDoesNotExistException; +import org.apache.rya.api.client.ListInstances; +import org.apache.rya.api.client.RemoveUser; +import org.apache.rya.api.client.RyaClient; +import org.apache.rya.api.client.RyaClientException; +import org.apache.rya.api.client.Uninstall; +import org.apache.rya.api.client.accumulo.AccumuloConnectionDetails; +import org.apache.rya.api.instance.RyaDetails; +import org.apache.rya.api.instance.RyaDetails.EntityCentricIndexDetails; +import org.apache.rya.api.instance.RyaDetails.FreeTextIndexDetails; +import org.apache.rya.api.instance.RyaDetails.JoinSelectivityDetails; +import org.apache.rya.api.instance.RyaDetails.PCJIndexDetails; +import org.apache.rya.api.instance.RyaDetails.PCJIndexDetails.FluoDetails; +import org.apache.rya.api.instance.RyaDetails.PCJIndexDetails.PCJDetails; +import org.apache.rya.api.instance.RyaDetails.PCJIndexDetails.PCJDetails.PCJUpdateStrategy; +import org.apache.rya.api.instance.RyaDetails.ProspectorDetails; +import org.apache.rya.api.instance.RyaDetails.TemporalIndexDetails; +import org.apache.rya.shell.util.InstallPrompt; +import org.apache.rya.shell.util.SparqlPrompt; +import org.apache.rya.shell.util.UninstallPrompt; +import org.junit.Test; + +import com.google.common.base.Optional; +import com.google.common.collect.Lists; + +/** + * Unit tests the methods of {@link RyaAdminCommands}. + */ +public class RyaAdminCommandsTest { + + @Test + public void createPCJ() throws InstanceDoesNotExistException, RyaClientException, IOException { + // Mock the object that performs the create operation. + final String instanceName = "unitTest"; + final String sparql = "SELECT * WHERE { ?person <http://isA> ?noun }"; + final String pcjId = "123412342"; + final CreatePCJ mockCreatePCJ = mock(CreatePCJ.class); + when(mockCreatePCJ.createPCJ( eq(instanceName), eq(sparql) ) ).thenReturn( pcjId ); + + final RyaClient mockCommands = mock(RyaClient.class); + when(mockCommands.getCreatePCJ()).thenReturn( mockCreatePCJ ); + + final SharedShellState state = new SharedShellState(); + state.connectedToAccumulo(mock(AccumuloConnectionDetails.class), mockCommands); + state.connectedToInstance(instanceName); + + final SparqlPrompt mockSparqlPrompt = mock(SparqlPrompt.class); + when(mockSparqlPrompt.getSparql()).thenReturn(Optional.of(sparql)); + + // Execute the command. + final RyaAdminCommands commands = new RyaAdminCommands(state, mock(InstallPrompt.class), mockSparqlPrompt, mock(UninstallPrompt.class)); + final String message = commands.createPcj(); + + // Verify the values that were provided to the command were passed through to CreatePCJ. + verify(mockCreatePCJ).createPCJ(eq(instanceName), eq(sparql)); + + // Verify a message is returned that explains what was created. + final String expected = "The PCJ has been created. Its ID is '123412342'."; + assertEquals(expected, message); + } + + @Test + public void createPCJ_cancelledPrompt() throws InstanceDoesNotExistException, RyaClientException, IOException { + // Mock the object that performs the create operation. + final String instanceName = "unitTest"; + + final RyaClient mockCommands = mock(RyaClient.class); + + final SharedShellState state = new SharedShellState(); + state.connectedToAccumulo(mock(AccumuloConnectionDetails.class), mockCommands); + state.connectedToInstance(instanceName); + + final SparqlPrompt mockSparqlPrompt = mock(SparqlPrompt.class); + when(mockSparqlPrompt.getSparql()).thenReturn(Optional.absent()); + + // Execute the command. + final RyaAdminCommands commands = new RyaAdminCommands(state, mock(InstallPrompt.class), mockSparqlPrompt, mock(UninstallPrompt.class)); + final String message = commands.createPcj(); + + // Verify a message is returned that explains what was created. + final String expected = ""; + assertEquals(expected, message); + } + + @Test + public void deletePCJ() throws InstanceDoesNotExistException, RyaClientException { + // Mock the object that performs the delete operation. + final DeletePCJ mockDeletePCJ = mock(DeletePCJ.class); + + final RyaClient mockCommands = mock(RyaClient.class); + when(mockCommands.getDeletePCJ()).thenReturn( mockDeletePCJ ); + + final SharedShellState state = new SharedShellState(); + state.connectedToAccumulo(mock(AccumuloConnectionDetails.class), mockCommands); + final String instanceName = "unitTests"; + state.connectedToInstance(instanceName); + + // Execute the command. + final String pcjId = "123412342"; + + final RyaAdminCommands commands = new RyaAdminCommands(state, mock(InstallPrompt.class), mock(SparqlPrompt.class), mock(UninstallPrompt.class)); + final String message = commands.deletePcj(pcjId); + + // Verify the values that were provided to the command were passed through to the DeletePCJ. + verify(mockDeletePCJ).deletePCJ(eq(instanceName), eq(pcjId)); + + // Verify a message is returned that explains what was deleted. + final String expected = "The PCJ has been deleted."; + assertEquals(expected, message); + } + + @Test + public void getInstanceDetails() throws InstanceDoesNotExistException, RyaClientException { + // This test is failed if the default timezone was not EST, so now it's fixed at EST. + // If you get assert mismatch of EST!=EDT, try the deprecated getTimeZone("EST") instead. + TimeZone.setDefault(TimeZone.getTimeZone("America/New_York")); + // Mock the object that performs the get operation. + final GetInstanceDetails mockGetInstanceDetails = mock(GetInstanceDetails.class); + final String instanceName = "test_instance"; + final RyaDetails details = RyaDetails.builder().setRyaInstanceName(instanceName) + .setRyaVersion("1.2.3.4") + .addUser("alice") + .addUser("bob") + .addUser("charlie") + .setEntityCentricIndexDetails( new EntityCentricIndexDetails(true) ) + //RYA-215.setGeoIndexDetails( new GeoIndexDetails(true) ) + .setTemporalIndexDetails( new TemporalIndexDetails(true) ) + .setFreeTextDetails( new FreeTextIndexDetails(true) ) + .setPCJIndexDetails( + PCJIndexDetails.builder() + .setEnabled(true) + .setFluoDetails( new FluoDetails("test_instance_rya_pcj_updater") ) + .addPCJDetails( + PCJDetails.builder() + .setId("pcj 1") + .setUpdateStrategy(PCJUpdateStrategy.BATCH) + .setLastUpdateTime( new Date(1252521351L) )) + .addPCJDetails( + PCJDetails.builder() + .setId("pcj 2") + .setUpdateStrategy(PCJUpdateStrategy.INCREMENTAL))) + .setProspectorDetails( new ProspectorDetails(Optional.of(new Date(12525211L))) ) + .setJoinSelectivityDetails( new JoinSelectivityDetails(Optional.of(new Date(125221351L))) ) + .build(); + + when(mockGetInstanceDetails.getDetails(eq(instanceName))).thenReturn( Optional.of(details) ); + + final RyaClient mockCommands = mock(RyaClient.class); + when(mockCommands.getGetInstanceDetails()).thenReturn( mockGetInstanceDetails ); + + final SharedShellState state = new SharedShellState(); + state.connectedToAccumulo(mock(AccumuloConnectionDetails.class), mockCommands); + state.connectedToInstance(instanceName); + + // Execute the command. + final RyaAdminCommands commands = new RyaAdminCommands(state, mock(InstallPrompt.class), mock(SparqlPrompt.class), mock(UninstallPrompt.class)); + final String message = commands.printInstanceDetails(); + + // Verify the values that were provided to the command were passed through to the GetInstanceDetails. + verify(mockGetInstanceDetails).getDetails(eq(instanceName)); + + // Verify a message is returned that includes the details. + final String expected = + "General Metadata:\n" + + " Instance Name: test_instance\n" + + " RYA Version: 1.2.3.4\n" + + " Users: alice, bob, charlie\n" + + "Secondary Indicies:\n" + + " Entity Centric Index:\n" + + " Enabled: true\n" + + //RYA-215" Geospatial Index:\n" + + //RYA-215" Enabled: true\n" + + " Free Text Index:\n" + + " Enabled: true\n" + + " Temporal Index:\n" + + " Enabled: true\n" + + " PCJ Index:\n" + + " Enabled: true\n" + + " Fluo App Name: test_instance_rya_pcj_updater\n" + + " PCJs:\n" + + " ID: pcj 1\n" + + " Update Strategy: BATCH\n" + + " Last Update Time: Thu Jan 15 06:55:21 EST 1970\n" + + " ID: pcj 2\n" + + " Update Strategy: INCREMENTAL\n" + + " Last Update Time: unavailable\n" + + "Statistics:\n" + + " Prospector:\n" + + " Last Update Time: Wed Dec 31 22:28:45 EST 1969\n" + + " Join Selectivity:\n" + + " Last Updated Time: Fri Jan 02 05:47:01 EST 1970\n"; + assertEquals(expected, message); + } + + @Test + public void install() throws DuplicateInstanceNameException, RyaClientException, IOException { + // Mock the object that performs the install operation. + final Install mockInstall = mock(Install.class); + + final RyaClient mockCommands = mock(RyaClient.class); + when(mockCommands.getInstall()).thenReturn( mockInstall ); + + final SharedShellState state = new SharedShellState(); + state.connectedToAccumulo(mock(AccumuloConnectionDetails.class), mockCommands); + + // Execute the command. + final String instanceName = "unitTests"; + final InstallConfiguration installConfig = InstallConfiguration.builder() + .setEnableGeoIndex(true) + .setEnablePcjIndex(true) + .build(); + + final InstallPrompt mockInstallPrompt = mock(InstallPrompt.class); + when(mockInstallPrompt.promptInstanceName()).thenReturn( instanceName ); + when(mockInstallPrompt.promptInstallConfiguration(instanceName)).thenReturn( installConfig ); + when(mockInstallPrompt.promptVerified(eq(instanceName), eq(installConfig))).thenReturn(true); + + final RyaAdminCommands commands = new RyaAdminCommands(state, mockInstallPrompt, mock(SparqlPrompt.class), mock(UninstallPrompt.class)); + final String message = commands.install(); + + // Verify the values that were provided to the command were passed through to the Install. + verify(mockInstall).install(eq(instanceName), eq(installConfig)); + + // Verify a message is returned that indicates the success of the operation. + final String expected = "The Rya instance named 'unitTests' has been installed."; + assertEquals(expected, message); + } + + @Test + public void installWithParameters() throws DuplicateInstanceNameException, RyaClientException, IOException { + // Mock the object that performs the install operation. + final Install mockInstall = mock(Install.class); + + final RyaClient mockCommands = mock(RyaClient.class); + when(mockCommands.getInstall()).thenReturn( mockInstall ); + + final SharedShellState state = new SharedShellState(); + state.connectedToAccumulo(mock(AccumuloConnectionDetails.class), mockCommands); + + final String instanceName = "unitTests"; + final boolean enableTableHashPrefix = false; + final boolean enableEntityCentricIndex = true; + final boolean enableFreeTextIndex = false; + final boolean enableGeospatialIndex = true; + final boolean enableTemporalIndex = false; + final boolean enablePcjIndex = true; + final String fluoPcjAppName = instanceName + "pcj_updater"; + + // Execute the command. + final InstallConfiguration installConfig = InstallConfiguration.builder() + .setEnableTableHashPrefix(enableTableHashPrefix) + .setEnableEntityCentricIndex(enableEntityCentricIndex) + .setEnableFreeTextIndex(enableFreeTextIndex) + .setEnableGeoIndex(enableGeospatialIndex) + .setEnableTemporalIndex(enableTemporalIndex) + .setEnablePcjIndex(enablePcjIndex) + .setFluoPcjAppName(fluoPcjAppName) + .build(); + + final InstallPrompt mockInstallPrompt = mock(InstallPrompt.class); + when(mockInstallPrompt.promptInstanceName()).thenReturn( instanceName ); + when(mockInstallPrompt.promptInstallConfiguration(instanceName)).thenReturn( installConfig ); + when(mockInstallPrompt.promptVerified(eq(instanceName), eq(installConfig))).thenReturn(true); + + final RyaAdminCommands commands = new RyaAdminCommands(state, mockInstallPrompt, mock(SparqlPrompt.class), mock(UninstallPrompt.class)); + final String message = commands.installWithParameters(instanceName, enableTableHashPrefix, enableEntityCentricIndex, enableFreeTextIndex, enableGeospatialIndex, enableTemporalIndex, enablePcjIndex, fluoPcjAppName); + + // Verify the values that were provided to the command were passed through to the Install. + verify(mockInstall).install(eq(instanceName), eq(installConfig)); + + // Verify a message is returned that indicates the success of the operation. + final String expected = "The Rya instance named 'unitTests' has been installed."; + assertEquals(expected, message); + } + + @Test + public void installWithParameters_userAbort() throws DuplicateInstanceNameException, RyaClientException, IOException { + // Mock the object that performs the install operation. + final Install mockInstall = mock(Install.class); + + final RyaClient mockCommands = mock(RyaClient.class); + when(mockCommands.getInstall()).thenReturn( mockInstall ); + + final SharedShellState state = new SharedShellState(); + state.connectedToAccumulo(mock(AccumuloConnectionDetails.class), mockCommands); + + final String instanceName = "unitTests"; + final boolean enableTableHashPrefix = false; + final boolean enableEntityCentricIndex = true; + final boolean enableFreeTextIndex = false; + final boolean enableGeospatialIndex = true; + final boolean enableTemporalIndex = false; + final boolean enablePcjIndex = true; + final String fluoPcjAppName = instanceName + "pcj_updater"; + + // Execute the command. + final InstallConfiguration installConfig = InstallConfiguration.builder() + .setEnableTableHashPrefix(enableTableHashPrefix) + .setEnableEntityCentricIndex(enableEntityCentricIndex) + .setEnableFreeTextIndex(enableFreeTextIndex) + .setEnableGeoIndex(enableGeospatialIndex) + .setEnableTemporalIndex(enableTemporalIndex) + .setEnablePcjIndex(enablePcjIndex) + .setFluoPcjAppName(fluoPcjAppName) + .build(); + + final InstallPrompt mockInstallPrompt = mock(InstallPrompt.class); + when(mockInstallPrompt.promptInstanceName()).thenReturn( instanceName ); + when(mockInstallPrompt.promptInstallConfiguration(instanceName)).thenReturn( installConfig ); + when(mockInstallPrompt.promptVerified(eq(instanceName), eq(installConfig))).thenReturn(false); + + final RyaAdminCommands commands = new RyaAdminCommands(state, mockInstallPrompt, mock(SparqlPrompt.class), mock(UninstallPrompt.class)); + final String message = commands.installWithParameters(instanceName, enableTableHashPrefix, enableEntityCentricIndex, enableFreeTextIndex, enableGeospatialIndex, enableTemporalIndex, enablePcjIndex, fluoPcjAppName); + + // Verify a message is returned that indicates the success of the operation. + final String expected = "Skipping Installation."; + assertEquals(expected, message); + } + + @Test + public void listInstances() throws RyaClientException, IOException { + // Mock the object that performs the list operation. + final ListInstances mockListInstances = mock(ListInstances.class); + final List<String> instanceNames = Lists.newArrayList("a", "b", "c", "d"); + when(mockListInstances.listInstances()).thenReturn(instanceNames); + + final RyaClient mockCommands = mock(RyaClient.class); + when(mockCommands.getListInstances()).thenReturn( mockListInstances ); + + final SharedShellState state = new SharedShellState(); + state.connectedToAccumulo(mock(AccumuloConnectionDetails.class), mockCommands); + state.connectedToInstance("b"); + + // Execute the command. + final RyaAdminCommands commands = new RyaAdminCommands(state, mock(InstallPrompt.class), mock(SparqlPrompt.class), mock(UninstallPrompt.class)); + final String message = commands.listInstances(); + + // Verify a message is returned that lists the the instances. + final String expected = + "Rya instance names:\n" + + " a\n" + + " * b\n" + + " c\n" + + " d\n"; + assertEquals(expected, message); + } + + @Test + public void addUser() throws Exception { + // Mock the object that performs the Add User command. + final AddUser mockAddUser = mock(AddUser.class); + + final RyaClient mockClient = mock(RyaClient.class); + when(mockClient.getAddUser()).thenReturn( mockAddUser ); + + final SharedShellState state = new SharedShellState(); + state.connectedToAccumulo(mock(AccumuloConnectionDetails.class), mockClient); + state.connectedToInstance("test_instance"); + + // Execute the command. + final RyaAdminCommands commands = new RyaAdminCommands(state, mock(InstallPrompt.class), mock(SparqlPrompt.class), mock(UninstallPrompt.class)); + commands.addUser("alice"); + + // Verify the add request was forwarded to the client. + verify(mockAddUser).addUser(eq("test_instance"), eq("alice")); + } + + @Test + public void removeUser() throws Exception { + // Mock the object that performs the Add User command. + final RemoveUser mockRemoveUser = mock(RemoveUser.class); + + final RyaClient mockClient = mock(RyaClient.class); + when(mockClient.getRemoveUser()).thenReturn( mockRemoveUser ); + + final SharedShellState state = new SharedShellState(); + state.connectedToAccumulo(mock(AccumuloConnectionDetails.class), mockClient); + state.connectedToInstance("test_instance"); + + // Execute the command. + final RyaAdminCommands commands = new RyaAdminCommands(state, mock(InstallPrompt.class), mock(SparqlPrompt.class), mock(UninstallPrompt.class)); + commands.removeUser("alice"); + + // Verify the add request was forwarded to the client. + verify(mockRemoveUser).removeUser(eq("test_instance"), eq("alice")); + } + + @Test + public void uninstall_yes() throws Exception { + // Mock the object that performs the Uninstall command. + final Uninstall mockUninstall = mock(Uninstall.class); + + // Mock a prompt that says the user does want to uninstall it. + final UninstallPrompt uninstallPrompt = mock(UninstallPrompt.class); + when(uninstallPrompt.promptAreYouSure( eq("test_instance") )).thenReturn(true); + + final RyaClient mockClient = mock(RyaClient.class); + when(mockClient.getUninstall()).thenReturn( mockUninstall ); + + final SharedShellState state = new SharedShellState(); + state.connectedToAccumulo(mock(AccumuloConnectionDetails.class), mockClient); + state.connectedToInstance("test_instance"); + + // Execute the command. + final RyaAdminCommands commands = new RyaAdminCommands(state, mock(InstallPrompt.class), mock(SparqlPrompt.class), uninstallPrompt); + commands.uninstall(); + + // Verify the request was forwarded to the client. + verify(mockUninstall).uninstall(eq("test_instance")); + } + + @Test + public void uninstall_no() throws Exception { + // Mock the object that performs the Uninstall command. + final Uninstall mockUninstall = mock(Uninstall.class); + + // Mock a prompt that says the user does want to uninstall it. + final UninstallPrompt uninstallPrompt = mock(UninstallPrompt.class); + when(uninstallPrompt.promptAreYouSure( eq("test_instance") )).thenReturn(false); + + final RyaClient mockClient = mock(RyaClient.class); + when(mockClient.getUninstall()).thenReturn( mockUninstall ); + + final SharedShellState state = new SharedShellState(); + state.connectedToAccumulo(mock(AccumuloConnectionDetails.class), mockClient); + state.connectedToInstance("test_instance"); + + // Execute the command. + final RyaAdminCommands commands = new RyaAdminCommands(state, mock(InstallPrompt.class), mock(SparqlPrompt.class), uninstallPrompt); + commands.uninstall(); + + // Verify the request was forwarded to the client. + verify(mockUninstall, never()).uninstall(eq("test_instance")); + } +}
http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/2564ac0a/extras/shell/src/test/java/org/apache/rya/shell/RyaCommandsTest.java ---------------------------------------------------------------------- diff --git a/extras/shell/src/test/java/org/apache/rya/shell/RyaCommandsTest.java b/extras/shell/src/test/java/org/apache/rya/shell/RyaCommandsTest.java new file mode 100644 index 0000000..a0a3979 --- /dev/null +++ b/extras/shell/src/test/java/org/apache/rya/shell/RyaCommandsTest.java @@ -0,0 +1,278 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.rya.shell; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Paths; + +import org.apache.commons.io.FileUtils; +import org.apache.rya.api.client.ExecuteSparqlQuery; +import org.apache.rya.api.client.InstanceDoesNotExistException; +import org.apache.rya.api.client.LoadStatementsFile; +import org.apache.rya.api.client.RyaClient; +import org.apache.rya.api.client.RyaClientException; +import org.apache.rya.api.client.accumulo.AccumuloConnectionDetails; +import org.apache.rya.shell.util.ConsolePrinter; +import org.apache.rya.shell.util.SparqlPrompt; +import org.junit.Test; +import org.openrdf.rio.RDFFormat; + +import com.google.common.base.Optional; + +/** + * Unit tests the methods of {@link RyaAdminCommands}. + */ +public class RyaCommandsTest { + + @Test + public void testLoadData() throws InstanceDoesNotExistException, RyaClientException, IOException { + // Mock the object that performs the create operation. + final String instanceName = "unitTest"; + final String statementsFile = "/path/to/statements.nt"; + final String format = null; + + final LoadStatementsFile mockLoadStatementsFile = mock(LoadStatementsFile.class); + final RyaClient mockCommands = mock(RyaClient.class); + when(mockCommands.getLoadStatementsFile()).thenReturn(mockLoadStatementsFile); + + final SharedShellState state = new SharedShellState(); + state.connectedToAccumulo(mock(AccumuloConnectionDetails.class), mockCommands); + state.connectedToInstance(instanceName); + + final SparqlPrompt mockSparqlPrompt = mock(SparqlPrompt.class); + + final ConsolePrinter mockConsolePrinter = mock(ConsolePrinter.class); + + // Execute the command. + final RyaCommands commands = new RyaCommands(state, mockSparqlPrompt, mockConsolePrinter); + final String message = commands.loadData(statementsFile, format); + + // Verify the values that were provided to the command were passed through to LoadStatementsFile. + verify(mockLoadStatementsFile).loadStatements(instanceName, Paths.get(statementsFile), RDFFormat.NTRIPLES); + + // Verify a message is returned that explains what was created. + assertTrue(message.startsWith("Loaded the file: '" + statementsFile +"' successfully in ")); + assertTrue(message.endsWith(" seconds.")); + } + + @Test + public void testLoadData_specifyFormat() throws InstanceDoesNotExistException, RyaClientException, IOException { + // Mock the object that performs the create operation. + final String instanceName = "unitTest"; + final String statementsFile = "/path/to/statements.nt"; + final String format = "N-TRIPLES"; + + final LoadStatementsFile mockLoadStatementsFile = mock(LoadStatementsFile.class); + final RyaClient mockCommands = mock(RyaClient.class); + when(mockCommands.getLoadStatementsFile()).thenReturn(mockLoadStatementsFile); + + final SharedShellState state = new SharedShellState(); + state.connectedToAccumulo(mock(AccumuloConnectionDetails.class), mockCommands); + state.connectedToInstance(instanceName); + + final SparqlPrompt mockSparqlPrompt = mock(SparqlPrompt.class); + + final ConsolePrinter mockConsolePrinter = mock(ConsolePrinter.class); + + // Execute the command. + final RyaCommands commands = new RyaCommands(state, mockSparqlPrompt, mockConsolePrinter); + final String message = commands.loadData(statementsFile, format); + + // Verify the values that were provided to the command were passed through to LoadStatementsFile. + verify(mockLoadStatementsFile).loadStatements(instanceName, Paths.get(statementsFile), RDFFormat.NTRIPLES); + + // Verify a message is returned that explains what was created. + assertTrue(message.startsWith("Loaded the file: '" + statementsFile +"' successfully in ")); + assertTrue(message.endsWith(" seconds.")); + } + + @Test(expected = RuntimeException.class) + public void testLoadData_specifyInvalidFormat() throws InstanceDoesNotExistException, RyaClientException, IOException { + // Mock the object that performs the create operation. + final String instanceName = "unitTest"; + final String statementsFile = "/path/to/statements.nt"; + final String format = "INVALID_FORMAT_NAME"; + + final LoadStatementsFile mockLoadStatementsFile = mock(LoadStatementsFile.class); + final RyaClient mockCommands = mock(RyaClient.class); + when(mockCommands.getLoadStatementsFile()).thenReturn(mockLoadStatementsFile); + + final SharedShellState state = new SharedShellState(); + state.connectedToAccumulo(mock(AccumuloConnectionDetails.class), mockCommands); + state.connectedToInstance(instanceName); + + final SparqlPrompt mockSparqlPrompt = mock(SparqlPrompt.class); + + final ConsolePrinter mockConsolePrinter = mock(ConsolePrinter.class); + + // Execute the command. + final RyaCommands commands = new RyaCommands(state, mockSparqlPrompt, mockConsolePrinter); + + commands.loadData(statementsFile, format); + } + + @Test(expected = RuntimeException.class) + public void testLoadData_specifyInvalidFilenameFormat() throws InstanceDoesNotExistException, RyaClientException, IOException { + // Mock the object that performs the create operation. + final String instanceName = "unitTest"; + final String statementsFile = "/path/to/statements.invalidFormat"; + final String format = null; + + final LoadStatementsFile mockLoadStatementsFile = mock(LoadStatementsFile.class); + final RyaClient mockCommands = mock(RyaClient.class); + when(mockCommands.getLoadStatementsFile()).thenReturn(mockLoadStatementsFile); + + final SharedShellState state = new SharedShellState(); + state.connectedToAccumulo(mock(AccumuloConnectionDetails.class), mockCommands); + state.connectedToInstance(instanceName); + + final SparqlPrompt mockSparqlPrompt = mock(SparqlPrompt.class); + + final ConsolePrinter mockConsolePrinter = mock(ConsolePrinter.class); + + // Execute the command. + final RyaCommands commands = new RyaCommands(state, mockSparqlPrompt, mockConsolePrinter); + + commands.loadData(statementsFile, format); + } + + @Test + public void testSparqlQuery() throws InstanceDoesNotExistException, RyaClientException, IOException { + // Mock the object that performs the create operation. + final String instanceName = "unitTest"; + final String queryFile = "src/test/resources/Query1.sparql"; + final String queryContent = FileUtils.readFileToString(new File(queryFile), StandardCharsets.UTF_8); + final String expectedMessage = "MockAnswer"; + + final ExecuteSparqlQuery mockExecuteSparqlQuery = mock(ExecuteSparqlQuery.class); + when(mockExecuteSparqlQuery.executeSparqlQuery(instanceName, queryContent)).thenReturn(expectedMessage); + + final RyaClient mockCommands = mock(RyaClient.class); + when(mockCommands.getExecuteSparqlQuery()).thenReturn(mockExecuteSparqlQuery); + + + final SharedShellState state = new SharedShellState(); + state.connectedToAccumulo(mock(AccumuloConnectionDetails.class), mockCommands); + state.connectedToInstance(instanceName); + + final SparqlPrompt mockSparqlPrompt = mock(SparqlPrompt.class); + + final ConsolePrinter mockConsolePrinter = mock(ConsolePrinter.class); + + // Execute the command. + final RyaCommands commands = new RyaCommands(state, mockSparqlPrompt, mockConsolePrinter); + final String message = commands.sparqlQuery(queryFile); + + // Verify the values that were provided to the command were passed through to LoadStatementsFile. + verify(mockExecuteSparqlQuery).executeSparqlQuery(instanceName, queryContent); + + assertEquals(expectedMessage, message); + // Verify a message is returned that explains what was created. + } + + @Test(expected = RuntimeException.class) + public void testSparqlQuery_nonexistentFile() throws InstanceDoesNotExistException, RyaClientException, IOException { + // Mock the object that performs the create operation. + final String instanceName = "unitTest"; + final String queryFile = "src/test/resources/Nonexistent.sparql"; + + final RyaClient mockCommands = mock(RyaClient.class); + + final SharedShellState state = new SharedShellState(); + state.connectedToAccumulo(mock(AccumuloConnectionDetails.class), mockCommands); + state.connectedToInstance(instanceName); + + final SparqlPrompt mockSparqlPrompt = mock(SparqlPrompt.class); + + final ConsolePrinter mockConsolePrinter = mock(ConsolePrinter.class); + + // Execute the command. + final RyaCommands commands = new RyaCommands(state, mockSparqlPrompt, mockConsolePrinter); + commands.sparqlQuery(queryFile); + } + + @Test + public void testSparqlQuery_fromPrompt() throws InstanceDoesNotExistException, RyaClientException, IOException { + // Mock the object that performs the create operation. + final String instanceName = "unitTest"; + final String queryContent = "SELECT * WHERE { ?person <http://isA> ?noun }"; + final String queryFile = null; + final String expectedMessage = "MockAnswer"; + + final ExecuteSparqlQuery mockExecuteSparqlQuery = mock(ExecuteSparqlQuery.class); + when(mockExecuteSparqlQuery.executeSparqlQuery(instanceName, queryContent)).thenReturn(expectedMessage); + + final RyaClient mockCommands = mock(RyaClient.class); + when(mockCommands.getExecuteSparqlQuery()).thenReturn(mockExecuteSparqlQuery); + + + final SharedShellState state = new SharedShellState(); + state.connectedToAccumulo(mock(AccumuloConnectionDetails.class), mockCommands); + state.connectedToInstance(instanceName); + + final SparqlPrompt mockSparqlPrompt = mock(SparqlPrompt.class); + when(mockSparqlPrompt.getSparql()).thenReturn(Optional.of(queryContent)); + + final ConsolePrinter mockConsolePrinter = mock(ConsolePrinter.class); + + // Execute the command. + final RyaCommands commands = new RyaCommands(state, mockSparqlPrompt, mockConsolePrinter); + final String message = commands.sparqlQuery(queryFile); + + // Verify the values that were provided to the command were passed through to LoadStatementsFile. + verify(mockExecuteSparqlQuery).executeSparqlQuery(instanceName, queryContent); + + assertEquals(expectedMessage, message); + // Verify a message is returned that explains what was created. + } + + @Test + public void testSparqlQuery_fromPrompt_cancelled() throws InstanceDoesNotExistException, RyaClientException, IOException { + // Mock the object that performs the create operation. + final String instanceName = "unitTest"; + final String queryFile = null; + final String expectedMessage = ""; + + final RyaClient mockCommands = mock(RyaClient.class); + + final SharedShellState state = new SharedShellState(); + state.connectedToAccumulo(mock(AccumuloConnectionDetails.class), mockCommands); + state.connectedToInstance(instanceName); + + final SparqlPrompt mockSparqlPrompt = mock(SparqlPrompt.class); + when(mockSparqlPrompt.getSparql()).thenReturn(Optional.absent()); + + final ConsolePrinter mockConsolePrinter = mock(ConsolePrinter.class); + + // Execute the command. + final RyaCommands commands = new RyaCommands(state, mockSparqlPrompt, mockConsolePrinter); + final String message = commands.sparqlQuery(queryFile); + + assertEquals(expectedMessage, message); + // Verify a message is returned that explains what was created. + } + +} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/2564ac0a/extras/shell/src/test/java/org/apache/rya/shell/RyaConnectionCommandsIT.java ---------------------------------------------------------------------- diff --git a/extras/shell/src/test/java/org/apache/rya/shell/RyaConnectionCommandsIT.java b/extras/shell/src/test/java/org/apache/rya/shell/RyaConnectionCommandsIT.java new file mode 100644 index 0000000..f424c49 --- /dev/null +++ b/extras/shell/src/test/java/org/apache/rya/shell/RyaConnectionCommandsIT.java @@ -0,0 +1,250 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.rya.shell; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.when; + +import java.io.IOException; + +import org.apache.accumulo.minicluster.MiniAccumuloCluster; +import org.apache.rya.api.client.Install.InstallConfiguration; +import org.apache.rya.shell.SharedShellState.ConnectionState; +import org.apache.rya.shell.SharedShellState.ShellState; +import org.apache.rya.shell.util.InstallPrompt; +import org.apache.rya.shell.util.PasswordPrompt; +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.shell.Bootstrap; +import org.springframework.shell.core.CommandResult; +import org.springframework.shell.core.JLineShellComponent; + +/** + * Integration tests the methods of {@link RyaConnectionCommands}. + */ +public class RyaConnectionCommandsIT extends RyaShellITBase { + + @Test + public void connectAccumulo() throws IOException { + final MiniAccumuloCluster cluster = getCluster(); + final Bootstrap bootstrap = getTestBootstrap(); + final JLineShellComponent shell = getTestShell(); + + // Mock the user entering the correct password. + final ApplicationContext context = bootstrap.getApplicationContext(); + final PasswordPrompt mockPrompt = context.getBean( PasswordPrompt.class ); + when(mockPrompt.getPassword()).thenReturn("password".toCharArray()); + + // Execute the connect command. + final String cmd = + RyaConnectionCommands.CONNECT_ACCUMULO_CMD + " " + + "--username root " + + "--instanceName " + cluster.getInstanceName() + " "+ + "--zookeepers " + cluster.getZooKeepers(); + + final CommandResult connectResult = shell.executeCommand(cmd); + + // Ensure the connection was successful. + assertTrue( connectResult.isSuccess() ); + } + + @Test + public void connectAccumulo_noAuths() throws IOException { + final MiniAccumuloCluster cluster = getCluster(); + final Bootstrap bootstrap = getTestBootstrap(); + final JLineShellComponent shell = getTestShell(); + + // Mock the user entering the correct password. + final ApplicationContext context = bootstrap.getApplicationContext(); + final PasswordPrompt mockPrompt = context.getBean( PasswordPrompt.class ); + when(mockPrompt.getPassword()).thenReturn("password".toCharArray()); + + // Execute the command + final String cmd = + RyaConnectionCommands.CONNECT_ACCUMULO_CMD + " " + + "--username root " + + "--instanceName " + cluster.getInstanceName() + " "+ + "--zookeepers " + cluster.getZooKeepers(); + + final CommandResult connectResult = shell.executeCommand(cmd); + + // Ensure the connection was successful. + assertTrue( connectResult.isSuccess() ); + } + + @Test + public void connectAccumulo_wrongCredentials() throws IOException { + final MiniAccumuloCluster cluster = getCluster(); + final Bootstrap bootstrap = getTestBootstrap(); + final JLineShellComponent shell = getTestShell(); + + // Mock the user entering the wrong password. + final ApplicationContext context = bootstrap.getApplicationContext(); + final PasswordPrompt mockPrompt = context.getBean( PasswordPrompt.class ); + when(mockPrompt.getPassword()).thenReturn("asjifo[ijwa".toCharArray()); + + // Execute the command + final String cmd = + RyaConnectionCommands.CONNECT_ACCUMULO_CMD + " " + + "--username root " + + "--instanceName " + cluster.getInstanceName() + " "+ + "--zookeepers " + cluster.getZooKeepers(); + + final CommandResult connectResult = shell.executeCommand(cmd); + + // Ensure the command failed. + assertFalse( connectResult.isSuccess() ); + } + + @Test + public void printConnectionDetails_notConnected() { + final JLineShellComponent shell = getTestShell(); + + // Run the print connection details command. + final CommandResult printResult = shell.executeCommand( RyaConnectionCommands.PRINT_CONNECTION_DETAILS_CMD ); + final String msg = (String) printResult.getResult(); + + final String expected = "The shell is not connected to anything."; + assertEquals(expected, msg); + } + + @Test + public void printConnectionDetails_connectedToAccumulo() throws IOException { + final MiniAccumuloCluster cluster = getCluster(); + final Bootstrap bootstrap = getTestBootstrap(); + final JLineShellComponent shell = getTestShell(); + + // Mock the user entering the correct password. + final ApplicationContext context = bootstrap.getApplicationContext(); + final PasswordPrompt mockPrompt = context.getBean( PasswordPrompt.class ); + when(mockPrompt.getPassword()).thenReturn("password".toCharArray()); + + // Connect to the mini accumulo instance. + final String cmd = + RyaConnectionCommands.CONNECT_ACCUMULO_CMD + " " + + "--username root " + + "--instanceName " + cluster.getInstanceName() + " "+ + "--zookeepers " + cluster.getZooKeepers(); + shell.executeCommand(cmd); + + // Run the print connection details command. + final CommandResult printResult = shell.executeCommand( RyaConnectionCommands.PRINT_CONNECTION_DETAILS_CMD ); + final String msg = (String) printResult.getResult(); + + final String expected = + "The shell is connected to an instance of Accumulo using the following parameters:\n" + + " Username: root\n" + + " Instance Name: " + cluster.getInstanceName() + "\n" + + " Zookeepers: " + cluster.getZooKeepers(); + assertEquals(expected, msg); + } + + @Test + public void connectToInstance() throws IOException { + final MiniAccumuloCluster cluster = getCluster(); + final Bootstrap bootstrap = getTestBootstrap(); + final JLineShellComponent shell = getTestShell(); + + // Mock the user entering the correct password. + final ApplicationContext context = bootstrap.getApplicationContext(); + final PasswordPrompt mockPrompt = context.getBean( PasswordPrompt.class ); + when(mockPrompt.getPassword()).thenReturn("password".toCharArray()); + + // Connect to the mini accumulo instance. + String cmd = + RyaConnectionCommands.CONNECT_ACCUMULO_CMD + " " + + "--username root " + + "--instanceName " + cluster.getInstanceName() + " "+ + "--zookeepers " + cluster.getZooKeepers(); + CommandResult result = shell.executeCommand(cmd); + + // Install an instance of rya. + final String instanceName = "testInstance"; + final InstallConfiguration installConf = InstallConfiguration.builder().build(); + + final InstallPrompt installPrompt = context.getBean( InstallPrompt.class ); + when(installPrompt.promptInstanceName()).thenReturn("testInstance"); + when(installPrompt.promptInstallConfiguration("testInstance")).thenReturn( installConf ); + when(installPrompt.promptVerified(instanceName, installConf)).thenReturn(true); + + result = shell.executeCommand( RyaAdminCommands.INSTALL_CMD ); + assertTrue( result.isSuccess() ); + + // Connect to the instance that was just installed. + cmd = RyaConnectionCommands.CONNECT_INSTANCE_CMD + " --instance " + instanceName; + result = shell.executeCommand(cmd); + assertTrue( result.isSuccess() ); + + // Verify the shell state indicates it is connected to an instance. + final SharedShellState sharedState = context.getBean( SharedShellState.class ); + final ShellState state = sharedState.getShellState(); + assertEquals(ConnectionState.CONNECTED_TO_INSTANCE, state.getConnectionState()); + } + + @Test + public void connectToInstance_instanceDoesNotExist() throws IOException { + final MiniAccumuloCluster cluster = getCluster(); + final Bootstrap bootstrap = getTestBootstrap(); + final JLineShellComponent shell = getTestShell(); + + // Mock the user entering the correct password. + final ApplicationContext context = bootstrap.getApplicationContext(); + final PasswordPrompt mockPrompt = context.getBean( PasswordPrompt.class ); + when(mockPrompt.getPassword()).thenReturn("password".toCharArray()); + + // Connect to the mini accumulo instance. + String cmd = + RyaConnectionCommands.CONNECT_ACCUMULO_CMD + " " + + "--username root " + + "--instanceName " + cluster.getInstanceName() + " "+ + "--zookeepers " + cluster.getZooKeepers(); + shell.executeCommand(cmd); + + // Try to connect to a non-existing instance. + cmd = RyaConnectionCommands.CONNECT_INSTANCE_CMD + " --instance doesNotExist"; + final CommandResult result = shell.executeCommand(cmd); + assertFalse( result.isSuccess() ); + } + + @Test + public void disconnect() throws IOException { + final MiniAccumuloCluster cluster = getCluster(); + final Bootstrap bootstrap = getTestBootstrap(); + final JLineShellComponent shell = getTestShell(); + + // Mock the user entering the correct password. + final ApplicationContext context = bootstrap.getApplicationContext(); + final PasswordPrompt mockPrompt = context.getBean( PasswordPrompt.class ); + when(mockPrompt.getPassword()).thenReturn("password".toCharArray()); + + // Connect to the mini accumulo instance. + final String cmd = + RyaConnectionCommands.CONNECT_ACCUMULO_CMD + " " + + "--username root " + + "--instanceName " + cluster.getInstanceName() + " "+ + "--zookeepers " + cluster.getZooKeepers(); + shell.executeCommand(cmd); + + // Disconnect from it. + final CommandResult disconnectResult = shell.executeCommand( RyaConnectionCommands.DISCONNECT_COMMAND_NAME_CMD ); + assertTrue( disconnectResult.isSuccess() ); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/2564ac0a/extras/shell/src/test/java/org/apache/rya/shell/RyaPromptProviderTest.java ---------------------------------------------------------------------- diff --git a/extras/shell/src/test/java/org/apache/rya/shell/RyaPromptProviderTest.java b/extras/shell/src/test/java/org/apache/rya/shell/RyaPromptProviderTest.java new file mode 100644 index 0000000..d3eaa4e --- /dev/null +++ b/extras/shell/src/test/java/org/apache/rya/shell/RyaPromptProviderTest.java @@ -0,0 +1,80 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.rya.shell; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; + +import org.junit.Test; + +import org.apache.rya.api.client.RyaClient; +import org.apache.rya.api.client.accumulo.AccumuloConnectionDetails; + +/** + * Tests the methods of {@link RyaPromptProvider}. + */ +public class RyaPromptProviderTest { + + @Test + public void notConnected() { + // Create a shared state that is disconnected. + final SharedShellState sharedState = new SharedShellState(); + sharedState.disconnected(); + + // Create the prompt. + final String prompt = new RyaPromptProvider(sharedState).getPrompt(); + + // Verify the prompt is formatted correctly. + final String expected = "rya> "; + assertEquals(expected, prompt); + } + + @Test + public void isConnected_noInstanceName() { + // Create a shared state that is connected to a storage, but not a rya instance. + final SharedShellState sharedState = new SharedShellState(); + + final AccumuloConnectionDetails connectionDetails = new AccumuloConnectionDetails("", new char[]{}, "testInstance", ""); + sharedState.connectedToAccumulo(connectionDetails, mock(RyaClient.class)); + + // Create a prompt. + final String prompt = new RyaPromptProvider(sharedState).getPrompt(); + + // Verify the prompt is formatted correctly. + final String expected = "rya/testInstance> "; + assertEquals(expected, prompt); + } + + @Test + public void isConnected_hasInstanceName() { + // Create a shared state that is connected to a specific instance. + final SharedShellState sharedState = new SharedShellState(); + + final AccumuloConnectionDetails connectionDetails = new AccumuloConnectionDetails("", new char[]{}, "testInstance", ""); + sharedState.connectedToAccumulo(connectionDetails, mock(RyaClient.class)); + sharedState.connectedToInstance("testRya"); + + // Create a prompt. + final String prompt = new RyaPromptProvider(sharedState).getPrompt(); + + // Verify the prompt is formatted correctly. + final String expected = "rya/testInstance:testRya> "; + assertEquals(expected, prompt); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/2564ac0a/extras/shell/src/test/java/org/apache/rya/shell/RyaShellITBase.java ---------------------------------------------------------------------- diff --git a/extras/shell/src/test/java/org/apache/rya/shell/RyaShellITBase.java b/extras/shell/src/test/java/org/apache/rya/shell/RyaShellITBase.java new file mode 100644 index 0000000..bcbaa5b --- /dev/null +++ b/extras/shell/src/test/java/org/apache/rya/shell/RyaShellITBase.java @@ -0,0 +1,102 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.rya.shell; + +import java.io.IOException; + +import org.apache.accumulo.core.client.AccumuloException; +import org.apache.accumulo.core.client.AccumuloSecurityException; +import org.apache.accumulo.minicluster.MiniAccumuloCluster; +import org.apache.log4j.Level; +import org.apache.log4j.Logger; +import org.apache.rya.accumulo.MiniAccumuloSingleton; +import org.apache.rya.accumulo.RyaTestInstanceRule; +import org.apache.zookeeper.ClientCnxn; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.springframework.shell.Bootstrap; +import org.springframework.shell.core.JLineShellComponent; + +import org.apache.rya.accumulo.MiniAccumuloClusterInstance; + +/** + * All Rya Shell integration tests should extend this one. It provides startup + * and shutdown hooks for a Mini Accumulo Cluster when you start and stop testing. + * It also creates a new shell to test with between each test. + */ +public class RyaShellITBase { + + /** + * The bootstrap that was used to initialize the Shell that will be tested. + */ + private Bootstrap bootstrap; + + /** + * The shell that will be tested. + */ + private JLineShellComponent shell; + + @Rule + public RyaTestInstanceRule testInstance = new RyaTestInstanceRule(false); + + @BeforeClass + public static void killLoudLogs() { + Logger.getLogger(ClientCnxn.class).setLevel(Level.ERROR); + } + + @Before + public void startShell() throws IOException, InterruptedException, AccumuloException, AccumuloSecurityException { + // Bootstrap the shell with the test bean configuration. + bootstrap = new Bootstrap(new String[]{}, new String[]{"file:src/test/resources/RyaShellTest-context.xml"}); + shell = bootstrap.getJLineShellComponent(); + } + + @After + public void stopShell() throws IOException, InterruptedException { + shell.stop(); + } + + /** + * @return The bootstrap that was used to initialize the Shell that will be tested. + */ + public Bootstrap getTestBootstrap() { + return bootstrap; + } + + /** + * @return The shell that will be tested. + */ + public JLineShellComponent getTestShell() { + return shell; + } + + /** + * @return The cluster that is hosting the test. + */ + public MiniAccumuloCluster getCluster() { + return MiniAccumuloSingleton.getInstance().getCluster(); + } + + public String getInstanceName() { + return testInstance.getRyaInstanceName(); + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/2564ac0a/extras/shell/src/test/java/org/apache/rya/shell/SharedShellStateTest.java ---------------------------------------------------------------------- diff --git a/extras/shell/src/test/java/org/apache/rya/shell/SharedShellStateTest.java b/extras/shell/src/test/java/org/apache/rya/shell/SharedShellStateTest.java new file mode 100644 index 0000000..e79d186 --- /dev/null +++ b/extras/shell/src/test/java/org/apache/rya/shell/SharedShellStateTest.java @@ -0,0 +1,167 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.rya.shell; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; + +import org.junit.Test; + +import org.apache.rya.api.client.RyaClient; +import org.apache.rya.api.client.accumulo.AccumuloConnectionDetails; +import org.apache.rya.shell.SharedShellState.ConnectionState; +import org.apache.rya.shell.SharedShellState.ShellState; + +/** + * Tests the methods of {@link SharedShellState}. + */ +public class SharedShellStateTest { + + @Test + public void initialStateIsDisconnected() { + final SharedShellState state = new SharedShellState(); + + // Verify disconnected and no values are set. + final ShellState expected = ShellState.builder() + .setConnectionState(ConnectionState.DISCONNECTED) + .build(); + + assertEquals(expected, state.getShellState()); + } + + @Test + public void disconnectedToConnectedToStorage() { + final SharedShellState state = new SharedShellState(); + + // Connect to Accumulo. + final AccumuloConnectionDetails connectionDetails = mock(AccumuloConnectionDetails.class); + final RyaClient connectedCommands = mock(RyaClient.class); + state.connectedToAccumulo(connectionDetails, connectedCommands); + + // Verify the state. + final ShellState expected = ShellState.builder() + .setConnectionState(ConnectionState.CONNECTED_TO_STORAGE) + .setAccumuloConnectionDetails(connectionDetails) + .setConnectedCommands(connectedCommands) + .build(); + + assertEquals(expected, state.getShellState()); + } + + @Test(expected = IllegalStateException.class) + public void connectToStorageAgain() { + final SharedShellState state = new SharedShellState(); + + // Connect to Accumulo. + final AccumuloConnectionDetails connectionDetails = mock(AccumuloConnectionDetails.class); + final RyaClient connectedCommands = mock(RyaClient.class); + state.connectedToAccumulo(connectionDetails, connectedCommands); + + // Try to set the information again. + state.connectedToAccumulo(connectionDetails, connectedCommands); + } + + @Test + public void connectedToInstance() { + final SharedShellState state = new SharedShellState(); + + // Connect to Accumulo. + final AccumuloConnectionDetails connectionDetails = mock(AccumuloConnectionDetails.class); + final RyaClient connectedCommands = mock(RyaClient.class); + state.connectedToAccumulo(connectionDetails, connectedCommands); + + // Connect to an Instance. + state.connectedToInstance("instance"); + + // Verify the state. + final ShellState expected = ShellState.builder() + .setConnectionState(ConnectionState.CONNECTED_TO_INSTANCE) + .setAccumuloConnectionDetails(connectionDetails) + .setConnectedCommands(connectedCommands) + .setRyaInstanceName("instance") + .build(); + + assertEquals(expected, state.getShellState()); + } + + @Test + public void ConnectedToInstanceAgain() { + final SharedShellState state = new SharedShellState(); + + // Connect to Accumulo. + final AccumuloConnectionDetails connectionDetails = mock(AccumuloConnectionDetails.class); + final RyaClient connectedCommands = mock(RyaClient.class); + state.connectedToAccumulo(connectionDetails, connectedCommands); + + // Connect to an Instance. + state.connectedToInstance("instance"); + + // Connect to another instance. + state.connectedToInstance("secondInstance"); + + // Verify the state. + final ShellState expected = ShellState.builder() + .setConnectionState(ConnectionState.CONNECTED_TO_INSTANCE) + .setAccumuloConnectionDetails(connectionDetails) + .setConnectedCommands(connectedCommands) + .setRyaInstanceName("secondInstance") + .build(); + assertEquals(expected, state.getShellState()); + } + + @Test(expected = IllegalStateException.class) + public void connectedToInstanceWhileDisconnectedFromStorage() { + final SharedShellState state = new SharedShellState(); + + state.connectedToInstance("instance"); + } + + @Test + public void disconnected() { + final SharedShellState state = new SharedShellState(); + + // Connect to Accumulo and an instance. + final AccumuloConnectionDetails connectionDetails = mock(AccumuloConnectionDetails.class); + final RyaClient connectedCommands = mock(RyaClient.class); + state.connectedToAccumulo(connectionDetails, connectedCommands); + state.connectedToInstance("instance"); + + // Disconnect. + state.disconnected(); + + // Verify the state. + final ShellState expected = ShellState.builder() + .setConnectionState(ConnectionState.DISCONNECTED) + .build(); + assertEquals(expected, state.getShellState()); + } + + @Test + public void disconnectedAgain() { + // Indicate we have diconnected while already in the disconnected state. + final SharedShellState state = new SharedShellState(); + state.disconnected(); + + // Verify the state. + final ShellState expected = ShellState.builder() + .setConnectionState(ConnectionState.DISCONNECTED) + .build(); + assertEquals(expected, state.getShellState()); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/2564ac0a/extras/shell/src/test/java/org/apache/rya/shell/util/ConnectorFactoryIT.java ---------------------------------------------------------------------- diff --git a/extras/shell/src/test/java/org/apache/rya/shell/util/ConnectorFactoryIT.java b/extras/shell/src/test/java/org/apache/rya/shell/util/ConnectorFactoryIT.java new file mode 100644 index 0000000..c3a5e74 --- /dev/null +++ b/extras/shell/src/test/java/org/apache/rya/shell/util/ConnectorFactoryIT.java @@ -0,0 +1,57 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.rya.shell.util; + +import java.nio.CharBuffer; + +import org.apache.accumulo.core.client.AccumuloException; +import org.apache.accumulo.core.client.AccumuloSecurityException; +import org.junit.Test; + +import org.apache.rya.accumulo.AccumuloITBase; + +/** + * Tests the methods of {@link ConnectorFactory}. + */ +public class ConnectorFactoryIT extends AccumuloITBase { + + @Test + public void connect_successful() throws AccumuloException, AccumuloSecurityException { + // Setup the values that will be tested with. + final CharSequence password = CharBuffer.wrap( getPassword() ); + + final ConnectorFactory ac = new ConnectorFactory(); + ac.connect(getUsername(), + password, + getInstanceName(), + getZookeepers()); + } + + @Test(expected = AccumuloSecurityException.class) + public void connect_wrongCredentials() throws AccumuloException, AccumuloSecurityException { + // Setup the values that will be tested with. + final CharSequence password = CharBuffer.wrap( new char[] {'w','r','o','n','g','p','a','s','s'} ); + + final ConnectorFactory ac = new ConnectorFactory(); + ac.connect(getUsername(), + password, + getInstanceName(), + getZookeepers()); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/2564ac0a/extras/shell/src/test/java/org/apache/rya/shell/util/InstanceNamesFormatterTest.java ---------------------------------------------------------------------- diff --git a/extras/shell/src/test/java/org/apache/rya/shell/util/InstanceNamesFormatterTest.java b/extras/shell/src/test/java/org/apache/rya/shell/util/InstanceNamesFormatterTest.java new file mode 100644 index 0000000..d6e23df --- /dev/null +++ b/extras/shell/src/test/java/org/apache/rya/shell/util/InstanceNamesFormatterTest.java @@ -0,0 +1,81 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.rya.shell.util; + +import static org.junit.Assert.assertEquals; + +import java.util.List; + +import org.junit.Test; + +import com.beust.jcommander.internal.Lists; + +/** + * Tests an instance of {@link InstanceNamesFormatter}. + */ +public class InstanceNamesFormatterTest { + + @Test + public void format_withConnectedName() { + final List<String> instanceNames = Lists.newArrayList("a", "b", "c", "d"); + + final String formatted = new InstanceNamesFormatter().format(instanceNames, "c"); + + final String expected = + "Rya instance names:\n" + + " a\n" + + " b\n" + + " * c\n" + + " d\n"; + + assertEquals(expected, formatted); + } + + @Test + public void format_connectedNameNotInList() { + final List<String> instanceNames = Lists.newArrayList("a", "b", "c", "d"); + + final String formatted = new InstanceNamesFormatter().format(instanceNames, "not_in_list"); + + final String expected = + "Rya instance names:\n" + + " a\n" + + " b\n" + + " c\n" + + " d\n"; + + assertEquals(expected, formatted); + } + + @Test + public void format() { + final List<String> instanceNames = Lists.newArrayList("a", "b", "c", "d"); + + final String formatted = new InstanceNamesFormatter().format(instanceNames); + + final String expected = + "Rya instance names:\n" + + " a\n" + + " b\n" + + " c\n" + + " d\n"; + + assertEquals(expected, formatted); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/2564ac0a/extras/shell/src/test/java/org/apache/rya/shell/util/RyaDetailsFormatterTest.java ---------------------------------------------------------------------- diff --git a/extras/shell/src/test/java/org/apache/rya/shell/util/RyaDetailsFormatterTest.java b/extras/shell/src/test/java/org/apache/rya/shell/util/RyaDetailsFormatterTest.java new file mode 100644 index 0000000..9e45a4f --- /dev/null +++ b/extras/shell/src/test/java/org/apache/rya/shell/util/RyaDetailsFormatterTest.java @@ -0,0 +1,111 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.rya.shell.util; + +import static org.junit.Assert.assertEquals; + +import java.util.Date; +import java.util.TimeZone; + +import org.apache.rya.api.instance.RyaDetails; +import org.apache.rya.api.instance.RyaDetails.EntityCentricIndexDetails; +import org.apache.rya.api.instance.RyaDetails.FreeTextIndexDetails; +import org.apache.rya.api.instance.RyaDetails.JoinSelectivityDetails; +import org.apache.rya.api.instance.RyaDetails.PCJIndexDetails; +import org.apache.rya.api.instance.RyaDetails.PCJIndexDetails.FluoDetails; +import org.apache.rya.api.instance.RyaDetails.PCJIndexDetails.PCJDetails; +import org.apache.rya.api.instance.RyaDetails.PCJIndexDetails.PCJDetails.PCJUpdateStrategy; +import org.apache.rya.api.instance.RyaDetails.ProspectorDetails; +import org.apache.rya.api.instance.RyaDetails.TemporalIndexDetails; +import org.junit.Test; + +import com.google.common.base.Optional; + +/** + * Tests the methods of {@link RyaDetailsFormatter}. + */ +public class RyaDetailsFormatterTest { + + @Test + public void format() { + // This test failed if the default timezone was not EST, so now it's fixed at EST. + TimeZone.setDefault(TimeZone.getTimeZone("America/New_York")); + // Create the object that will be formatted. + final RyaDetails details = RyaDetails.builder().setRyaInstanceName("test_instance") + .setRyaVersion("1.2.3.4") + .addUser("alice") + .addUser("bob") + .addUser("charlie") + .setEntityCentricIndexDetails( new EntityCentricIndexDetails(true) ) + //RYA-215 .setGeoIndexDetails( new GeoIndexDetails(true) ) + .setTemporalIndexDetails( new TemporalIndexDetails(true) ) + .setFreeTextDetails( new FreeTextIndexDetails(true) ) + .setPCJIndexDetails( + PCJIndexDetails.builder() + .setEnabled(true) + .setFluoDetails( new FluoDetails("test_instance_rya_pcj_updater") ) + .addPCJDetails( + PCJDetails.builder() + .setId("pcj 1") + .setUpdateStrategy(PCJUpdateStrategy.BATCH) + .setLastUpdateTime( new Date(1252521351L) )) + .addPCJDetails( + PCJDetails.builder() + .setId("pcj 2") + .setUpdateStrategy(PCJUpdateStrategy.INCREMENTAL))) + .setProspectorDetails( new ProspectorDetails(Optional.of(new Date(12525211L))) ) + .setJoinSelectivityDetails( new JoinSelectivityDetails(Optional.of(new Date(125221351L))) ) + .build(); + + final String formatted = new RyaDetailsFormatter().format(details); + + // Verify the created object matches the expected result. + final String expected = + "General Metadata:\n" + + " Instance Name: test_instance\n" + + " RYA Version: 1.2.3.4\n" + + " Users: alice, bob, charlie\n" + + "Secondary Indicies:\n" + + " Entity Centric Index:\n" + + " Enabled: true\n" + + //RYA-215 " Geospatial Index:\n" + + //RYA-215 " Enabled: true\n" + + " Free Text Index:\n" + + " Enabled: true\n" + + " Temporal Index:\n" + + " Enabled: true\n" + + " PCJ Index:\n" + + " Enabled: true\n" + + " Fluo App Name: test_instance_rya_pcj_updater\n" + + " PCJs:\n" + + " ID: pcj 1\n" + + " Update Strategy: BATCH\n" + + " Last Update Time: Thu Jan 15 06:55:21 EST 1970\n" + + " ID: pcj 2\n" + + " Update Strategy: INCREMENTAL\n" + + " Last Update Time: unavailable\n" + + "Statistics:\n" + + " Prospector:\n" + + " Last Update Time: Wed Dec 31 22:28:45 EST 1969\n" + + " Join Selectivity:\n" + + " Last Updated Time: Fri Jan 02 05:47:01 EST 1970\n"; + + assertEquals(expected, formatted); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/2564ac0a/extras/shell/src/test/resources/Query1.sparql ---------------------------------------------------------------------- diff --git a/extras/shell/src/test/resources/Query1.sparql b/extras/shell/src/test/resources/Query1.sparql new file mode 100644 index 0000000..555607e --- /dev/null +++ b/extras/shell/src/test/resources/Query1.sparql @@ -0,0 +1,20 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +SELECT * WHERE { ?person <http://isA> ?noun } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/2564ac0a/extras/shell/src/test/resources/RyaShellTest-context.xml ---------------------------------------------------------------------- diff --git a/extras/shell/src/test/resources/RyaShellTest-context.xml b/extras/shell/src/test/resources/RyaShellTest-context.xml new file mode 100644 index 0000000..f7ffe0f --- /dev/null +++ b/extras/shell/src/test/resources/RyaShellTest-context.xml @@ -0,0 +1,63 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. + +--> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns:context="http://www.springframework.org/schema/context" + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> + + <!-- Tell Spring where it can find all of the Command components. --> + <context:component-scan base-package="org.apache.rya.shell"/> + + <!-- Define the shell state bean that will be shared across all of the commands. --> + <bean id="sharedShellState" class="org.apache.rya.shell.SharedShellState" /> + + <!-- We use a mock Password Prompt here to simulate a user entering a password. --> + <bean id="passwordPrompt" class="org.mockito.Mockito" factory-method="mock"> + <constructor-arg value="org.apache.rya.shell.util.PasswordPrompt" /> + </bean> + + <!-- We use a mock Install Prompt here to simulate a user entering the installation configuration. --> + <bean id="installPrompt" class="org.mockito.Mockito" factory-method="mock"> + <constructor-arg value="org.apache.rya.shell.util.InstallPrompt"/> + </bean> + + <!-- We use a mock SPARQL Prompt here to simulate a user entering the installation configuration. --> + <bean id="sparqlPrompt" class="org.mockito.Mockito" factory-method="mock"> + <constructor-arg value="org.apache.rya.shell.util.SparqlPrompt"/> + </bean> + + <!-- We use a mock ConsolePrintert here to simulate console output to the user. --> + <bean id="consolePrinter" class="org.mockito.Mockito" factory-method="mock"> + <constructor-arg value="org.apache.rya.shell.util.ConsolePrinter"/> + </bean> + + <!-- We use a mock Uninstall Prompt here to simulate a user entering the installation configuration. --> + <bean id="uninstallPrompt" class="org.mockito.Mockito" factory-method="mock"> + <constructor-arg value="org.apache.rya.shell.util.UninstallPrompt"/> + </bean> + + <!-- Define each of the beans that hold onto commands used by the shell. --> + <bean id="ryaConnectionCommands" class="org.apache.rya.shell.RyaConnectionCommands" /> + <bean id="ryaCommands" class="org.apache.rya.shell.RyaCommands" /> + <bean id="ryaAdminCommands" class="org.apache.rya.shell.RyaAdminCommands" /> +</beans> \ No newline at end of file