|
| 1 | +package fr.inria.corese.command.utils.http; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 5 | + |
| 6 | +import java.io.PrintWriter; |
| 7 | +import java.io.StringWriter; |
| 8 | +import java.lang.reflect.Field; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | + |
| 14 | +import fr.inria.corese.command.programs.AbstractCommand; |
| 15 | +import fr.inria.corese.command.programs.QueryEndpoint; |
| 16 | +import picocli.CommandLine; |
| 17 | +import picocli.CommandLine.Model.CommandSpec; |
| 18 | + |
| 19 | +public class SparqlHttpClientTest { |
| 20 | + |
| 21 | + private final ArrayList<String> emptyList = new ArrayList<String>(); |
| 22 | + |
| 23 | + // Default data for the tests |
| 24 | + private final String serverUrl = "http://localhost:8080/sparql"; |
| 25 | + private final String graphUri = "http://example.orgraphUrig/graph"; |
| 26 | + |
| 27 | + /** |
| 28 | + * Don't look at it. This is an ugly hack to get a valid CommandSpec instance for the tests. |
| 29 | + * (It uses reflective API to get a protected attribute of the QueryEndpoint class) |
| 30 | + * We are only testing the query validation which is not concerned with CommandSpec (but a valid one is required anyway). |
| 31 | + * @return |
| 32 | + */ |
| 33 | + private CommandSpec createCommandSpecObject() { |
| 34 | + Field spec = null; |
| 35 | + |
| 36 | + QueryEndpoint queryEndpoint = new QueryEndpoint(); |
| 37 | + CommandLine cmd = new CommandLine(queryEndpoint); |
| 38 | + |
| 39 | + PrintWriter out = new PrintWriter(new StringWriter()); |
| 40 | + PrintWriter err = new PrintWriter(new StringWriter()); |
| 41 | + cmd.setOut(out); |
| 42 | + cmd.setErr(err); |
| 43 | + |
| 44 | + try { |
| 45 | + spec = AbstractCommand.class.getDeclaredField("spec"); |
| 46 | + } catch (IllegalArgumentException | SecurityException | NoSuchFieldException e) { |
| 47 | + System.err.println("*** Error in reflective API getting a ComandSpec object"); |
| 48 | + e.printStackTrace(); |
| 49 | + return null; |
| 50 | + } |
| 51 | + |
| 52 | + try { |
| 53 | + spec.setAccessible(true); |
| 54 | + return (CommandSpec) (spec.get(queryEndpoint)); |
| 55 | + } catch (IllegalArgumentException | IllegalAccessException e) { |
| 56 | + e.printStackTrace(); |
| 57 | + return null; |
| 58 | + } |
| 59 | + |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Test that validateQuery fails for an empty query |
| 64 | + */ |
| 65 | + @Test |
| 66 | + public void testValidateQueryFalseForEmptyQuery() { |
| 67 | + |
| 68 | + SparqlHttpClient httpClient = new SparqlHttpClient(createCommandSpecObject(), serverUrl); |
| 69 | + |
| 70 | + /* Invalid null query */ |
| 71 | + assertThrows( |
| 72 | + IllegalArgumentException.class, |
| 73 | + () -> httpClient.validateQuery(null, emptyList, emptyList) ); |
| 74 | + |
| 75 | + /* Invalid empty query */ |
| 76 | + assertThrows( |
| 77 | + IllegalArgumentException.class, |
| 78 | + () -> httpClient.validateQuery("", emptyList, emptyList) ); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Test that validateQuery fails for query with a syntax error |
| 83 | + */ |
| 84 | + @Test |
| 85 | + public void testValidateQueryFalseForSyntaxError() { |
| 86 | + |
| 87 | + SparqlHttpClient httpClient = new SparqlHttpClient(createCommandSpecObject(), serverUrl); |
| 88 | + |
| 89 | + /* valid query : passes */ |
| 90 | + httpClient.validateQuery("SELECT * WHERE { ?s ?p ?o }", emptyList, emptyList ); |
| 91 | + |
| 92 | + /* Invalid query: syntax error */ |
| 93 | + assertThrows( |
| 94 | + IllegalArgumentException.class, |
| 95 | + () -> httpClient.validateQuery("SELECT * WHERE", emptyList, emptyList) ); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Test that validateQuery fails for update query using the POST request method |
| 100 | + */ |
| 101 | + @Test |
| 102 | + public void testValidateQueryFalseForUpdateWithGET() { |
| 103 | + String query = "DELETE WHERE { ?s ?p ?o }"; |
| 104 | + SparqlHttpClient httpClient = new SparqlHttpClient(createCommandSpecObject(), serverUrl); |
| 105 | + |
| 106 | + /* update query not defined by user: will set the requestMethod and pass */ |
| 107 | + httpClient.validateQuery(query, emptyList, emptyList ); |
| 108 | + |
| 109 | + /* update query defined by user with POST request method: passes */ |
| 110 | + httpClient.setRequestMethod(EnumRequestMethod.POST_URLENCODED); |
| 111 | + httpClient.validateQuery(query, emptyList, emptyList ); |
| 112 | + |
| 113 | + /* Invalid update query: defined by user with GET request method */ |
| 114 | + httpClient.setRequestMethod(EnumRequestMethod.GET); |
| 115 | + assertThrows( |
| 116 | + IllegalArgumentException.class, |
| 117 | + () -> httpClient.validateQuery(query, emptyList, emptyList) ); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Test that validateQuery fails for an clause FROM and non empty GraphURI |
| 122 | + */ |
| 123 | + @Test |
| 124 | + public void testValidateQueryFalseForFromClauseAndGraphURI() { |
| 125 | + String query = "SELECT * FROM <" + graphUri + "> WHERE { ?s ?p ?o }"; |
| 126 | + |
| 127 | + List<String> nonEmptyList = new ArrayList<String>(); |
| 128 | + nonEmptyList.add(graphUri); |
| 129 | + |
| 130 | + SparqlHttpClient httpClient = new SparqlHttpClient(createCommandSpecObject(), serverUrl); |
| 131 | + |
| 132 | + /* Valid query with FROM clause and empty URI list */ |
| 133 | + httpClient.validateQuery(query, emptyList, emptyList); |
| 134 | + |
| 135 | + /* Invalid query with FROM clause and non empty URI list */ |
| 136 | + assertThrows( |
| 137 | + IllegalArgumentException.class, |
| 138 | + () -> httpClient.validateQuery(query, nonEmptyList, emptyList) ); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Test that validateQuery fails for a WITH clause and non empty GraphURI |
| 143 | + */ |
| 144 | + @Test |
| 145 | + public void testValidateQueryFalseForWithClauseAndGraphURI() { |
| 146 | + String query = "WITH <" + graphUri + "> DELETE { ?s ?p ?o } WHERE { ?s ?p ?o }"; |
| 147 | + |
| 148 | + List<String> nonEmptyList = new ArrayList<String>(); |
| 149 | + nonEmptyList.add(graphUri); |
| 150 | + |
| 151 | + SparqlHttpClient httpClient = new SparqlHttpClient(createCommandSpecObject(), serverUrl); |
| 152 | + |
| 153 | + /* Valid query having WITH clause and empty URI list */ |
| 154 | + httpClient.validateQuery(query, emptyList, emptyList); |
| 155 | + |
| 156 | + /* Invalid query having WITH clause and non empty URI list */ |
| 157 | + assertThrows( |
| 158 | + IllegalArgumentException.class, |
| 159 | + () -> httpClient.validateQuery(query, nonEmptyList, emptyList) ); |
| 160 | + } |
| 161 | + |
| 162 | +} |
0 commit comments