Skip to content

Commit

Permalink
Development (#21)
Browse files Browse the repository at this point in the history
* hot fix UI on user modification, add reff example from schema component, enhance path mock

* enhance crud and add security configuration and enhance ui fixing some ui
  • Loading branch information
dekaulitz authored Jun 15, 2020
1 parent 1f4713b commit 272eff9
Show file tree
Hide file tree
Showing 15 changed files with 1,758 additions and 72 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<h2 align="left">MOCKYUP</h2>
MockyUp is a mockup server that release with java version that following OASOPENAPI version 3.xx. You can easyly to integration your swagger documentation with this mockup server with injecting some vendor extension for generating some example.
**We only supported for json request and response**.

For <a href="https://github.com/dekaulitz/mockup-frontend">Mockup UI Repository </a>
<h2 align="left">Stack</h2>
This project build under spring boot framework with spring data mongodb and <a href="https://github.com/swagger-api">swagger library version 3 </a>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public SecurityAuthenticationFilter authenticationTokenFilter() {
new AntPathRequestMatcher("/mocks/users"),
new AntPathRequestMatcher("/mocks/users/list"),
new AntPathRequestMatcher("/mocks/user/**/update"),
new AntPathRequestMatcher("/mocks/user/**/detail")
new AntPathRequestMatcher("/mocks/user/**/detail"),
new AntPathRequestMatcher("/mocks/user/**/delete")
)));
filter.setAuthenticationSuccessHandler(new SecurityAuthenticationSuccess());
filter.setAuthenticationFailureHandler(new SecurityAuthenticationFail());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ public ResponseEntity<Object> adduser(@Valid @RequestBody RegistrationVmodel vmo
} catch (Exception e) {
return this.handlingErrorResponse(e, request);
}
}

