QueryResourceProvider implements ResourceProvider<QueryResource> { @Override public Set<QueryResource> getResources(ReadRequest request) throws SystemException, NoSuchResourceException, UnsupportedPropertyException { Set<QueryResource> resources; try { LOG.debug("Get queries: View Context Properties = {}", viewContext.getProperties()); resources = getQueries(); LOG.debug("Retrieved {} queries from HAWQ Master", resources.size()); } catch (SQLException e) { LOG.error(e.getMessage(), e); throw new SystemException("Can't get current queries.", e); } return resources; }  @Override QueryResource getResource(String resourceId, Set<String> propertyIds); @Override Set<QueryResource> getResources(ReadRequest request); @Override void createResource(String resourceId, Map<String, Object> stringObjectMap); @Override boolean updateResource(String resourceId, Map<String, Object> stringObjectMap); @Override boolean deleteResource(String resourceId);  }
@Test public void testGetResources() throws Exception { expect(viewContext.getProperties()).andReturn(prepareViewContextProperties()).anyTimes(); expect(dataSource.getConnection("host1", "1234", "gpadmin-user", "gpadmin-password")).andReturn(connection); expect(connection.createStatement()).andReturn(statement); expect(statement.executeQuery(anyString())).andReturn(rs); expect(rs.getMetaData()).andReturn(rsMetaData); expect(rsMetaData.getColumnCount()).andReturn(2); expect(rs.next()).andReturn(true); expect(rsMetaData.getColumnName(1)).andReturn("procpid"); expect(rs.getObject(1)).andReturn(10); expect(rsMetaData.getColumnName(2)).andReturn("text"); expect(rs.getObject(2)).andReturn("text-10"); expect(rs.next()).andReturn(true); expect(rsMetaData.getColumnName(1)).andReturn("procpid"); expect(rs.getObject(1)).andReturn(20); expect(rsMetaData.getColumnName(2)).andReturn("text"); expect(rs.getObject(2)).andReturn("text-20"); expect(rs.next()).andReturn(false); rs.close(); expectLastCall().once(); statement.close(); expectLastCall().once(); connection.close(); expectLastCall().once(); replayAll(); Set<QueryResource> results = provider.getResources(null); assertEquals(2, results.size()); Set<String> ids = new HashSet<>(); ids.add("10"); ids.add("20"); for (QueryResource query : results) { String id = query.getId(); ids.remove(id); assertEquals(id, query.getAttributes().get("procpid").toString()); assertEquals("text-" + id, query.getAttributes().get("text")); } assertTrue(ids.isEmpty()); verifyAll(); }
@Test public void testGetResources_MissingUserName() throws Exception { thrown.expect(SystemException.class); thrown.expectMessage("HAWQ User Name property is not specified for the view instance."); Map<String, String> props = prepareViewContextProperties(); props.remove("hawq.user.name"); expect(viewContext.getProperties()).andReturn(props).anyTimes(); replayAll(); provider.getResources(null); }
@Test public void testGetResources_MissingUserPassword() throws Exception { thrown.expect(SystemException.class); thrown.expectMessage("HAWQ User Password property is not specified for the view instance."); Map<String, String> props = prepareViewContextProperties(); props.remove("hawq.user.password"); expect(viewContext.getProperties()).andReturn(props).anyTimes(); replayAll(); provider.getResources(null); }
@Test public void testGetResources_MissingHostName() throws Exception { thrown.expect(SystemException.class); thrown.expectMessage("HAWQ Master Host property is not specified for the view instance."); Map<String, String> props = prepareViewContextProperties(); props.remove("hawq.master.host"); expect(viewContext.getProperties()).andReturn(props).anyTimes(); replayAll(); provider.getResources(null); }
@Test public void testGetResources_MissingHostPort() throws Exception { thrown.expect(SystemException.class); thrown.expectMessage("HAWQ Master Port property is not specified for the view instance."); Map<String, String> props = prepareViewContextProperties(); props.remove("hawq.master.port"); expect(viewContext.getProperties()).andReturn(props).anyTimes(); replayAll(); provider.getResources(null); }
@Test public void testGetResources_ExceptionOnExecute() throws Exception { thrown.expect(SystemException.class); thrown.expectMessage("Can't get current queries."); expect(viewContext.getProperties()).andReturn(prepareViewContextProperties()).anyTimes(); expect(dataSource.getConnection("host1", "1234", "gpadmin-user", "gpadmin-password")).andReturn(connection); expect(connection.createStatement()).andReturn(statement); expect(statement.executeQuery(anyString())).andStubThrow(new SQLException("test message")); statement.close(); expectLastCall().once(); connection.close(); expectLastCall().once(); replayAll(); provider.getResources(null); }