private Optional<RestMockResponse> getDefaultMockResponse(final RestMethod restMethod, final List<RestMockResponse> mockResponses){ final String defaultResponseId = restMethod.getDefaultMockResponseId(); if(defaultResponseId != null && !defaultResponseId.isEmpty()){ LOGGER.info("Use the default response"); for (RestMockResponse tmpMockResponse : mockResponses) { if(defaultResponseId.equals(tmpMockResponse.getId())){ return Optional.of(tmpMockResponse); } } LOGGER.error("Unable to find the default response"); } return Optional.empty(); }
/** * The process message is responsible for processing an incoming serviceTask and generate * a response based on the incoming serviceTask input * @param serviceTask The serviceTask that will be processed by the service * @return A result based on the processed incoming serviceTask * @see ServiceTask * @see ServiceResult */ @Override public ServiceResult<DeleteRestMockResponsesOutput> process(final ServiceTask<DeleteRestMockResponsesInput> serviceTask) { final DeleteRestMockResponsesInput input = serviceTask.getInput(); for(RestMockResponse restMockResponse : input.getRestMockResponses()){ this.deleteMockResponse(restMockResponse.getId()); } return createServiceResult(DeleteRestMockResponsesOutput.builder().build()); } }
@Test public void testProcess(){ final String projectId = "ProjectId"; final String applicationId = "ApplicationId"; final RestResource resource = RestResourceGenerator.generateRestResource(); final RestMethod method = RestMethodGenerator.generateRestMethod(); final RestMockResponse mockResponse = RestMockResponseGenerator.generateRestMockResponse(); Mockito.when(resourceRepository.findWithApplicationId(applicationId)).thenReturn(Arrays.asList(resource)); Mockito.when(methodRepository.findWithResourceId(resource.getId())).thenReturn(Arrays.asList(method)); Mockito.when(mockResponseRepository.findWithMethodId(method.getId())).thenReturn(Arrays.asList(mockResponse)); final DeleteRestApplicationInput input = DeleteRestApplicationInput.builder() .restProjectId(projectId) .restApplicationId(applicationId) .build(); final ServiceTask<DeleteRestApplicationInput> serviceTask = new ServiceTask<DeleteRestApplicationInput>(input); final ServiceResult<DeleteRestApplicationOutput> serviceResult = service.process(serviceTask); Mockito.verify(applicationRepository, Mockito.times(1)).delete(applicationId); Mockito.verify(resourceRepository, Mockito.times(1)).delete(resource.getId()); Mockito.verify(methodRepository, Mockito.times(1)).delete(method.getId()); Mockito.verify(mockResponseRepository, Mockito.times(1)).delete(mockResponse.getId()); Mockito.verify(resourceRepository, Mockito.times(1)).findWithApplicationId(applicationId); Mockito.verify(methodRepository, Mockito.times(1)).findWithResourceId(resource.getId()); Mockito.verify(mockResponseRepository, Mockito.times(1)).findWithMethodId(method.getId()); } }
@Test public void testProcess(){ final String projectId = "ProjectId"; final String applicationId = "ApplicationId"; final String resourceId = "ResourceId"; final RestMethod method = RestMethodGenerator.generateRestMethod(); final RestMockResponse mockResponse = RestMockResponseGenerator.generateRestMockResponse(); Mockito.when(methodRepository.findWithResourceId(resourceId)).thenReturn(Arrays.asList(method)); Mockito.when(mockResponseRepository.findWithMethodId(method.getId())).thenReturn(Arrays.asList(mockResponse)); final DeleteRestResourceInput input = DeleteRestResourceInput.builder() .restProjectId(projectId) .restApplicationId(applicationId) .restResourceId(resourceId) .build(); final ServiceTask<DeleteRestResourceInput> serviceTask = new ServiceTask<DeleteRestResourceInput>(input); final ServiceResult<DeleteRestResourceOutput> serviceResult = service.process(serviceTask); Mockito.verify(resourceRepository, Mockito.times(1)).delete(resourceId); Mockito.verify(methodRepository, Mockito.times(1)).delete(method.getId()); Mockito.verify(mockResponseRepository, Mockito.times(1)).delete(mockResponse.getId()); Mockito.verify(methodRepository, Mockito.times(1)).findWithResourceId(resourceId); Mockito.verify(mockResponseRepository, Mockito.times(1)).findWithMethodId(method.getId()); } }
@Test public void testProcess(){ final String projectId = "ProjectId"; final String applicationId = "ApplicationId"; final String resourceId = "ResourceId"; final String methodId = "MethodId"; final RestMockResponse mockResponse = RestMockResponseGenerator.generateRestMockResponse(); Mockito.when(mockResponseRepository.findWithMethodId(methodId)).thenReturn(Arrays.asList(mockResponse)); final DeleteRestMethodInput input = DeleteRestMethodInput.builder() .restProjectId(projectId) .restApplicationId(applicationId) .restResourceId(resourceId) .restMethodId(methodId) .build(); final ServiceTask<DeleteRestMethodInput> serviceTask = new ServiceTask<DeleteRestMethodInput>(input); final ServiceResult<DeleteRestMethodOutput> serviceResult = service.process(serviceTask); Mockito.verify(methodRepository, Mockito.times(1)).delete(methodId); Mockito.verify(mockResponseRepository, Mockito.times(1)).delete(mockResponse.getId()); Mockito.verify(mockResponseRepository, Mockito.times(1)).findWithMethodId(methodId); } }
Mockito.verify(resourceRepository, Mockito.times(1)).delete(resource.getId()); Mockito.verify(methodRepository, Mockito.times(1)).delete(method.getId()); Mockito.verify(mockResponseRepository, Mockito.times(1)).delete(mockResponse.getId());
+ SLASH + RESOURCE + SLASH + resourceId + SLASH + METHOD + SLASH + mockResponse.getMethodId() + SLASH + RESPONSE + SLASH + mockResponse.getId()); searchResult.setDescription(REST_TYPE + COMMA + responseType); searchResults.add(searchResult);
@Test public void testProcess(){ final String projectId = "ProjectId"; final String applicationId = "ApplicationId"; final String resourceId = "ResourceId"; final String methodId = "MethodId"; final RestMockResponse mockResponse = RestMockResponseGenerator.generateRestMockResponse(); final ReadRestMockResponseInput input = ReadRestMockResponseInput.builder() .restProjectId(projectId) .restApplicationId(applicationId) .restResourceId(resourceId) .restMethodId(methodId) .restMockResponse(mockResponse.getId()) .build(); final ServiceTask<ReadRestMockResponseInput> serviceTask = new ServiceTask<ReadRestMockResponseInput>(input); Mockito.when(mockResponseRepository.findOne(mockResponse.getId())).thenReturn(mockResponse); final ServiceResult<ReadRestMockResponseOutput> result = service.process(serviceTask); Mockito.verify(mockResponseRepository, Mockito.times(1)).findOne(mockResponse.getId()); Assert.assertNotNull(result.getOutput()); Assert.assertEquals(mockResponse, result.getOutput().getRestMockResponse()); }
if(mockResponse.getId().equals(restMethod.getDefaultMockResponseId())){ restMethod.setDefaultResponseName(mockResponse.getName()); defaultQueryMockResponseId = true;
@Test public void testDeleteMockResponseConfirm() throws Exception { final RestProject restProject = RestProjectGenerator.generateRestProject(); final RestApplication restApplication = RestApplicationGenerator.generateRestApplication(); final RestResource restResource = RestResourceGenerator.generateRestResource(); final RestMethod restMethod = RestMethodGenerator.generateRestMethod(); final RestMockResponse restMockResponse = RestMockResponseGenerator.generateRestMockResponse(); when(serviceProcessor.process(any(DeleteRestMockResponseInput.class))).thenReturn(DeleteRestMockResponsesOutput.builder().build()); final MockHttpServletRequestBuilder message = MockMvcRequestBuilders.get(SERVICE_URL + PROJECT + SLASH + restProject.getId() + SLASH + APPLICATION + SLASH + restApplication.getId() + SLASH + RESOURCE + SLASH + restResource.getId() + SLASH + METHOD + SLASH + restMethod.getId() + SLASH + RESPONSE + SLASH + restMockResponse.getId() + SLASH + DELETE + SLASH + CONFIRM); mockMvc.perform(message) .andExpect(MockMvcResultMatchers.status().isFound()) .andExpect(MockMvcResultMatchers.model().size(0)); }
@Test public void testUpdateMockResponse() throws Exception { final RestProject restProject = RestProjectGenerator.generateRestProject(); final RestApplication restApplication = RestApplicationGenerator.generateRestApplication(); final RestResource restResource = RestResourceGenerator.generateRestResource(); final RestMethod restMethod = RestMethodGenerator.generateRestMethod(); final RestMockResponse restMockResponse = RestMockResponseGenerator.generateRestMockResponse(); when(serviceProcessor.process(any(UpdateRestMockResponseInput.class))).thenReturn(UpdateRestMockResponseOutput.builder() .updatedRestMockResponse(restMockResponse) .build()); final MockHttpServletRequestBuilder message = MockMvcRequestBuilders.post(SERVICE_URL + PROJECT + SLASH + restProject.getId() + SLASH + APPLICATION + SLASH + restApplication.getId() + SLASH + RESOURCE + SLASH + restResource.getId() + SLASH + METHOD + SLASH + restMethod.getId() + SLASH + RESPONSE + SLASH + restMockResponse.getId() + SLASH + UPDATE); mockMvc.perform(message) .andExpect(MockMvcResultMatchers.status().isFound()) .andExpect(MockMvcResultMatchers.model().size(1)); }
Assert.assertTrue(response200Xml.isUsingExpressions()); Assert.assertTrue(response200Xml.getContentEncodings().isEmpty()); Assert.assertNull(response200Xml.getId()); Assert.assertNull(response200Xml.getMethodId()); Assert.assertEquals(1, response200Xml.getHttpHeaders().size()); Assert.assertTrue(response200Json.isUsingExpressions()); Assert.assertTrue(response200Json.getContentEncodings().isEmpty()); Assert.assertNull(response200Json.getId()); Assert.assertNull(response200Json.getMethodId()); Assert.assertEquals(1, response200Json.getHttpHeaders().size()); Assert.assertTrue(invalidMockResponse.isUsingExpressions()); Assert.assertTrue(invalidMockResponse.getContentEncodings().isEmpty()); Assert.assertNull(invalidMockResponse.getId()); Assert.assertNull(invalidMockResponse.getMethodId()); Assert.assertEquals(0, invalidMockResponse.getHttpHeaders().size()); Assert.assertTrue(notFoundResponse.isUsingExpressions()); Assert.assertTrue(notFoundResponse.getContentEncodings().isEmpty()); Assert.assertNull(notFoundResponse.getId()); Assert.assertNull(notFoundResponse.getMethodId()); Assert.assertEquals(0, notFoundResponse.getHttpHeaders().size()); Assert.assertTrue(invalidMockResponse.isUsingExpressions()); Assert.assertTrue(invalidMockResponse.getContentEncodings().isEmpty()); Assert.assertNull(invalidMockResponse.getId()); Assert.assertNull(invalidMockResponse.getMethodId()); Assert.assertEquals(0, invalidMockResponse.getHttpHeaders().size());
Assert.assertTrue(restMockResponse.isUsingExpressions()); Assert.assertNull(restMockResponse.getId()); Assert.assertNull(restMockResponse.getMethodId()); Assert.assertNull(restMockResponse.getBody());
SLASH + restProject.getId() + SLASH + APPLICATION + SLASH + restApplication.getId() + SLASH + RESOURCE + SLASH + restResource.getId() + SLASH + METHOD + SLASH + restMethod.getId() + SLASH + RESPONSE + SLASH + restMockResponse.getId()); mockMvc.perform(message) .andExpect(MockMvcResultMatchers.status().isOk())
@Test public void testDeleteMockResponse() throws Exception { final RestProject restProject = RestProjectGenerator.generateRestProject(); final RestApplication restApplication = RestApplicationGenerator.generateRestApplication(); final RestResource restResource = RestResourceGenerator.generateRestResource(); final RestMethod restMethod = RestMethodGenerator.generateRestMethod(); final RestMockResponse restMockResponse = RestMockResponseGenerator.generateRestMockResponse(); when(serviceProcessor.process(any(ReadRestMockResponseInput.class))).thenReturn(ReadRestMockResponseOutput.builder() .restMockResponse(restMockResponse) .build()); final MockHttpServletRequestBuilder message = MockMvcRequestBuilders.get(SERVICE_URL + PROJECT + SLASH + restProject.getId() + SLASH + APPLICATION + SLASH + restApplication.getId() + SLASH + RESOURCE + SLASH + restResource.getId() + SLASH + METHOD + SLASH + restMethod.getId() + SLASH + RESPONSE + SLASH + restMockResponse.getId() + SLASH + DELETE); mockMvc.perform(message) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.model().size(5 + GLOBAL_VIEW_MODEL_COUNT)) .andExpect(MockMvcResultMatchers.forwardedUrl(INDEX)) .andExpect(MockMvcResultMatchers.model().attribute(PARTIAL, PAGE)) .andExpect(MockMvcResultMatchers.model().attribute(REST_PROJECT_ID, restProject.getId())) .andExpect(MockMvcResultMatchers.model().attribute(REST_APPLICATION_ID, restApplication.getId())) .andExpect(MockMvcResultMatchers.model().attribute(REST_RESOURCE_ID, restResource.getId())) .andExpect(MockMvcResultMatchers.model().attribute(REST_METHOD_ID, restMethod.getId())) .andExpect(MockMvcResultMatchers.model().attribute(REST_MOCK_RESPONSE, restMockResponse)); }