@PreAuthorize("hasAnyAuthority('USERS_READ_WRITE')")
@DeleteMapping(value = "/mocks/user/{id}/delete", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Object> deleteUser(@PathVariable String id, HttpServletRequest request) {
try {
this.userModel.deleteUser(id, this.getAuthenticationProfileModel());
return ResponseEntity.ok().build();
} catch (Exception e) {
return this.handlingErrorResponse(e, request);
}

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@
public interface MockHistoryRepository extends MongoRepository<MockHistoryEntities, String> {

public List<MockHistoryEntities> findByMockId(String id);

public void deleteAllByMockId(String mockId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
import com.github.dekaulitz.mockyup.utils.Role;
import com.github.dekaulitz.mockyup.vmodels.MockVmodel;
import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.PathItem;
import io.swagger.v3.oas.models.Paths;
import io.swagger.v3.oas.models.*;
import io.swagger.v3.parser.core.models.SwaggerParseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -35,7 +32,6 @@ abstract class BaseMockModel<M, M1> implements BaseMock<M, M1> {
protected Logger log = LoggerFactory.getLogger(this.getClass());

public void setSaveMockEntity(MockVmodel body, MockEntities mockEntities, AuthenticationProfileModel authenticationProfileModel) throws InvalidMockException {
// Assert.notNull(body.getTitle());
SwaggerParseResult result = null;
try {
mockEntities.setSwagger(JsonMapper.mapper().writeValueAsString(body.getSpec()));
Expand Down Expand Up @@ -68,7 +64,7 @@ private void parsingSpecToOpenApi(MockVmodel body, MockEntities mockEntities) th
OpenAPI openAPI = result.getOpenAPI();
Paths newPath = new Paths();
openAPI.getPaths().forEach((s, pathItem) -> {
newPath.put(s.replace(".", "_").replace("{", "*{"), pathItem);
newPath.put(s.replace("{", "*{"), pathItem);
});
openAPI.setPaths(newPath);
mockEntities.setSpec(JsonMapper.mapper().writeValueAsString(openAPI));
Expand All @@ -85,34 +81,34 @@ public void setUpdateMockEntity(MockVmodel body, MockEntities mockEntities, Auth

}

public MockHelper getMockResponse(PathItem pathItem, HttpServletRequest request, String body, String[] openAPIPaths, String[] paths)
public MockHelper getMockResponse(PathItem pathItem, HttpServletRequest request, String body, String[] openAPIPaths, String[] paths, Components components)
throws NotFoundException, JsonProcessingException, InvalidMockException, UnsupportedEncodingException {
MockHelper mock = null;
JsonNode jsonNode = JsonMapper.mapper().valueToTree(pathItem);
Operation ops = JsonMapper.mapper().treeToValue(jsonNode.get(request.getMethod().toLowerCase()), Operation.class);
if (ops.getExtensions() == null) throw new NotFoundException("no extension found");
if (ops.getExtensions() == null) throw new NotFoundException(ResponseCode.MOCKUP_NOT_FOUND);
Map<String, Object> examples = (Map<String, Object>) ops.getExtensions().get(MockHelper.X_EXAMPLES);
if (examples == null) throw new NotFoundException("no mock example found");
if (examples == null) throw new NotFoundException(ResponseCode.MOCKUP_NOT_FOUND);
for (Map.Entry<String, Object> extension : examples.entrySet()) {
switch (extension.getKey()) {
case MockHelper.X_PATH:
mock = MockHelper.generateResponnsePath(request, (List<Map<String, Object>>) extension.getValue(), openAPIPaths, paths);
mock = MockHelper.generateResponnsePath(request, (List<Map<String, Object>>) extension.getValue(), openAPIPaths, paths, components);
if (mock != null) return mock;
break;
case MockHelper.X_HEADERS:
mock = MockHelper.generateResponseHeader(request, (List<Map<String, Object>>) extension.getValue());
mock = MockHelper.generateResponseHeader(request, (List<Map<String, Object>>) extension.getValue(), components);
if (mock != null) return mock;
break;
case MockHelper.X_QUERY:
mock = MockHelper.generateResponnseQuery(request, (List<Map<String, Object>>) extension.getValue());
mock = MockHelper.generateResponnseQuery(request, (List<Map<String, Object>>) extension.getValue(), components);
if (mock != null) return mock;
break;
case MockHelper.X_BODY:
mock = MockHelper.generateResponseBody(request, (List<Map<String, Object>>) extension.getValue(), body);
mock = MockHelper.generateResponseBody(request, (List<Map<String, Object>>) extension.getValue(), body, components);
if (mock != null) return mock;
break;
case MockHelper.X_DEFAULT:
mock = MockHelper.generateResponseDefault((LinkedHashMap<String, Object>) extension.getValue());
mock = MockHelper.generateResponseDefault((LinkedHashMap<String, Object>) extension.getValue(), components);
if (mock != null) return mock;
break;
default:
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/com/github/dekaulitz/mockyup/models/MockModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public void deleteById(String id, AuthenticationProfileModel authenticationProfi
}
this.checkAccessModificationMocks(mockEntities.get(), authenticationProfileModel);
mockRepository.deleteById(mockEntities.get().getId());
mockHistoryRepository.deleteAllByMockId(mockEntities.get().getId());
}

@Override
Expand Down Expand Up @@ -231,16 +232,14 @@ public MockHelper getMockMocking(HttpServletRequest request, String path, String
//pathitem is exist or without path parameter clean url
PathItem item = openAPI.getPaths().get(path);
if (item != null) {
String openApiPath = path.replace("_", ".");
String[] openAPIPaths = openApiPath.split("/");
return getMockResponse(item, request, body, openAPIPaths, paths);
String[] openAPIPaths = path.split("/");
return getMockResponse(item, request, body, openAPIPaths, paths, openAPI.getComponents());
}
//path item with path parameter we should check every endpoint that related with data
for (Map.Entry<String, PathItem> entry : openAPI.getPaths().entrySet()) {
String s = entry.getKey();
PathItem pathItem = entry.getValue();
String openApiPath = s.replace("_", ".");
String[] openAPIPaths = openApiPath.split("/");
String[] openAPIPaths = s.split("/");
String[] regexPath = new String[paths.length];
// every targeting mocks uri should has same length
if (openAPIPaths.length == paths.length) {
Expand All @@ -259,8 +258,8 @@ public MockHelper getMockMocking(HttpServletRequest request, String path, String
}
}
//check if current openapi path equal with regexpath (path)
if (openApiPath.equals(String.join("/", regexPath)))
return getMockResponse(pathItem, request, body, openAPIPaths, paths);
if (s.equals(String.join("/", regexPath)))
return getMockResponse(pathItem, request, body, openAPIPaths, paths, openAPI.getComponents());
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.HashMap;
import java.util.Map;

@AllArgsConstructor
@NoArgsConstructor
Expand All @@ -14,5 +14,16 @@
public class MockResponseModel {
private int httpCode;
private Object response;
private HashMap<String, Object> headers;
private Map<String, Object> headers;

public Object get$ref() {
return $ref;
}

public void set$ref(Object $ref) {
this.$ref = $ref;
}

private Object $ref;

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ public UserEntities addUser(RegistrationVmodel vmodel, AuthenticationProfileMode
return this.userRepository.save(userEntities);
}

public void deleteUser(String userId, AuthenticationProfileModel authenticationProfileModel) throws DuplicateDataEntry {
Optional<UserEntities> userEntities = this.userRepository.findById(userId);
if (!userEntities.isPresent()) {
throw new DuplicateDataEntry(ResponseCode.USER_NOT_FOUND);
}

this.userRepository.delete(userEntities.get());
}

public AccessVmodel doLogin(UserLoginVmodel vmodel) throws UnathorizedAccess, UnsupportedEncodingException {
UserEntities userExist = this.userRepository.findFirstByUsername(vmodel.getUsername());
if (userExist == null) {
Expand Down
Loading

0 comments on commit 272eff9

Please sign in to comment.