Search in sources :

Example 1 with GenericResource

use of io.github.microcks.domain.GenericResource in project microcks by microcks.

the class DynamicMockRestController method updateResource.

@RequestMapping(value = "/{service}/{version}/{resource}/{resourceId}", method = RequestMethod.PUT, produces = "application/json")
public ResponseEntity<String> updateResource(@PathVariable("service") String serviceName, @PathVariable("version") String version, @PathVariable("resource") String resource, @PathVariable("resourceId") String resourceId, @RequestParam(value = "delay", required = false) Long delay, @RequestBody(required = true) String body, HttpServletRequest request) {
    log.debug("Update resource '{}:{}' for service '{}-{}'", resource, resourceId, serviceName, version);
    long startTime = System.currentTimeMillis();
    serviceName = sanitizeServiceName(serviceName);
    MockContext mockContext = getMockContext(serviceName, version, "PUT /" + resource + "/:id");
    if (mockContext != null) {
        // Get the requested generic resource.
        GenericResource genericResource = genericResourceRepository.findById(resourceId).orElse(null);
        if (genericResource != null) {
            Document document = null;
            try {
                // Try parsing body payload that should be json.
                document = Document.parse(body);
                document.remove(ID_FIELD);
                // Now update the generic resource payload.
                genericResource.setPayload(document);
                genericResourceRepository.save(genericResource);
            } catch (JsonParseException jpe) {
                // Return a 422 code : unprocessable entity.
                return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
            }
            // Wait if specified before returning.
            waitForDelay(startTime, delay, mockContext);
            // Return the updated resource as well as a 200 code.
            return new ResponseEntity<>(transformToResourceJSON(genericResource), HttpStatus.OK);
        } else {
            // Wait if specified before returning.
            waitForDelay(startTime, delay, mockContext);
            // Return a 404 code : not found.
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }
    // Return a 400 code : bad request.
    return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) GenericResource(io.github.microcks.domain.GenericResource) Document(org.bson.Document) JsonParseException(org.bson.json.JsonParseException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with GenericResource

use of io.github.microcks.domain.GenericResource in project microcks by microcks.

the class DynamicMockRestController method createResource.

@RequestMapping(value = "/{service}/{version}/{resource}", method = RequestMethod.POST, produces = "application/json")
public ResponseEntity<String> createResource(@PathVariable("service") String serviceName, @PathVariable("version") String version, @PathVariable("resource") String resource, @RequestParam(value = "delay", required = false) Long delay, @RequestBody(required = true) String body, HttpServletRequest request) {
    log.debug("Creating a new resource '{}' for service '{}-{}'", resource, serviceName, version);
    long startTime = System.currentTimeMillis();
    serviceName = sanitizeServiceName(serviceName);
    MockContext mockContext = getMockContext(serviceName, version, "POST /" + resource);
    if (mockContext != null) {
        Document document = null;
        GenericResource genericResource = null;
        try {
            // Try parsing body payload that should be json.
            document = Document.parse(body);
            // Now create a generic resource.
            genericResource = new GenericResource();
            genericResource.setServiceId(mockContext.service.getId());
            genericResource.setPayload(document);
            genericResource = genericResourceRepository.save(genericResource);
        } catch (JsonParseException jpe) {
            // Return a 422 code : unprocessable entity.
            return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
        }
        // Append id and wait if specified before returning.
        document.append(ID_FIELD, genericResource.getId());
        waitForDelay(startTime, delay, mockContext);
        return new ResponseEntity<>(document.toJson(), HttpStatus.CREATED);
    }
    // Return a 400 code : bad request.
    return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) GenericResource(io.github.microcks.domain.GenericResource) Document(org.bson.Document) JsonParseException(org.bson.json.JsonParseException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with GenericResource

use of io.github.microcks.domain.GenericResource in project microcks by microcks.

the class DynamicMockRestController method getResource.

@RequestMapping(value = "/{service}/{version}/{resource}/{resourceId}", method = RequestMethod.GET, produces = "application/json")
public ResponseEntity<String> getResource(@PathVariable("service") String serviceName, @PathVariable("version") String version, @PathVariable("resource") String resource, @PathVariable("resourceId") String resourceId, @RequestParam(value = "delay", required = false) Long delay) {
    log.debug("Get resource '{}:{}' for service '{}-{}'", resource, resourceId, serviceName, version);
    long startTime = System.currentTimeMillis();
    serviceName = sanitizeServiceName(serviceName);
    MockContext mockContext = getMockContext(serviceName, version, "GET /" + resource + "/:id");
    if (mockContext != null) {
        // Get the requested generic resource.
        GenericResource genericResource = genericResourceRepository.findById(resourceId).orElse(null);
        // Wait if specified before returning.
        waitForDelay(startTime, delay, mockContext);
        if (genericResource != null) {
            // Return the resource as well as a 200 code.
            return new ResponseEntity<>(transformToResourceJSON(genericResource), HttpStatus.OK);
        } else {
            // Return a 404 code : not found.
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }
    // Return a 400 code : bad request.
    return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) GenericResource(io.github.microcks.domain.GenericResource) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with GenericResource

use of io.github.microcks.domain.GenericResource in project microcks by microcks.

the class GenericResourceRepositoryTest method testCreateGenericResource.

@Test
public void testCreateGenericResource() {
    // Create a minimal service.
    Service service = new Service();
    service.setName("DynamicAPI");
    service.setVersion("1.0");
    serviceRepository.save(service);
    // Define a JSON payload to put into a GenericResource associated to service.
    String jsonString = "{ \"foo\": 1234, \"bar\": \"string value\" }";
    GenericResource resource = new GenericResource();
    resource.setServiceId(service.getId());
    resource.setPayload(Document.parse(jsonString));
    // Save to repository.
    GenericResource dynaResource = repository.save(resource);
    // Test some manipulations of the payload Document.
    Document payload = dynaResource.getPayload();
    payload.append("id", dynaResource.getId());
    assertTrue(1234 == payload.getInteger("foo"));
    assertEquals("string value", payload.getString("bar"));
    assertEquals(dynaResource.getId(), payload.getString("id"));
    // Adding more resources before querying.
    jsonString = "{ \"foo\": 1234, \"bar\": \"other value\" }";
    GenericResource resource1 = new GenericResource();
    resource1.setServiceId(service.getId());
    resource1.setPayload(Document.parse(jsonString));
    repository.save(resource1);
    jsonString = "{ \"foo\": 1235, \"bar\": \"other value\" }";
    GenericResource resource2 = new GenericResource();
    resource2.setServiceId(service.getId());
    resource2.setPayload(Document.parse(jsonString));
    repository.save(resource2);
    // Query by example using 1 field.
    List<GenericResource> dynaResources = repository.findByServiceIdAndJSONQuery(service.getId(), "{ \"foo\": 1234 }");
    assertEquals(2, dynaResources.size());
    for (GenericResource r : dynaResources) {
        assertTrue(resource.getId().equals(r.getId()) || resource1.getId().equals(r.getId()));
    }
    // Query by example using 1 other field value.
    dynaResources = repository.findByServiceIdAndJSONQuery(service.getId(), "{ \"foo\": 1235 }");
    assertEquals(1, dynaResources.size());
    assertEquals(resource2.getId(), dynaResources.get(0).getId());
    // Query by example using 2 fields.
    dynaResources = repository.findByServiceIdAndJSONQuery(service.getId(), "{ \"foo\": 1234, \"bar\": \"other value\"}");
    assertEquals(1, dynaResources.size());
    assertEquals(resource1.getId(), dynaResources.get(0).getId());
    // Query by example using 1 field with complex expression.
    dynaResources = repository.findByServiceIdAndJSONQuery(service.getId(), "{ \"foo\": {$gt: 1234, $lt: 1236} }");
    assertEquals(1, dynaResources.size());
    assertEquals(resource2.getId(), dynaResources.get(0).getId());
}
Also used : GenericResource(io.github.microcks.domain.GenericResource) Service(io.github.microcks.domain.Service) Document(org.bson.Document) Test(org.junit.Test)

Aggregations

GenericResource (io.github.microcks.domain.GenericResource)4 Document (org.bson.Document)3 ResponseEntity (org.springframework.http.ResponseEntity)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 JsonParseException (org.bson.json.JsonParseException)2 Service (io.github.microcks.domain.Service)1 Test (org.junit.Test)1