/** * MkContainer can return certain answers for matching conditions. * @throws Exception If something goes wrong inside. */ @Test public void answersConditionally() throws Exception { final String match = "matching"; final String mismatch = "not matching"; try (final MkContainer container = new MkGrizzlyContainer()) { container.next( new MkAnswer.Simple(mismatch), Matchers.not(new IsAnything<MkQuery>()) ).next(new MkAnswer.Simple(match), new IsAnything<MkQuery>()) .start(); new JdkRequest(container.home()) .through(VerboseWire.class) .fetch().as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .assertBody( Matchers.allOf( Matchers.is(match), Matchers.not(mismatch) ) ); } }
/** * MkContainer can answer multiple times for matching requests. * @throws Exception If something goes wrong inside */ @Test public void canAnswerMultipleTimes() throws Exception { final String body = "multiple"; final int times = 5; try (final MkContainer container = new MkGrizzlyContainer()) { container.next( new MkAnswer.Simple(body), new IsAnything<MkQuery>(), times ).start(); final Request req = new JdkRequest(container.home()) .through(VerboseWire.class); for (int idx = 0; idx < times; idx += 1) { req.fetch().as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .assertBody(Matchers.is(body)); } } }
/** * MkContainer can prioritize multiple matching answers by using the * first matching request. * @throws Exception If something goes wrong inside. */ @Test public void prioritizesMatchingAnswers() throws Exception { final String first = "first"; final String second = "second"; try (final MkContainer container = new MkGrizzlyContainer()) { container .next(new MkAnswer.Simple(first), new IsAnything<MkQuery>()) .next(new MkAnswer.Simple(second), new IsAnything<MkQuery>()) .start(); new JdkRequest(container.home()) .through(VerboseWire.class) .fetch().as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .assertBody( Matchers.allOf( Matchers.is(first), Matchers.not(second) ) ); } }
@Test public void getExtractionResultReturnCorrectEntities() { // GIVEN String fixture1 = "Fixture1"; Integer fixture2 = 2; MappingFixture mappingFixture = new MappingFixture(fixture1, fixture2) .addExtractorMatcher(ExtractorMatcher.create(new IsAnything())); // WHEN ExtractionResult extractionResult = mappingFixture.getExtractionResult(); // THEN assertThat(extractionResult.getEntities()).containsOnly(fixture1, fixture2); }
@Test public void testTransform() throws NoSuchFieldException { // GIVEN XlsxFileFixture xlsxFileFixture = new XlsxFileFixture("classpath:tests/fixtures/xlsx-fixture.xlsx"); xlsxFileFixture.addExtractorMatcher(ExtractorMatcher.create(new IsAnything())); xlsxFileFixture.setFilter(new Filter() { @Override public boolean filter(Object entity) { return false; } }); // WHEN ObjectFixture transformedFixture = xlsxFileFixtureTransformer.transform(xlsxFileFixture); // THEN assertCorrectCartEntry(xlsxFileFixture, transformedFixture); }
@Test public void testTransform() throws NoSuchFieldException { // GIVEN XmlFileFixture xmlFileFixture = new XmlFileFixture("classpath:tests/fixtures/xml-fixture.xml"); xmlFileFixture.addExtractorMatcher(ExtractorMatcher.create(new IsAnything())); xmlFileFixture.setFilter(new Filter() { @Override public boolean filter(Object entity) { return false; } }); // WHEN ObjectFixture transformedFixture = xmlFileFixtureTransformer.transform(xmlFileFixture); // THEN assertCorrectCartEntry(xmlFileFixture, transformedFixture); }
@Test public void testTransform() throws NoSuchFieldException { // GIVEN SpringFixture springFixture = new SpringFixture("classpath:/tests/fixtures/spring-fixture.xml", User.class); springFixture.addExtractorMatcher(ExtractorMatcher.create(new IsAnything())); springFixture.setFilter(new Filter() { @Override public boolean filter(Object entity) { return false; } }); // WHEN ObjectFixture transformedFixture = springFixtureTransformer.transform(springFixture); // THEN assertThat(transformedFixture).isInstanceOf(Fixture.class); assertThat(transformedFixture.getObjects()).hasSize(1); assertThat(transformedFixture.getObjects().get(0)).isInstanceOf(User.class); assertThat(springFixture.getExtractionResult().getEntities()).hasSize(1); assertThat(FilterableFixtureProxy.get(transformedFixture).filter("")).isFalse(); }
@Test public void testTransform() throws NoSuchFieldException { // GIVEN XlsFileFixture xlsFileFixture = new XlsFileFixture("classpath:tests/fixtures/xls-fixture.xls"); xlsFileFixture.addExtractorMatcher(ExtractorMatcher.create(new IsAnything())); xlsFileFixture.setFilter(new Filter() { @Override public boolean filter(Object entity) { return false; } }); // WHEN ObjectFixture transformedFixture = xlsFileFixtureTransformer.transform(xlsFileFixture); // THEN assertCorrectCartEntry(xlsFileFixture, transformedFixture); }
@Test public void getExtractionResultReturnCorrectEntities() { // GIVEN generatedFixture // .addGenerators( // GeneratedFixture.from(new Product()) // .addFieldGenerator("id", FieldGenerators.in("1", "2")) // ) // .addExtractorMatcher(ExtractorMatcher.create("name", new IsAnything())) // .start(); // WHEN while (generatedFixture.hasNext()) { generatedFixture.next(); } // THEN assertThat(generatedFixture.getExtractionResult().getEntities()).hasSize(2); }
@Test public void getExtractionResultReturnCorrectEntities() throws IOException { // GIVEN File file = folder.newFile("foo.txt"); FileUtils.writeStringToFile(file, "someContent"); String filePath = file.getAbsoluteFile().getAbsolutePath(); FileFixture fileFixture = buildFixture(filePath); fileFixture.addExtractorMatcher(ExtractorMatcher.create(new IsAnything())); fileFixture.populateExtractionResult(Arrays.<Object>asList("string1", "string2")); // WHEN ExtractionResult extractionResult = fileFixture.getExtractionResult(); // THEN assertThat(extractionResult.getEntities()).containsOnly("string1", "string2"); }
/** * MkContainer returns HTTP 500 if no answers match. * @throws Exception If something goes wrong inside */ @Test(expected = NoSuchElementException.class) public void returnsErrorIfNoMatches() throws Exception { try (final MkContainer container = new MkGrizzlyContainer()) { container.next( new MkAnswer.Simple("not supposed to match"), Matchers.not(new IsAnything<MkQuery>()) ).start(); new JdkRequest(container.home()) .through(VerboseWire.class) .fetch().as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_INTERNAL_ERROR); container.take(); } }
/** * LastModifiedCachingWire can cache GET requests. * @throws Exception If fails */ @Test public void cachesGetRequest() throws Exception { final Map<String, String> headers = Collections.singletonMap( HttpHeaders.LAST_MODIFIED, "Wed, 15 Nov 1995 04:58:08 GMT" ); final MkContainer container = new MkGrizzlyContainer() .next( new MkAnswer.Simple( HttpURLConnection.HTTP_OK, headers.entrySet(), LastModifiedCachingWireTest.BODY.getBytes() ) ) .next( new MkAnswer.Simple(HttpURLConnection.HTTP_NOT_MODIFIED), new IsAnything<MkQuery>(), Tv.TEN ).start(); try { final Request req = new JdkRequest(container.home()) .through(LastModifiedCachingWire.class); for (int idx = 0; idx < Tv.TEN; ++idx) { req.fetch().as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .assertBody( Matchers.equalTo(LastModifiedCachingWireTest.BODY) ); } MatcherAssert.assertThat( container.queries(), Matchers.equalTo(Tv.TEN) ); } finally { container.stop(); } }
@Test public void functionMatcherOnUpperAndLowerFunctions() { sqlWithFunctions = "SELECT UPPER(foo), LOWER(bar) FROM MyTable"; statementWithFunctions = getParseTree(sqlWithFunctions); assertThat(statementWithFunctions, hasInQuery(result(upper(new IsAnything<QueryTreeNode>())))); assertThat(statementWithFunctions, hasInQuery(result(upper(column("foo"))))); assertThat(statementWithFunctions, hasInQuery(result(lower(column("bar"))))); //no such column assertThat(statementWithFunctions, not(hasInQuery(where(lower(column("barry")))))); }
@Test public void getExtractionResultReturnCorrectEntities() { // GIVEN SpringFixture springFixture = new SpringFixture("context", Arrays.<Class<?>>asList(String.class, Integer.class)); springFixture.addExtractorMatcher(ExtractorMatcher.create(new IsAnything())); springFixture.populateExtractionResult(Arrays.<Object>asList("string1", "string2")); // WHEN ExtractionResult extractionResult = springFixture.getExtractionResult(); // THEN assertThat(extractionResult.getEntities()).containsOnly("string1", "string2"); }
@Override public Matcher<View> getConstraints() { return new IsAnything<>(); }
@Override public MkContainer next(final MkAnswer answer) { return this.next(answer, new IsAnything<MkQuery>()); }
public static <T> Matcher<Async<T>> succeeded() { return result(new IsAnything<T>()); }
public static Matcher<Async<?>> failed() { return failure(new IsAnything<>()); }
/** * Syntactic sugar! This version only checks the content of a conditional in a WHEN clause, such as * CASE WHEN (my_id=35) * * For example, assertThat(query, cased(column("my_id").equals(35))); */ @Factory public static Matcher<QueryTreeNode> cased(Matcher<QueryTreeNode> whenMatcher) { return new CaseStatementMatcher(whenMatcher, new IsAnything<QueryTreeNode>(), new IsAnything<QueryTreeNode>()); }
/** * Syntactic sugar for the case where you are merely trying to match a casted value. * * For example, assertThat(query, hasInQuery(castedValue(literal("11:23:45")))); */ @Factory public static Matcher<QueryTreeNode> castedValue(Matcher<QueryTreeNode> subMatcher) { return new CastMatcher(new IsAnything<TypeId>(), subMatcher); }
/** * Syntactic sugar! * * For example, assertThat(query, hasInQuery(like(column("full_name"), "%Doe%"))); */ @Factory public static Matcher<QueryTreeNode> like(Matcher<QueryTreeNode> receiverMatcher, Matcher<QueryTreeNode> leftMatcher) { return new TernaryMatcher(receiverMatcher, "LIKE", leftMatcher, new IsAnything<QueryTreeNode>()); }
/** * Syntactic sugar for the presence of a GROUP BY clause. * * For example, assertThat(query, groupBy()); */ @Factory public static Matcher<QueryTreeNode> groupBy() { return new UnderNodeMatcher<QueryTreeNode>(new IsAnything<QueryTreeNode>(), GroupByList.class, "a group by "); }
/** * Syntactic sugar for the presence of an ORDER BY clause. * * For example, assertThat(query, groupBy()); */ @Factory public static Matcher<QueryTreeNode> orderBy() { return new UnderNodeMatcher<QueryTreeNode>(new IsAnything<QueryTreeNode>(), OrderByList.class, "an order by "); }
/** * Syntactic sugar! * * For example, assertThat(query, hasInQuery(fromSubquery("my_inner"))); */ @Factory public static FromSubqueryMatcher fromSubquery(String tableName) { return new FromSubqueryMatcher(new IsAnything<QueryTreeNode>(), tableName); }