Package com.gatehill.apibms.core.model

Examples of com.gatehill.apibms.core.model.MockDefinition


        // deserialize to model
        final AstBlueprint blueprint = (AstBlueprint) astParserService.fromAst(astFile, astFormat);

        // convert to mock
        final MockDefinition mock = toMockDefinition(blueprint);

        // set blueprint for documentation
        mock.setBlueprintFile(blueprintFile);

        return mock;
    }
View Full Code Here


     * @return
     */
    protected MockDefinition toMockDefinition(AstBlueprint astBlueprint) {
        LOGGER.info("Converting blueprint '{}' model to mock definition", astBlueprint.getName());

        final MockDefinition definition = new MockDefinition();
        definition.setName(astBlueprint.getName());

        for (AstResourceGroup astResourceGroup : astBlueprint.getResourceGroups()) {
            for (AstResource astResource : astResourceGroup.getResources()) {
                // add resource headers
                final Map<String, String> parentHeaders = new HashMap<>();
                parentHeaders.putAll(asMap(astResource.getHeaders()));

                // build resource
                final ResourceDefinition resource = new ResourceDefinition(astResource.getName());
                resource.setName(astResource.getName());
                resource.setUrl(astResource.getUriTemplate());

                for (AstAction astAction : astResource.getActions()) {
                    // add action headers
                    parentHeaders.putAll(asMap(astAction.getHeaders()));

                    for (AstTransactionExample example : astAction.getExamples()) {
                        buildRequests(parentHeaders, resource, astAction, example);
                        buildResponses(parentHeaders, resource, example);
                    }
                }

                definition.addEndpoint(resource);
            }
        }

        LOGGER.debug("AST blueprint parsed: {}", definition);
        return definition;
View Full Code Here

    /**
     * {@inheritDoc}
     */
    @Override
    public ExecutionInstance execute(File blueprintFile, MockFactory.BlueprintFormat format, String host, int port) throws ServiceException {
        final MockDefinition definition = mockFactory.createMock(blueprintFile, format);
        final MockServer server = serverService.start(definition, host, port);

        return new ExecutionInstance(server.getHost(), server.getPort(),
                definition.getEndpoints().toArray(new ResourceDefinition[definition.getEndpoints().size()]));
    }
View Full Code Here

        // mock behaviour
        when(astParserService.fromAst(any(File.class), any(AstFormat.class)))
                .thenReturn(ast);

        // call
        final MockDefinition actual = factory.createMock(blueprint, MockFactory.BlueprintFormat.AST_JSON);

        // assert
        Assert.assertNotNull(actual);
        Assert.assertEquals("My API", actual.getName());
        Assert.assertEquals(1, actual.getEndpoints().size());

        final ResourceDefinition endpoint1 = actual.getEndpoints().get(0);
        Assert.assertNotNull(endpoint1);
        Assert.assertEquals("/message", endpoint1.getUrl());

        Assert.assertEquals(1, endpoint1.getRequests().size());
        final RequestDefinition request = endpoint1.getRequests().get(0);
View Full Code Here

    @Test
    public void testExecute() throws Exception {
        // test data
        final File jsonAst = new File(ExecutionServiceTest.class.getResource("/api1.json").getPath());

        final MockDefinition mock = new MockDefinition();
        mock.getEndpoints().add(new ResourceDefinition());

        final MockServer server = new MockServer(ServerService.HOST_LOCALHOST, PORT);

        // mock behaviour
        when(mockFactory.createMock(any(File.class), any(MockFactory.BlueprintFormat.class)))
View Full Code Here

        // test data
        final File blueprint = new File(MarkdownMockFactoryTest.class.getResource("/api1.md").getPath());

        // call
        final MockDefinition actual = factory.createMock(blueprint, MockFactory.BlueprintFormat.MARKDOWN);

        // assert
        Assert.assertNotNull(actual);
        Assert.assertEquals("My API", actual.getName());
        Assert.assertEquals(1, actual.getEndpoints().size());

        final ResourceDefinition endpoint1 = actual.getEndpoints().get(0);
        Assert.assertNotNull(endpoint1);
        Assert.assertEquals("/message", endpoint1.getUrl());

        Assert.assertEquals(1, endpoint1.getRequests().size());
        final RequestDefinition request = endpoint1.getRequests().get(0);
View Full Code Here

        if (!BlueprintFormat.MARKDOWN.equals(format)) {
            throw new UnsupportedOperationException("This implementation only supports Markdown parsing");
        }

        LOGGER.info("Creating {} mock from file {}", format, blueprintFile);
        final MockDefinition mock = createMock(parsingService.toXhtmlDocument(parsingService.parseMarkdownFile(blueprintFile)));

        // set blueprint for documentation
        mock.setBlueprintFile(blueprintFile);

        return mock;
    }
View Full Code Here

     * @return
     * @throws ServiceException
     */
    private MockDefinition createMock(Document doc) throws ServiceException {
        try {
            final MockDefinition definition = new MockDefinition();

            // get body from document
            final XPath xPath = XPathFactory.newInstance().newXPath();

            final NodeList nodes = (NodeList) xPath.evaluate("/html/body",
                    doc.getDocumentElement(), XPathConstants.NODESET);

            if (nodes.getLength() != 1) {
                throw new ServiceException("XHTML should be exactly 1 body element");
            }

            final Node body = nodes.item(0);

            // build endpoint definitions
            if (body.hasChildNodes()) {
                ResourceDefinition currentEndpoint = null;

                for (int i = 0; i < body.getChildNodes().getLength(); i++) {
                    final Node child = body.getChildNodes().item(i);
                    LOGGER.debug("Found {}: {}", child.getNodeName(), child.getTextContent());

                    if (child.getNodeType() == Node.ELEMENT_NODE) {

                        // <h1>My API</h1>
                        if (isApiTitle(child.getNodeName())) {
                            definition.setName(child.getTextContent());
                            LOGGER.info("Found API: {}", definition.getName());
                        }

                        // <h2>GET /message</h2>
                        if (isEndpointHTTP(child.getNodeName())) {
                            // start of a new endpoint
                            currentEndpoint = new ResourceDefinition();
                            definition.addEndpoint(currentEndpoint);

                            LOGGER.info("Found endpoint: {}", currentEndpoint.getName());

                            final String[] http = child.getTextContent().split(" ");
                            currentEndpoint.setUrl(http[1]);
View Full Code Here

TOP

Related Classes of com.gatehill.apibms.core.model.MockDefinition

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.