/** * Read a classpath resource into a string. * * @param pathOnClasspath the path of the resource on the classpath * @return the string representation */ public static String readResourceAsString(String pathOnClasspath) { try (InputStream stream = readResourceAsStream(pathOnClasspath)) { return IoUtil.read(stream); } catch (IOException e) { Fail.fail("Unable to read '" + pathOnClasspath + "'", e); return null; } }
protected static <T> Statistics time(String desc, int repeat, Callable<T> runnable, Consumer<T> cleanup) throws InterruptedException { sw.start(); try { sws.time(repeat, runnable, result -> { if (cleanup != null) cleanup.accept(result); }); } catch (Throwable t) { t.printStackTrace(); fail(t.getMessage()); } sw.stop(); // if (desc != null) Testing.print(60, "Time to " + desc + ":", sw.durations().statistics().getTotalAsString()); // Testing.print(60,"Total clock time:",sw.durations().statistics().getTotalAsString()); // Testing.print(54,"Time to invoke the functions:",sws); return sw.durations().statistics(); }
@Test public void testEmptyDataStore() throws Exception { try { JsonNode configNode = ObjectMapperFactory.create().readTree("{ db: 'testDataStore', datastore: ''}"); setUpSystem((ObjectNode) configNode); fail("InitializationException should have been thrown"); } catch (InitializationException ie) { // Expected } }
@Test public void testNullDataStore() throws Exception { try { JsonNode configNode = ObjectMapperFactory.create().readTree("{ db: 'testDataStore', datastore: null}"); setUpSystem((ObjectNode) configNode); fail("InitializationException should have been thrown"); } catch (InitializationException ie) { // Expected } }
@Test public void emptyConfig() throws Exception { ResourceState config = new DefaultResourceState(); //should fail since the config needs at least a DB property specified try { setUpSystem(config); fail(); } catch (InitializationException e) { //expected } }
@Test public void testExecuteError() throws Exception { mojo.getPackageNames().add(TableFail.class.getPackage().getName()); try { mojo.execute(); Fail.fail("Expected an exception due to invalid JPQL queries"); } catch (MojoFailureException e) { assertThat(e.getMessage()).isEqualTo("Some JPQL queries could not be validated"); } }
@Test public void nullDbConfig() throws Exception { ResourceState config = new DefaultResourceState(); config.putProperty("db", null); //should fail since the DB propery should be a string try { setUpSystem(config); fail(); } catch (InitializationException e) { //expected } }
@Test public void emptyDbConfig() throws Exception { ResourceState config = new DefaultResourceState(); config.putProperty("db", ""); //should fail since the DB propery should be a string try { setUpSystem(config); fail(); } catch (InitializationException e) { //expected } }
@Test public void invalidDbConfig() throws Exception { ResourceState config = new DefaultResourceState(); config.putProperty("db", 123); //should fail since the DB property should be a string try { setUpSystem(config); fail(); } catch (InitializationException e) { //expected } }
private void assertExceptionIsThrown(String queryString) { try ( Session session = openSession() ) { Transaction tx = null; try { tx = session.beginTransaction(); session.createQuery( queryString ).list(); tx.commit(); Fail.fail( "Expected exception for query: [" + queryString + "]" ); } catch ( PersistenceException e) { assertThat( e.getCause() ).isInstanceOf( HibernateException.class ); assertThat( e.getCause().getMessage() ).startsWith( "OGM000089: " ); } finally { if ( tx != null && tx.isActive() ) { tx.rollback(); } } } }
@Test public void deleteNonExistentCollection() throws Exception { String methodName = "testDeleteNonExistantCollection"; assertThat(db.getCollectionNames().contains(methodName)).isFalse(); try { client.delete(new RequestContext.Builder().build(), "/testApp/" + BASEPATH + "/" + methodName); Fail.fail("shouldn't get here"); } catch (ResourceNotFoundException rnfe) { // expected } }
@Test public void deleteInvalidId() throws Exception { String methodName = "testDeleteInvalidId"; assertThat(db.getCollectionNames().contains(methodName)).isFalse(); try { client.delete(new RequestContext.Builder().build(), "/testApp/" + BASEPATH + "/foobar123"); Fail.fail("shouldn't get here"); } catch (ResourceNotFoundException e) { // expected } }
@Test public void invalidHost() throws Exception { ResourceState config = new DefaultResourceState(); config.putProperty("db", "testInvalidHostDB"); ResourceState server = new DefaultResourceState(); server.putProperty("host", "myServer123ABC"); server.putProperty("port", 12345); List<ResourceState> serversResourceState = new ArrayList<>(); serversResourceState.add(server); config.putProperty("servers", serversResourceState); try { setUpSystem(config); fail(); } catch (InitializationException e) { //expected } }
@Test public void invalidType() throws Exception { ResourceState config = new DefaultResourceState(); config.putProperty("db", "testInvalidTypeDB"); config.putProperty("servers", new ArrayList()); ResourceState configReadPref = new DefaultResourceState(); configReadPref.putProperty(ReadPreferenceState.TYPE, "foobar"); config.putProperty(ReadPreferenceState.ID, configReadPref); try { setUpSystem(config); Fail.fail(); } catch (InitializationException e) { //expected } }
@Test public void deleteNonExistentId() throws Exception { String methodName = "testDeleteNonExistantId"; assertThat(db.getCollectionNames().contains(methodName)).isFalse(); ObjectId objectId = new ObjectId(); try { client.delete(new RequestContext.Builder().build(), "/testApp/" + BASEPATH + "/" + methodName + "/" + objectId.toString()); Fail.fail("shouldn't get here"); } catch (ResourceNotFoundException e) { // expected } }
@Test public void deleteGrandchildObject() throws Exception { String methodName = "testDeleteGrandchildObject"; assertThat(db.getCollectionNames().contains(methodName)).isFalse(); // create the object using the mongo driver directly BasicDBObject object = new BasicDBObject(); object.append("foo", new BasicDBObject("bar", new BasicDBObject("ABC", new BasicDBObject("123", "XYZ")))); db.getCollection(methodName).insert(object); String id = object.getObjectId("_id").toString(); assertThat(db.getCollection(methodName).findOne(new BasicDBObject("_id", new ObjectId(id)))).isNotNull(); // we should not be able to directly delete a child object try { client.delete(new RequestContext.Builder().returnFields(new DefaultReturnFields("bar(ABC(*))")).build(), "/testApp/" + BASEPATH + "/" + methodName + "/" + id + "/foo"); Fail.fail(); } catch (ResourceNotFoundException e) { // expected } assertThat((DBObject) object).isEqualTo(db.getCollection(methodName).findOne()); }
@Test public void getChildDirectly() throws Exception { String methodName = "testGetChildDirectly"; assertThat(db.collectionExists(methodName)).isFalse(); // create the object using the mongo driver directly BasicDBObject object = new BasicDBObject(); object.append("foo", "bar").append("abc", "123").append("obj", new BasicDBObject("foo2", "bar2")); object.append("child", new BasicDBObject("grandchild", new BasicDBObject("foo3", "bar3"))); db.getCollection(methodName).insert(object); assertEquals(1, db.getCollection(methodName).getCount()); String id = "ObjectId(\"" + object.getObjectId("_id").toString() + "\")"; try { client.read(new RequestContext.Builder().build(), "/testApp/" + BASEPATH + "/" + methodName + "/" + id + "/child"); Fail.fail(); } catch (ResourceNotFoundException e) { // expected } }
@Test public void deleteChildProperty() throws Exception { String methodName = "testDeleteChildProperty"; assertThat(db.getCollectionNames().contains(methodName)).isFalse(); // create the object using the mongo driver directly BasicDBObject object = new BasicDBObject(); object.append("foo", new BasicDBObject("bar", new BasicDBObject("ABC", 123))); db.getCollection(methodName).insert(object); String id = object.getObjectId("_id").toString(); assertThat(db.getCollection(methodName).findOne(new BasicDBObject("_id", new ObjectId(id)))).isNotNull(); // we should not be able to directly delete a child property try { client.delete(new RequestContext.Builder().build(), "/testApp/" + BASEPATH + "/" + methodName + "/" + id + "/foo/bar"); Fail.fail("We should not be able to directly delete a property on a child object"); } catch (ResourceNotFoundException e) { // expected } assertThat((DBObject) object).isEqualTo(db.getCollection(methodName).findOne()); }
@Test public void deleteChildObject() throws Exception { String methodName = "testDeleteChildObject"; assertThat(db.getCollectionNames().contains(methodName)).isFalse(); // create the object using the mongo driver directly BasicDBObject object = new BasicDBObject(); object.append("foo", new BasicDBObject("bar", new BasicDBObject("ABC", 123))); db.getCollection(methodName).insert(object); String id = object.getObjectId("_id").toString(); assertThat(db.getCollection(methodName).findOne(new BasicDBObject("_id", new ObjectId(id)))).isNotNull(); // we should not be able to directly delete a child object try { client.delete(new RequestContext.Builder().returnFields(new DefaultReturnFields("bar(ABC(*))")).build(), "/testApp/" + BASEPATH + "/" + methodName + "/" + id + "/foo"); Fail.fail("We should not be able to directly delete a child object"); } catch (ResourceNotFoundException e) { // expected } assertThat((DBObject) object).isEqualTo(db.getCollection(methodName).findOne()); }