restMethod.getMockResponses().add(restMockResponse);
public static RestProject generateFullRestProject(){ final RestProject project = new RestProject(); project.setId("REST PROJECT"); project.setName("Project name"); project.setDescription("Project description"); project.setCreated(new Date()); project.setUpdated(new Date()); project.setApplications(new ArrayList<RestApplication>()); for(int applicationIndex = 0; applicationIndex < 3; applicationIndex++){ final RestApplication restApplication = RestApplicationGenerator.generateRestApplication(); restApplication.setResources(new ArrayList<RestResource>()); project.getApplications().add(restApplication); for(int resourceIndex = 0; resourceIndex < 3; resourceIndex++){ final RestResource restResource = RestResourceGenerator.generateRestResource(); restResource.setMethods(new ArrayList<RestMethod>()); restApplication.getResources().add(restResource); for(int methodIndex = 0; methodIndex < 3; methodIndex++){ final RestMethod restMethod = RestMethodGenerator.generateRestMethod(); restMethod.setMockResponses(new ArrayList<RestMockResponse>()); restResource.getMethods().add(restMethod); for(int responseIndex = 0; responseIndex < 3; responseIndex++){ final RestMockResponse restMockResponse = RestMockResponseGenerator.generateRestMockResponse(); restMethod.getMockResponses().add(restMockResponse); } } } } return project; }
if(!operation.getResponses().isEmpty()){ Collection<RestMockResponse> mockResponses = generateResponse(operation.getResponses(), definitions, operation.getProduces()); restMethod.getMockResponses().addAll(mockResponses); } else { RestMockResponse generatedResponse = generateResponse(); restMethod.getMockResponses().add(generatedResponse);
restMethod.getMockResponses().addAll(mockResponses);
restMethod.getMockResponses().addAll(mockResponses);
for (RestMockResponse mockResponse : method.getMockResponses()) { if(this.mockResponseRepository.exists(mockResponse.getId())){ throw new IllegalArgumentException("A mock response with the following key already exists: " + mockResponse.getId());
for(RestMockResponse mockResponse : restMethod.getMockResponses()){ if(mockResponse.getStatus().equals(RestMockResponseStatus.ENABLED)){ enabledMockResponses.add(mockResponse);
@Test public void testMockedXpathMatch(){ // Input final HttpServletRequest httpServletRequest = getMockedHttpServletRequest(XML_REQUEST_BODY); final HttpServletResponse httpServletResponse = getHttpServletResponse(); final RestXPathExpression xPathExpression = new RestXPathExpression(); xPathExpression.setExpression("//request/variable[text()='Value 1']"); final RestMethod restMethod = getMockedRestMethod(); restMethod.getMockResponses().get(0).getXpathExpressions().add(xPathExpression); restMethod.setResponseStrategy(RestResponseStrategy.XPATH); final IdentifyRestMethodOutput identifyRestMethodOutput = IdentifyRestMethodOutput.builder() .restProjectId(PROJECT_ID) .restApplicationId(APPLICATION_ID) .restResourceId(RESOURCE_ID) .restMethodId(METHOD_ID) .restMethod(restMethod) .pathParameters(PATH_PARAMETERS) .build(); when(serviceProcessor.process(any(IdentifyRestMethodInput.class))).thenReturn(identifyRestMethodOutput); when(httpServletRequest.getRequestURI()).thenReturn(CONTEXT + SLASH + MOCK + SLASH + REST + SLASH + PROJECT + SLASH + PROJECT_ID + SLASH + APPLICATION + SLASH + APPLICATION_ID + "/method/test"); final ResponseEntity responseEntity = restServiceController.postMethod(PROJECT_ID, APPLICATION_ID, httpServletRequest, httpServletResponse); Assert.assertEquals(XML_RESPONSE_BODY, responseEntity.getBody()); Assert.assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); Assert.assertEquals(true, responseEntity.getHeaders().containsKey(CONTENT_TYPE_HEADER)); Assert.assertEquals(true, responseEntity.getHeaders().containsKey(ACCEPT_HEADER)); Assert.assertEquals(APPLICATION_XML, responseEntity.getHeaders().get(CONTENT_TYPE_HEADER).get(0)); Assert.assertEquals(APPLICATION_XML, responseEntity.getHeaders().get(ACCEPT_HEADER).get(0)); }
@Test public void testMockedJsonPathMatch(){ // Input final HttpServletRequest httpServletRequest = getMockedHttpServletRequest(JSON_REQUEST_BODY); final HttpServletResponse httpServletResponse = getHttpServletResponse(); final RestJsonPathExpression restJsonPathExpression = new RestJsonPathExpression(); restJsonPathExpression.setExpression("$.request[?(@.variable == 'Value 1')]"); final RestMethod restMethod = getMockedRestMethod(); restMethod.getMockResponses().get(0).getJsonPathExpressions().add(restJsonPathExpression); restMethod.setResponseStrategy(RestResponseStrategy.JSON_PATH); final IdentifyRestMethodOutput identifyRestMethodOutput = IdentifyRestMethodOutput.builder() .restProjectId(PROJECT_ID) .restApplicationId(APPLICATION_ID) .restResourceId(RESOURCE_ID) .restMethodId(METHOD_ID) .restMethod(restMethod) .pathParameters(PATH_PARAMETERS) .build(); when(serviceProcessor.process(any(IdentifyRestMethodInput.class))).thenReturn(identifyRestMethodOutput); when(httpServletRequest.getRequestURI()).thenReturn(CONTEXT + SLASH + MOCK + SLASH + REST + SLASH + PROJECT + SLASH + PROJECT_ID + SLASH + APPLICATION + SLASH + APPLICATION_ID + "/method/test"); final ResponseEntity responseEntity = restServiceController.postMethod(PROJECT_ID, APPLICATION_ID, httpServletRequest, httpServletResponse); Assert.assertEquals(XML_RESPONSE_BODY, responseEntity.getBody()); Assert.assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); Assert.assertEquals(true, responseEntity.getHeaders().containsKey(CONTENT_TYPE_HEADER)); Assert.assertEquals(true, responseEntity.getHeaders().containsKey(ACCEPT_HEADER)); Assert.assertEquals(APPLICATION_XML, responseEntity.getHeaders().get(CONTENT_TYPE_HEADER).get(0)); Assert.assertEquals(APPLICATION_XML, responseEntity.getHeaders().get(ACCEPT_HEADER).get(0)); }
for(RestMockResponse mockResponse : method.getMockResponses()){ mockResponse.setMethodId(savedMethod.getId()); this.mockResponseRepository.save(mockResponse);
@Test public void testProcessWithoutStatus(){ final RestMethod restMethod = RestMethodGenerator.generateRestMethod(); restMethod.setStatus(null); Mockito.when(methodRepository.save(Mockito.any(RestMethod.class))).thenReturn(restMethod); final CreateRestMethodInput input = CreateRestMethodInput.builder() .projectId(restProject.getId()) .applicationId(restApplication.getId()) .resourceId(restResource.getId()) .method(restMethod) .build(); final ServiceTask<CreateRestMethodInput> serviceTask = new ServiceTask<CreateRestMethodInput>(input); final ServiceResult<CreateRestMethodOutput> serviceResult = service.process(serviceTask); final CreateRestMethodOutput createRestApplicationOutput = serviceResult.getOutput(); final RestMethod returnedRestMethod = createRestApplicationOutput.getCreatedRestMethod(); Assert.assertEquals(restMethod.getName(), returnedRestMethod.getName()); Assert.assertEquals(restMethod.getHttpMethod(), returnedRestMethod.getHttpMethod()); Assert.assertEquals(restMethod.getDefaultBody(), returnedRestMethod.getDefaultBody()); Assert.assertEquals(restMethod.getForwardedEndpoint(), returnedRestMethod.getForwardedEndpoint()); Assert.assertEquals(restMethod.getStatus(), RestMethodStatus.MOCKED); Assert.assertEquals(restMethod.getMockResponses(), returnedRestMethod.getMockResponses()); Assert.assertEquals(restMethod.getResponseStrategy(), returnedRestMethod.getResponseStrategy()); }
@Test public void testProcess(){ final RestMethod restMethod = RestMethodGenerator.generateRestMethod(); Mockito.when(methodRepository.save(Mockito.any(RestMethod.class))).thenReturn(restMethod); final CreateRestMethodInput input = CreateRestMethodInput.builder() .projectId(restProject.getId()) .applicationId(restApplication.getId()) .resourceId(restResource.getId()) .method(restMethod) .build(); final ServiceTask<CreateRestMethodInput> serviceTask = new ServiceTask<CreateRestMethodInput>(input); final ServiceResult<CreateRestMethodOutput> serviceResult = service.process(serviceTask); final CreateRestMethodOutput createRestApplicationOutput = serviceResult.getOutput(); final RestMethod returnedRestMethod = createRestApplicationOutput.getCreatedRestMethod(); Assert.assertEquals(restMethod.getName(), returnedRestMethod.getName()); Assert.assertEquals(restMethod.getHttpMethod(), returnedRestMethod.getHttpMethod()); Assert.assertEquals(restMethod.getDefaultBody(), returnedRestMethod.getDefaultBody()); Assert.assertEquals(restMethod.getForwardedEndpoint(), returnedRestMethod.getForwardedEndpoint()); Assert.assertEquals(restMethod.getStatus(), returnedRestMethod.getStatus()); Assert.assertEquals(restMethod.getMockResponses(), returnedRestMethod.getMockResponses()); Assert.assertEquals(restMethod.getResponseStrategy(), returnedRestMethod.getResponseStrategy()); }
@Test public void testProcessWithoutResponseStrategy(){ final RestMethod restMethod = RestMethodGenerator.generateRestMethod(); restMethod.setResponseStrategy(null); Mockito.when(methodRepository.save(Mockito.any(RestMethod.class))).thenReturn(restMethod); final CreateRestMethodInput input = CreateRestMethodInput.builder() .projectId(restProject.getId()) .applicationId(restApplication.getId()) .resourceId(restResource.getId()) .method(restMethod) .build(); final ServiceTask<CreateRestMethodInput> serviceTask = new ServiceTask<CreateRestMethodInput>(input); final ServiceResult<CreateRestMethodOutput> serviceResult = service.process(serviceTask); final CreateRestMethodOutput createRestApplicationOutput = serviceResult.getOutput(); final RestMethod returnedRestMethod = createRestApplicationOutput.getCreatedRestMethod(); Assert.assertEquals(restMethod.getName(), returnedRestMethod.getName()); Assert.assertEquals(restMethod.getHttpMethod(), returnedRestMethod.getHttpMethod()); Assert.assertEquals(restMethod.getDefaultBody(), returnedRestMethod.getDefaultBody()); Assert.assertEquals(restMethod.getForwardedEndpoint(), returnedRestMethod.getForwardedEndpoint()); Assert.assertEquals(restMethod.getStatus(), returnedRestMethod.getStatus()); Assert.assertEquals(restMethod.getMockResponses(), returnedRestMethod.getMockResponses()); Assert.assertEquals(restMethod.getResponseStrategy(), RestResponseStrategy.RANDOM); }
Assert.assertEquals(4, getAllMockServicesMethod.getMockResponses().size()); RestMockResponse response200Xml = getAllMockServicesMethod.getMockResponses().stream() .filter(method -> method.getName().equals("successful operation (application/xml)")) .findFirst() RestMockResponse response200Json = getAllMockServicesMethod.getMockResponses().stream() .filter(method -> method.getName().equals("successful operation (application/json)")) .findFirst() RestMockResponse invalidMockResponse = getAllMockServicesMethod.getMockResponses().stream() .filter(method -> method.getName().equals("Invalid mock id supplied")) .findFirst() RestMockResponse notFoundResponse = getAllMockServicesMethod.getMockResponses().stream() .filter(method -> method.getName().equals("Mock not found")) .findFirst() Assert.assertEquals(0, notFoundResponse.getHttpHeaders().size()); } else { Assert.assertEquals(0, getAllMockServicesMethod.getMockResponses().size()); Assert.assertEquals(1, createMockMethod.getMockResponses().size()); RestMockResponse invalidMockResponse = getAllMockServicesMethod.getMockResponses().stream() .filter(method -> method.getName().equals("Invalid mock id supplied")) .findFirst() Assert.assertEquals(0, createMockMethod.getMockResponses().size());
Assert.assertEquals(1, restMethod.getMockResponses().size()); RestMockResponse restMockResponse = restMethod.getMockResponses().get(0); Assert.assertEquals(AUTO_GENERATED_MOCK_RESPONSE_DEFAULT_NAME, restMockResponse.getName()); Assert.assertEquals(Integer.valueOf(DEFAULT_RESPONSE_CODE), restMockResponse.getHttpStatusCode()); Assert.assertEquals(0, restMethod.getMockResponses().size());
mockResponse.setContentEncodings(contentEncodings); mockResponse.setMethodId(restMethod.getId()); restMethod.getMockResponses().add(mockResponse);