io.virtualan.controller.VirtualServiceController Maven / Gradle / Ivy
/*
* Copyright 2018 Virtualan Contributors (https://virtualan.io)
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package io.virtualan.controller;
import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.NoSuchMessageException;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.virtualan.VirtualServiceInfo;
import io.virtualan.VirtualServiceUtil;
import io.virtualan.api.ApiType;
import io.virtualan.core.model.VirtualServiceRequest;
import io.virtualan.core.model.VirtualServiceStatus;
import io.virtualan.requestbody.RequestBodyTypes;
import io.virtualan.service.VirtualService;
/**
* This is a entry point to to record mock data in the Virtualan.
*
* Virtualan-UI and Virtualan-OpenAPI would interact through this web services.
*
* @author Elan Thangamani
*
**/
@RestController("virtualServiceController")
public class VirtualServiceController {
@Autowired
private VirtualService virtualService;
@Autowired
private ObjectMapper objectMapper;
@Autowired
private ApiType apiType;
private ObjectMapper getObjectMapper() {
objectMapper.findAndRegisterModules();
return objectMapper.enable(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE,
DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES
// ,DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES
);
}
@Autowired
private MessageSource messageSource;
VirtualServiceInfo virtualServiceInfo;
public VirtualServiceInfo getVirtualServiceInfo() {
return virtualServiceInfo;
}
public void setVirtualServiceInfo(VirtualServiceInfo virtualServiceInfo) {
this.virtualServiceInfo = virtualServiceInfo;
}
Locale locale = LocaleContextHolder.getLocale();
@Autowired
private VirtualServiceUtil virtualServiceUtil;
@PostConstruct
public void init() throws ClassNotFoundException, JsonProcessingException,
InstantiationException, IllegalAccessException {
virtualServiceUtil.setVirtualServiceType(apiType.findApiType());
if (virtualServiceUtil.getVirtualServiceType() != null) {
virtualServiceInfo = virtualServiceUtil.getVirtualServiceInfo();
virtualServiceInfo.loadVirtualServices();
virtualServiceInfo.setResourceParent(virtualServiceInfo.loadMapper());
}
}
@RequestMapping(value = "/virtualservices/load", method = RequestMethod.GET)
public Map> listAllMockLoadRequest()
throws InstantiationException, IllegalAccessException, ClassNotFoundException,
JsonParseException, JsonMappingException, IOException {
return virtualServiceInfo != null ? virtualServiceInfo.loadVirtualServices()
: new HashMap<>();
}
@RequestMapping(value = "/virtualservices", method = RequestMethod.GET)
public ResponseEntity> listAllMockLoadRequests() {
List MockLoadRequests = virtualService.findAllMockRequests();
if (MockLoadRequests.isEmpty()) {
return new ResponseEntity>(HttpStatus.NO_CONTENT);
}
return new ResponseEntity>(MockLoadRequests, HttpStatus.OK);
}
@RequestMapping(value = "/virtualservices/{id}", method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity getMockLoadRequest(@PathVariable("id") long id) {
VirtualServiceRequest mockLoadRequest = virtualService.findById(id);
if (mockLoadRequest == null) {
return new ResponseEntity(HttpStatus.NOT_FOUND);
}
return new ResponseEntity(mockLoadRequest, HttpStatus.OK);
}
@RequestMapping(value = "/virtualservices", method = RequestMethod.POST)
public ResponseEntity createMockRequest(
@RequestBody VirtualServiceRequest virtualServiceRequest) {
try {
validateExpectedInput(virtualServiceRequest);
// find the operationId for the given Request. It required for the Automation test cases
findOperationIdForService(virtualServiceRequest);
ResponseEntity responseEntity = validateRequestBody(virtualServiceRequest);
if (responseEntity != null) {
return responseEntity;
}
responseEntity = validateResponseBody(virtualServiceRequest);
if (responseEntity != null) {
return responseEntity;
}
responseEntity = checkIfServiceDataAlreadyExists(virtualServiceRequest);
if (responseEntity != null) {
return responseEntity;
}
VirtualServiceRequest mockTransferObject =
virtualService.saveMockRequest(virtualServiceRequest);
mockTransferObject.setMockStatus(
new VirtualServiceStatus(messageSource.getMessage("VS_SUCCESS", null, locale)));
return new ResponseEntity<>(mockTransferObject, HttpStatus.CREATED);
} catch (Exception e) {
e.printStackTrace();
return new ResponseEntity(new VirtualServiceStatus(
messageSource.getMessage("VS_UNEXPECTED_ERROR", null, locale) + e.getMessage()),
HttpStatus.BAD_REQUEST);
}
}
private ResponseEntity checkIfServiceDataAlreadyExists(VirtualServiceRequest mockLoadRequest) {
if (virtualServiceUtil.isMockAlreadyExists(mockLoadRequest)) {
return new ResponseEntity(
new VirtualServiceStatus(
messageSource.getMessage("VS_DATA_ALREADY_EXISTS", null, locale)),
HttpStatus.BAD_REQUEST);
}
return null;
}
private ResponseEntity validateResponseBody(VirtualServiceRequest mockLoadRequest) {
if (!virtualServiceUtil.isMockResponseBodyValid(mockLoadRequest)) {
return new ResponseEntity(
new VirtualServiceStatus(
messageSource.getMessage("VS_RESPONSE_BODY_MISMATCH", null, locale)),
HttpStatus.BAD_REQUEST);
}
return null;
}
private ResponseEntity validateRequestBody(VirtualServiceRequest virtualServiceRequest) {
if (virtualServiceInfo != null) {
Class inputObjectType = virtualServiceInfo.getInputType(virtualServiceRequest);
if (inputObjectType == null && (virtualServiceRequest.getInput() == null
|| virtualServiceRequest.getInput().length() == 0)) {
return null;
} else if (virtualServiceRequest.getInput() != null
&& virtualServiceRequest.getInput().length() > 0 && inputObjectType != null) {
io.virtualan.requestbody.RequestBody requestBody =
new io.virtualan.requestbody.RequestBody();
requestBody.setObjectMapper(getObjectMapper());
requestBody.setInputRequest(virtualServiceRequest.getInput());
requestBody.setInputObjectType(inputObjectType);
Object object = null;
try {
object = RequestBodyTypes.fromString(inputObjectType.getTypeName())
.getValidMockRequestBody(requestBody);
} catch (NoSuchMessageException | IOException e) {
e.printStackTrace();
object = null;
}
if (object == null) {
return new ResponseEntity(
new VirtualServiceStatus(messageSource
.getMessage("VS_REQUEST_BODY_MISMATCH", null, locale)),
HttpStatus.BAD_REQUEST);
}
}
}
return null;
}
private void findOperationIdForService(VirtualServiceRequest mockLoadRequest) {
if (mockLoadRequest.getOperationId() == null && virtualServiceInfo != null) {
String resourceUrl =
mockLoadRequest.getUrl().substring(1, mockLoadRequest.getUrl().length());
List resouceSplitterList =
new LinkedList(Arrays.asList(resourceUrl.split("/")));
if (resouceSplitterList.size() > 0) {
String operationId = virtualServiceInfo.getOperationId(mockLoadRequest.getMethod(),
virtualServiceInfo.getResourceParent(), resouceSplitterList);
mockLoadRequest.setOperationId(operationId);
mockLoadRequest.setResource(resouceSplitterList.get(0));
}
}
}
private ResponseEntity validateExpectedInput(VirtualServiceRequest mockLoadRequest) {
if (mockLoadRequest.getHttpStatusCode() == null || mockLoadRequest.getMethod() == null
|| mockLoadRequest.getUrl() == null) {
return new ResponseEntity(
new VirtualServiceStatus(
messageSource.getMessage("VS_CREATE_MISSING_INFO", null, locale)),
HttpStatus.BAD_REQUEST);
}
return null;
}
@RequestMapping(value = "/virtualservices/{id}", method = RequestMethod.PUT)
public ResponseEntity updateMockRequest(@PathVariable("id") long id,
@RequestBody VirtualServiceRequest mockLoadRequest) {
VirtualServiceRequest currentMockLoadRequest = virtualService.findById(id);
if (currentMockLoadRequest == null) {
return new ResponseEntity(HttpStatus.NOT_FOUND);
}
currentMockLoadRequest.setInput(mockLoadRequest.getInput());
currentMockLoadRequest.setOutput(mockLoadRequest.getOutput());
currentMockLoadRequest.setOperationId(mockLoadRequest.getOperationId());
virtualService.updateMockRequest(currentMockLoadRequest);
return new ResponseEntity(currentMockLoadRequest, HttpStatus.OK);
}
@RequestMapping(value = "/virtualservices/{id}", method = RequestMethod.DELETE)
public ResponseEntity deleteMockRequest(@PathVariable("id") long id) {
VirtualServiceRequest MockLoadRequest = virtualService.findById(id);
if (MockLoadRequest == null) {
return new ResponseEntity(HttpStatus.NOT_FOUND);
}
virtualService.deleteMockRequestById(id);
return new ResponseEntity(HttpStatus.NO_CONTENT);
}
@RequestMapping(value = "/swagger-catalogs/{name}", method = RequestMethod.GET)
public ResponseEntity> readCatalog(@PathVariable("name") String name) {
List fileList = new LinkedList<>();
try {
for (Resource file : getCatalogs(name)) {
fileList.add(file.getFilename());
}
} catch (IOException e) {
return new ResponseEntity>(HttpStatus.NOT_FOUND);
}
if (fileList.isEmpty()) {
return new ResponseEntity>(HttpStatus.NOT_FOUND);
} else {
return new ResponseEntity>(fileList, HttpStatus.OK);
}
}
private Resource[] getCatalogs(String name) throws IOException {
ClassLoader classLoader = MethodHandles.lookup().getClass().getClassLoader();
PathMatchingResourcePatternResolver resolver =
new PathMatchingResourcePatternResolver(classLoader);
return resolver.getResources("classpath:META-INF/resources/yaml/" + name + "/*.yaml");
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy