Skip to content

Commit

Permalink
feedback Flo
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan-Thurner committed Aug 17, 2024
1 parent 375d4e6 commit 77e04b7
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 164 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/analysis-of-endpoint-connections.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ jobs:
with:
java-version: '${{ env.java }}'
distribution: 'temurin'
cache: 'gradle'

- name: Set up node.js
uses: actions/setup-node@v4
with:
node-version: '${{ env.node }}'
cache: 'node'

- name: Parse client sided REST-API calls
run: |
Expand Down Expand Up @@ -63,6 +65,7 @@ jobs:
with:
distribution: 'temurin'
java-version: '${{ env.java }}'
cache: 'gradle'

- name: Download JSON files
uses: actions/download-artifact@v4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static void main(String[] args) {
}

/**
* Analyzes endpoints and matches them with REST calls.
* Analyzes server side endpoints and matches them with client side REST calls.
*
* This method reads endpoint and REST call information from JSON files,
* compares them to find matching REST calls for each endpoint, and writes
Expand All @@ -52,7 +52,7 @@ private static void analyzeEndpoints() {
for (RestCallFileInformation restCallFile : restCallFiles) {
for (RestCallInformation restCall : restCallFile.restCalls()) {
String restCallURI = restCall.buildComparableRestCallUri();
restCallMap.computeIfAbsent(restCallURI, k -> new ArrayList<>()).add(restCall);
restCallMap.computeIfAbsent(restCallURI, uri -> new ArrayList<>()).add(restCall);
}
}

Expand All @@ -66,7 +66,7 @@ private static void analyzeEndpoints() {
if (matchingRestCalls.isEmpty() && endpointURI.endsWith("*")) {
for (String uri : restCallMap.keySet()) {
if (uri.startsWith(endpoint.buildComparableEndpointUri().substring(0, endpoint.buildComparableEndpointUri().length() - 1))
&& endpoint.getHttpMethod().toLowerCase().equals(restCallMap.get(uri).get(0).getMethod().toLowerCase())) {
&& endpoint.getHttpMethod().toLowerCase().equals(restCallMap.get(uri).get(0).method().toLowerCase())) {
matchingRestCalls.addAll(restCallMap.get(uri));
}
}
Expand Down Expand Up @@ -111,9 +111,9 @@ private static void printEndpointAnalysisResult() {
endpointsAndMatchingRestCalls.unusedEndpoints().stream().forEach(endpoint -> {
logger.info("=============================================");
logger.info("Endpoint URI: {}", endpoint.buildCompleteEndpointURI());
logger.info("HTTP method: {}", endpoint.getHttpMethodAnnotation());
logger.info("File path: {}", endpoint.getClassName());
logger.info("Line: {}", endpoint.getLine());
logger.info("HTTP method: {}", endpoint.httpMethodAnnotation());
logger.info("File path: {}", endpoint.className());
logger.info("Line: {}", endpoint.line());
logger.info("=============================================");
logger.info("No matching REST call found for endpoint: {}", endpoint.buildCompleteEndpointURI());
logger.info("---------------------------------------------");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,97 +4,22 @@

import com.fasterxml.jackson.annotation.JsonIgnore;

public class EndpointInformation {

private String requestMapping;

private String endpoint;

private String httpMethodAnnotation;

private String URI;

private String className;

private int line;

private List<String> otherAnnotations;

public EndpointInformation() {

}

public EndpointInformation(String requestMapping, String endpoint, String httpMethodAnnotation, String URI, String className, int line, List<String> otherAnnotations) {
this.requestMapping = requestMapping;
this.endpoint = endpoint;
this.httpMethodAnnotation = httpMethodAnnotation;
this.URI = URI;
this.className = className;
this.line = line;
this.otherAnnotations = otherAnnotations;
}

public List<String> getOtherAnnotations() {
return otherAnnotations;
}

public void setOtherAnnotations(List<String> otherAnnotations) {
this.otherAnnotations = otherAnnotations;
}

public String getRequestMapping() {
return requestMapping;
}

public void setRequestMapping(String requestMapping) {
this.requestMapping = requestMapping;
}

public String getEndpoint() {
return endpoint;
}

public void setEndpoint(String endpoint) {
this.endpoint = endpoint;
}

public String getHttpMethodAnnotation() {
return httpMethodAnnotation;
}

public void setHttpMethodAnnotation(String httpMethodAnnotation) {
this.httpMethodAnnotation = httpMethodAnnotation;
}

public String getURI() {
return URI;
}

public void setURI(String URI) {
this.URI = URI;
}

public String getClassName() {
return className;
}

public void setClassName(String className) {
this.className = className;
}

public int getLine() {
return line;
}

public void setLine(int line) {
this.line = line;
}

public record EndpointInformation(
String requestMapping,
String endpoint,
String httpMethodAnnotation,
String URI,
String className,
int line,
List<String> otherAnnotations
) {
public String buildCompleteEndpointURI() {
StringBuilder result = new StringBuilder();
if (this.requestMapping != null && !this.requestMapping.isEmpty()) {
// Remove quotes from the requestMapping as they are used to define the String in the source code but are not part of the URI
result.append(this.requestMapping.replace("\"", ""));
}
// Remove quotes from the URI as they are used to define the String in the source code but are not part of the URI
result.append(this.URI.replace("\"", ""));
return result.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private static void analyzeRestCalls() {
for (EndpointClassInformation endpointClass : endpointClasses) {
for (EndpointInformation endpoint : endpointClass.endpoints()) {
String endpointURI = endpoint.buildComparableEndpointUri();
endpointMap.computeIfAbsent(endpointURI, k -> new ArrayList<>()).add(endpoint);
endpointMap.computeIfAbsent(endpointURI, uri -> new ArrayList<>()).add(endpoint);
}
}

Expand All @@ -60,22 +60,14 @@ private static void analyzeRestCalls() {
String restCallURI = restCall.buildComparableRestCallUri();
List<EndpointInformation> matchingEndpoints = endpointMap.getOrDefault(restCallURI, new ArrayList<>());

// Check for wildcard matches if no exact match is found
if (matchingEndpoints.isEmpty() && restCallURI.endsWith("*")) {
for (String uri : endpointMap.keySet()) {
if (uri.startsWith(restCallURI.substring(0, restCallURI.length() - 1))
&& endpointMap.get(uri).get(0).getHttpMethod().toLowerCase().equals(restCall.getMethod().toLowerCase())) {
matchingEndpoints.addAll(endpointMap.get(uri));
}
}
}
checkForWildcardMatches(restCall, matchingEndpoints, restCallURI, endpointMap);

if (matchingEndpoints.isEmpty()) {
restCallsWithoutMatchingEndpoint.add(restCall);
}
else {
for (EndpointInformation endpoint : matchingEndpoints) {
restCallsWithMatchingEndpoint.add(new RestCallWithMatchingEndpoint(endpoint, restCall, restCall.getFileName()));
restCallsWithMatchingEndpoint.add(new RestCallWithMatchingEndpoint(endpoint, restCall, restCall.fileName()));
}
}
}
Expand All @@ -89,6 +81,17 @@ private static void analyzeRestCalls() {
}
}

private static void checkForWildcardMatches(RestCallInformation restCall, List<EndpointInformation> matchingEndpoints, String restCallURI, Map<String, List<EndpointInformation>> endpointMap) {
if (matchingEndpoints.isEmpty() && restCallURI.endsWith("*")) {
for (String uri : endpointMap.keySet()) {
if (uri.startsWith(restCallURI.substring(0, restCallURI.length() - 1))
&& endpointMap.get(uri).get(0).getHttpMethod().toLowerCase().equals(restCall.method().toLowerCase())) {
matchingEndpoints.addAll(endpointMap.get(uri));
}
}
}
}

/**
* Prints the endpoint analysis result.
*
Expand All @@ -113,9 +116,9 @@ private static void printRestCallAnalysisResult() {
restCallsAndMatchingEndpoints.restCallsWithoutMatchingEndpoints().stream().forEach(endpoint -> {
logger.info("=============================================");
logger.info("REST call URI: {}", endpoint.buildCompleteRestCallURI());
logger.info("HTTP method: {}", endpoint.getMethod());
logger.info("File path: {}", endpoint.getFileName());
logger.info("Line: {}", endpoint.getLine());
logger.info("HTTP method: {}", endpoint.method());
logger.info("File path: {}", endpoint.fileName());
logger.info("Line: {}", endpoint.line());
logger.info("=============================================");
logger.info("No matching endpoint found for REST call: {}", endpoint.buildCompleteRestCallURI());
logger.info("---------------------------------------------");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,57 +1,11 @@
package de.tum.cit.endpointanalysis;

public class RestCallInformation {

private String method;

private String url;

private int line;

private String fileName;

public RestCallInformation() {
}

public RestCallInformation(String method, String url, int line, String fileName) {
this.method = method;
this.url = url;
this.line = line;
this.fileName = fileName;
}

public String getMethod() {
return method;
}

public void setMethod(String method) {
this.method = method;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public int getLine() {
return line;
}

public void setLine(int line) {
this.line = line;
}

public String getFileName() {
return fileName;
}

public void setFileName(String fileName) {
this.fileName = fileName;
}

public record RestCallInformation(
String method,
String url,
int line,
String fileName
) {
public String buildCompleteRestCallURI() {
String result = this.url.replace("`", "");
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ function parseTypeScriptFile(filePath: string): TSESTree.Program | null {
});
} catch (error) {
console.error(`Failed to parse TypeScript file at ${filePath}:`, error);
console.error('Please make sure the file is valid TypeScript code.');
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,8 @@ export class Postprocessor {
private readonly ast: TSESTree.Program;

/**
* Constructs a new instance of the Postprocessor class.
*
* @param fileName - The name of the file being processed.
* @param ast - The abstract syntax tree (AST) of the file being processed. The Tree must be a TSESTree
* @param ast - The abstract syntax tree (AST) of the processed file.
*/
constructor(fileName: string, ast: TSESTree.Program) {
this.fileName = fileName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ export class Preprocessor {
private memberVariables: Map<string, Attribute> = new Map<string, Attribute>();

/**
* Constructs a new instance of the Preprocessor class.
*
* @param ast - The abstract syntax tree (AST) of the file being processed. The Tree must be a TSESTree
* @param ast - The abstract syntax tree (AST) of the processed file.
*/
constructor(ast: TSESTree.Program) {
this.ast = ast;
Expand Down

0 comments on commit 77e04b7

Please sign in to comment.