public void testDefaultQuery() throws Exception { Resource r1 = registry.newResource(); String r1Content = "this is r1 content"; r1.setContent(r1Content.getBytes()); r1.setDescription("production ready."); String r1Path = "/c3/r1"; registry.put(r1Path, r1); Resource r2 = registry.newResource(); String r2Content = "content for r2 :)"; r2.setContent(r2Content); r2.setDescription("ready for production use."); String r2Path = "/c3/r2"; registry.put(r2Path, r2); Resource r3 = registry.newResource(); String r3Content = "content for r3 :)"; r3.setContent(r3Content); r3.setDescription("only for government use."); String r3Path = "/c3/r3"; registry.put(r3Path, r3); registry.applyTag("/c3/r1", "java"); registry.applyTag("/c3/r2", "jsp"); registry.applyTag("/c3/r3", "ajax"); String sql1 = "SELECT RT.REG_TAG_ID FROM REG_RESOURCE_TAG RT, REG_RESOURCE R " + "WHERE (R.REG_VERSION=RT.REG_VERSION OR " + "(R.REG_PATH_ID=RT.REG_PATH_ID AND R.REG_NAME=RT.REG_RESOURCE_NAME)) " + "AND R.REG_DESCRIPTION LIKE ? ORDER BY RT.REG_TAG_ID"; Resource q1 = systemRegistry.newResource(); q1.setContent(sql1); q1.setMediaType(RegistryConstants.SQL_QUERY_MEDIA_TYPE); q1.addProperty(RegistryConstants.RESULT_TYPE_PROPERTY_NAME, RegistryConstants.TAGS_RESULT_TYPE); systemRegistry.put("/qs/q3", q1); Map<String, String> parameters = new HashMap<String, String>(); parameters.put("1", "%production%"); Collection result = registry.executeQuery("/qs/q3", parameters); String[] tagPaths = result.getChildren(); assertEquals("There should be two matching tags.", tagPaths.length, 2); Resource tag2 = registry.get(tagPaths[0]); assertEquals("First matching tag should be 'java'", (String) tag2.getContent(), "java"); Resource tag1 = registry.get(tagPaths[1]); assertEquals("Second matching tag should be 'jsp'", (String) tag1.getContent(), "jsp"); }
public void testWithoutTableParamsQuery() throws Exception { Resource r1 = registry.newResource(); String r1Content = "this is r1 content"; r1.setContent(r1Content.getBytes()); r1.setDescription("production ready."); String r1Path = "/c1/r1"; registry.put(r1Path, r1); Resource r2 = registry.newResource(); String r2Content = "content for r2 :)"; r2.setContent(r2Content); r2.setDescription("ready for production use."); String r2Path = "/c2/r2"; registry.put(r2Path, r2); Resource r3 = registry.newResource(); String r3Content = "content for r3 :)"; r3.setContent(r3Content); r3.setDescription("only for government use."); String r3Path = "/c2/r3"; registry.put(r3Path, r3); String sql1 = "SELECT REG_PATH_ID, REG_NAME FROM REG_RESOURCE, " + "REG_TAG WHERE REG_DESCRIPTION LIKE ?"; Resource q1 = systemRegistry.newResource(); q1.setContent(sql1); q1.setMediaType(RegistryConstants.SQL_QUERY_MEDIA_TYPE); q1.addProperty( RegistryConstants.RESULT_TYPE_PROPERTY_NAME, RegistryConstants.RESOURCES_RESULT_TYPE); systemRegistry.put("/qs/q1", q1); Map<String, String> parameters = new HashMap<String, String>(); parameters.put("1", "%production%"); Resource result = registry.executeQuery("/qs/q1", parameters); assertTrue( "Search with result type Resource should return a directory.", result instanceof org.wso2.carbon.registry.core.Collection); List<String> matchingPaths = new ArrayList<String>(); String[] paths = (String[]) result.getContent(); matchingPaths.addAll(Arrays.asList(paths)); assertTrue("Path /c1/r1 should be in the results.", matchingPaths.contains("/c1/r1")); assertTrue("Path /c2/r2 should be in the results.", matchingPaths.contains("/c2/r2")); }
public void testWithoutWhereQuery() throws Exception { Resource r1 = registry.newResource(); String r1Content = "this is r1 content"; r1.setContent(r1Content.getBytes()); r1.setDescription("production ready."); String r1Path = "/c1/r1"; registry.put(r1Path, r1); Resource r2 = registry.newResource(); String r2Content = "content for r2 :)"; r2.setContent(r2Content); r2.setDescription("ready for production use."); String r2Path = "/c2/r2"; registry.put(r2Path, r2); Resource r3 = registry.newResource(); String r3Content = "content for r3 :)"; r3.setContent(r3Content); r3.setDescription("only for government use."); String r3Path = "/c2/r3"; registry.put(r3Path, r3); String sql1 = "SELECT REG_PATH_ID, REG_NAME FROM REG_RESOURCE, REG_TAG"; Resource q1 = systemRegistry.newResource(); q1.setContent(sql1); q1.setMediaType(RegistryConstants.SQL_QUERY_MEDIA_TYPE); q1.addProperty( RegistryConstants.RESULT_TYPE_PROPERTY_NAME, RegistryConstants.RESOURCES_RESULT_TYPE); systemRegistry.put("/qs/q1", q1); Map parameters = new HashMap(); Resource result = registry.executeQuery("/qs/q1", parameters); assertTrue( "Search with result type Resource should return a directory.", result instanceof org.wso2.carbon.registry.core.Collection); String[] paths = (String[]) result.getContent(); assertTrue("Should return all the resources", paths.length >= 3); }