From ba1c6477ab2361a5bbe336a875fad43dcdca9e0a Mon Sep 17 00:00:00 2001 From: serhii-vydiuk-kevychsolutions Date: Wed, 8 Jan 2025 09:11:11 +0200 Subject: [PATCH] feat(java): Collect and log request-response data (#1154) --- .../CustomUserDataCollectorConfig.java | 38 ++++ .../com/readme/example/OwlController.java | 33 ++-- .../src/main/resources/application.yaml | 2 +- .../README.md | 126 ++++++++++-- .../pom.xml | 2 +- .../DataCollectionAutoConfiguration.java | 54 ++++- .../starter/config/UserDataProperties.java | 2 +- .../datacollection/DataCollectionFilter.java | 71 ++++--- .../ServletDataPayloadAdapter.java | 58 +++++- .../ServletRequestDataCollector.java | 43 +++- .../userinfo}/FieldMapping.java | 2 +- .../userinfo/ServletUserDataCollector.java | 28 ++- .../userinfo/ServletUserDataExtractor.java | 30 +-- .../userinfo}/UserDataExtractor.java | 2 +- .../DataCollectionFilterTest.java | 143 ++++++++++++++ .../ServletDataPayloadAdapterTest.java | 31 +-- .../ServletRequestDataCollectorTest.java | 106 ++++++++++ .../ServletUserDataCollectorTest.java | 6 +- .../ServletUserDataExtractorTest.java | 14 ++ packages/java/readme-metrics/.gitignore | 8 +- packages/java/readme-metrics/.idea/.gitignore | 8 - .../java/readme-metrics/.idea/encodings.xml | 7 - packages/java/readme-metrics/.idea/misc.xml | 14 -- .../java/readme-metrics/.idea/uiDesigner.xml | 124 ------------ packages/java/readme-metrics/.idea/vcs.xml | 6 - packages/java/readme-metrics/pom.xml | 45 +++-- .../main/java/com/readme/ReadmeMetrics.java | 59 ------ .../readme/dataextraction/ApiKeyMasker.java | 26 +++ .../dataextraction/DataPayloadAdapter.java | 13 +- .../com/readme/dataextraction/LogOptions.java | 16 ++ .../dataextraction/payload/PayloadData.java | 19 ++ .../requestresponse/ApiCallLogData.java | 15 ++ .../payload/requestresponse/RequestData.java | 22 +++ .../RequestDataCollector.java | 10 +- .../payload/requestresponse/ResponseData.java | 18 ++ .../payload/user}/UserData.java | 2 +- .../{ => payload/user}/UserDataCollector.java | 4 +- .../{ => payload/user}/UserDataField.java | 2 +- .../{ => payload/user}/UserDataSource.java | 2 +- .../com/readme/datatransfer/DataSender.java | 4 +- .../readme/datatransfer/HttpDataSender.java | 39 ++-- .../readme/datatransfer/OutgoingLogBody.java | 42 ++++ .../OutgoingLogBodyConstructor.java | 187 ++++++++++++++++++ .../datatransfer/PayloadDataDispatcher.java | 31 +++ .../com/readme/datatransfer/har/Group.java | 12 ++ .../java/com/readme/datatransfer/har/Har.java | 39 ++++ .../com/readme/datatransfer/har/HarCache.java | 63 ++++++ .../readme/datatransfer/har/HarContent.java | 40 ++++ .../readme/datatransfer/har/HarCookie.java | 43 ++++ .../datatransfer/har/HarCreatorBrowser.java | 38 ++++ .../com/readme/datatransfer/har/HarEntry.java | 48 +++++ .../readme/datatransfer/har/HarHeader.java | 37 ++++ .../com/readme/datatransfer/har/HarLog.java | 44 +++++ .../com/readme/datatransfer/har/HarPage.java | 42 ++++ .../datatransfer/har/HarPageTiming.java | 42 ++++ .../readme/datatransfer/har/HarPostData.java | 41 ++++ .../datatransfer/har/HarPostDataParam.java | 39 ++++ .../datatransfer/har/HarQueryParam.java | 37 ++++ .../readme/datatransfer/har/HarRequest.java | 49 +++++ .../readme/datatransfer/har/HarResponse.java | 50 +++++ .../readme/datatransfer/har/HarTiming.java | 45 +++++ .../readme/datatransfer/har/HttpMethod.java | 5 + .../readme/datatransfer/har/HttpStatus.java | 59 ++++++ .../com/readme/domain/RequestPayload.java | 13 -- .../exception/EmptyRequestBodyException.java | 8 - .../datatransfer/HttpDataSenderTest.java | 111 +++++++---- .../OutgoingLogBodyConstructorTest.java | 105 ++++++++++ 67 files changed, 2056 insertions(+), 468 deletions(-) create mode 100644 packages/java/examples/OwlTestApp/src/main/java/com/readme/example/CustomUserDataCollectorConfig.java rename packages/java/{readme-metrics/src/main/java/com/readme/config => readme-metrics-spring-boot-starter/src/main/java/com/readme/starter/datacollection/userinfo}/FieldMapping.java (90%) rename packages/java/{readme-metrics/src/main/java/com/readme/dataextraction => readme-metrics-spring-boot-starter/src/main/java/com/readme/starter/datacollection/userinfo}/UserDataExtractor.java (96%) create mode 100644 packages/java/readme-metrics-spring-boot-starter/src/test/java/com/readme/starter/datacollection/DataCollectionFilterTest.java create mode 100644 packages/java/readme-metrics-spring-boot-starter/src/test/java/com/readme/starter/datacollection/ServletRequestDataCollectorTest.java delete mode 100644 packages/java/readme-metrics/.idea/.gitignore delete mode 100644 packages/java/readme-metrics/.idea/encodings.xml delete mode 100644 packages/java/readme-metrics/.idea/misc.xml delete mode 100644 packages/java/readme-metrics/.idea/uiDesigner.xml delete mode 100644 packages/java/readme-metrics/.idea/vcs.xml delete mode 100644 packages/java/readme-metrics/src/main/java/com/readme/ReadmeMetrics.java create mode 100644 packages/java/readme-metrics/src/main/java/com/readme/dataextraction/ApiKeyMasker.java create mode 100644 packages/java/readme-metrics/src/main/java/com/readme/dataextraction/LogOptions.java create mode 100644 packages/java/readme-metrics/src/main/java/com/readme/dataextraction/payload/PayloadData.java create mode 100644 packages/java/readme-metrics/src/main/java/com/readme/dataextraction/payload/requestresponse/ApiCallLogData.java create mode 100644 packages/java/readme-metrics/src/main/java/com/readme/dataextraction/payload/requestresponse/RequestData.java rename packages/java/readme-metrics/src/main/java/com/readme/dataextraction/{ => payload/requestresponse}/RequestDataCollector.java (80%) create mode 100644 packages/java/readme-metrics/src/main/java/com/readme/dataextraction/payload/requestresponse/ResponseData.java rename packages/java/readme-metrics/src/main/java/com/readme/{domain => dataextraction/payload/user}/UserData.java (75%) rename packages/java/readme-metrics/src/main/java/com/readme/dataextraction/{ => payload/user}/UserDataCollector.java (94%) rename packages/java/readme-metrics/src/main/java/com/readme/dataextraction/{ => payload/user}/UserDataField.java (83%) rename packages/java/readme-metrics/src/main/java/com/readme/dataextraction/{ => payload/user}/UserDataSource.java (82%) create mode 100644 packages/java/readme-metrics/src/main/java/com/readme/datatransfer/OutgoingLogBody.java create mode 100644 packages/java/readme-metrics/src/main/java/com/readme/datatransfer/OutgoingLogBodyConstructor.java create mode 100644 packages/java/readme-metrics/src/main/java/com/readme/datatransfer/PayloadDataDispatcher.java create mode 100644 packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/Group.java create mode 100644 packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/Har.java create mode 100644 packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarCache.java create mode 100644 packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarContent.java create mode 100644 packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarCookie.java create mode 100644 packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarCreatorBrowser.java create mode 100644 packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarEntry.java create mode 100644 packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarHeader.java create mode 100644 packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarLog.java create mode 100644 packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarPage.java create mode 100644 packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarPageTiming.java create mode 100644 packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarPostData.java create mode 100644 packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarPostDataParam.java create mode 100644 packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarQueryParam.java create mode 100644 packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarRequest.java create mode 100644 packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarResponse.java create mode 100644 packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarTiming.java create mode 100644 packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HttpMethod.java create mode 100644 packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HttpStatus.java delete mode 100644 packages/java/readme-metrics/src/main/java/com/readme/domain/RequestPayload.java delete mode 100644 packages/java/readme-metrics/src/main/java/com/readme/exception/EmptyRequestBodyException.java create mode 100644 packages/java/readme-metrics/src/test/java/com/readme/datatransfer/OutgoingLogBodyConstructorTest.java diff --git a/packages/java/examples/OwlTestApp/src/main/java/com/readme/example/CustomUserDataCollectorConfig.java b/packages/java/examples/OwlTestApp/src/main/java/com/readme/example/CustomUserDataCollectorConfig.java new file mode 100644 index 0000000000..f971870b9f --- /dev/null +++ b/packages/java/examples/OwlTestApp/src/main/java/com/readme/example/CustomUserDataCollectorConfig.java @@ -0,0 +1,38 @@ +package com.readme.example; + +import org.springframework.context.annotation.Configuration; + +/** + * Configuration class for customizing the strategy to collect user data. + * + *

This configuration provides a custom implementation of {@link UserDataCollector}, + * which overrides the default behavior provided by the SDK. It allows developers + * to specify their own logic for extracting user-specific information, such as API keys, + * email addresses, or labels, from the incoming HTTP requests.

+ * + *

In this example, the API key is extracted from the HTTP headers using the header + * "X-User-Name", while the email and label fields are hardcoded with custom values. + * Developers can modify this logic to suit their application's requirements.

+ * + *

By defining this bean, Spring Boot's auto-configuration will automatically use + * this custom implementation instead of the default {@link UserDataCollector}.

+ */ +@Configuration +public class CustomUserDataCollectorConfig { + + //Uncomment the code below to have a custom user data collection configuration. + //It automatically overrides the default one + + +// @Bean +// public UserDataCollector customUserDataCollector() { +// return payloadAdapter -> { +// String apiKey = payloadAdapter.getRequestHeaders().get("x-user-name"); +// return UserData.builder() +// .apiKey(apiKey) +// .email("owl@owlfactory.abc") +// .label("owl-label") +// .build(); +// }; +// } +} diff --git a/packages/java/examples/OwlTestApp/src/main/java/com/readme/example/OwlController.java b/packages/java/examples/OwlTestApp/src/main/java/com/readme/example/OwlController.java index c19f7913fe..88cc26d076 100644 --- a/packages/java/examples/OwlTestApp/src/main/java/com/readme/example/OwlController.java +++ b/packages/java/examples/OwlTestApp/src/main/java/com/readme/example/OwlController.java @@ -1,15 +1,12 @@ package com.readme.example; +import com.readme.datatransfer.har.HttpStatus; import org.springframework.beans.factory.annotation.Value; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PutMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.http.HttpHeaders; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; -import java.util.UUID; +import java.util.*; @RestController public class OwlController { @@ -34,9 +31,21 @@ public Collection getAllOwl() { } @PutMapping("/owl/{owlName}") - public String createOwl(@PathVariable String owlName) { - UUID owlUuid = UUID.randomUUID(); - owlStorage.put(owlUuid.toString(), owlName); - return "Owl " + owlName + " is created wit id: " + owlUuid; + public ResponseEntity createOwl(@PathVariable String owlName, @RequestBody String body) { + UUID birdId = UUID.randomUUID(); + owlStorage.put(birdId.toString(), owlName); + + String responseBody = "Bird " + owlName + " created a bird with id: " + birdId + "\n" + + "Creation request body: \n" + body; + + HttpHeaders headers = new HttpHeaders(); + headers.add("bird-id", birdId.toString()); + headers.add("bird-token", Base64.getEncoder() + .encodeToString(birdId.toString() + .getBytes())); + + return ResponseEntity.status(HttpStatus.CREATED.getCode()) + .headers(headers) + .body(responseBody); } } \ No newline at end of file diff --git a/packages/java/examples/OwlTestApp/src/main/resources/application.yaml b/packages/java/examples/OwlTestApp/src/main/resources/application.yaml index 7f2808045f..4611a65419 100644 --- a/packages/java/examples/OwlTestApp/src/main/resources/application.yaml +++ b/packages/java/examples/OwlTestApp/src/main/resources/application.yaml @@ -23,7 +23,7 @@ readme: # label: # source: jsonBody # fieldName: owl-creator/label - +# #readme: # readmeApiKey: ${README_API_KEY} # userdata: diff --git a/packages/java/readme-metrics-spring-boot-starter/README.md b/packages/java/readme-metrics-spring-boot-starter/README.md index a94a718781..61ece17f3b 100644 --- a/packages/java/readme-metrics-spring-boot-starter/README.md +++ b/packages/java/readme-metrics-spring-boot-starter/README.md @@ -1,46 +1,134 @@ -# Monitoring Library Configuration Guide +## Table of Contents +1. [Overview](#overview) +2. [Configuration](#configuration) + - [Readme API Key](#readme-api-key) + - [User data configuration ](#userdata-configuration) + - [Custom user data collection config](#customizing-user-data-collection) + +--- ## Overview -This spring-boot-starter provides possibility to integrate Readme Metrics SDK to a Spring Boot application easily. -It provides a convenient way to extract user-specific information (e.g., api-key, email, label) from -incoming HTTP requests. It allows configuring multiple extraction methods, such as HTTP headers, JWT claims, or JSON body fields. +This library provides an easy way to integrate the ReadMe Metrics into a Spring Boot application, +enabling comprehensive monitoring and logging capabilities. +The SDK is designed to collect detailed information from HTTP requests and responses, as well as user-specific data, +for better observability and insights into application behavior. + +### Key Features: +1. **Request and Response Data Logging**: + - Collects HTTP request and response details, including headers, body content, and HTTP status codes. + - Ensures minimal impact on the application's core business logic by leveraging efficient wrappers for request and response processing. + +2. **User Data Extraction**: + - Logs information about the user making the request, such as `api-key`, `email`, and `label`. + - Supports multiple configurable data extraction methods: + - **HTTP headers** + - **JWT claims** + - **JSON body fields** + +--- ## Configuration -Add the following properties to your `application.yaml` or `application.properties` file. -Each field (`apiKey`, `email`, `label`) requires two sub-properties: -- `source`: Defines where to extract the data from. +To configure the library, you need to define two main aspects: +1. The `ReadMe API Key`, which is required to send logged data to the ReadMe platform. +2. The `UserData` fields (`apiKey`, `email`, `label`), which define where to extract user-specific information from incoming requests. + +### ReadMe API Key configuration + +The `ReadMe API Key` is a unique identifier that you receive from the ReadMe platform. It is used to authenticate and authorize data sent to the ReadMe metrics endpoint. +You can configure the `ReadMe API Key` in your `application.yaml` or `application.properties` file using environment variables for security. + +**application.yaml:** +```yaml +readme: + readmeApiKey: ${README_API_KEY} +``` +**application.properties:** +```properties +readme.readmeApiKey=${README_API_KEY} +``` + +### UserData configuration + +The library allows you to extract user-specific data (`apiKey`, `email`, `label`) from incoming HTTP requests. Each field requires two properties: +- **`source`**: Specifies where to extract the data from. - Possible values: - - `header`: Extracts data from an HTTP header. - - `jwtClaim`: Extracts data from a JWT token claim. - - `jsonBody`: Extracts data from the JSON body of a request. -- `fieldName`: Specifies the name or key associated with the source. + - `header`: Extracts data from an HTTP header. + - `jwtClaim`: Extracts data from a JWT token claim. + - `jsonBody`: Extracts data from the JSON body of a request. +- **`fieldName`**: The key or field name corresponding to the specified source. -### Example Configuration (YAML) + +**application.yaml:** ```yaml readme: - readmeApiKey: ${readmeApiKey} userdata: apiKey: source: header fieldName: X-User-Id email: - source: jwt + source: jwtClaim fieldName: aud label: source: jsonBody - fieldName: user.name + fieldName: user/name ``` -### Example Configuration (PROPERTIES) +**application.properties:** ```properties readme.userdata.apikey.source=header readme.userdata.apikey.fieldname=X-User-Id -readme.userdata.email.source=jwtClaim +readme.userdata.email.source=jwt readme.userdata.email.fieldname=aud readme.userdata.label.source=jsonBody -readme.userdata.label.fieldname=user.name -``` \ No newline at end of file +readme.userdata.label.fieldname=user/name +``` + +### Customizing user data collection + +The library provides a default implementation of `UserDataCollector`, which extracts user data based on the configuration +in your YAML or properties file. However, some use cases may require custom logic to extract user-specific information. +For example: +- The user data comes from a unique header format. +- Complex logic is needed to determine user-specific fields. +- Multiple fields need to be combined dynamically. + +In such cases, you can configure the library with a custom way of extracting user data information +by creating your own implementation of `UserDataCollector`. + +--- + +#### How to Create a Custom UserDataCollector + +To create a custom `UserDataCollector`, define a Spring bean for your implementation. +The library's configuration will automatically use your custom implementation if it is present in the application context. + +--- + +#### Example: Custom Implementation + +Below is an example of a custom `UserDataCollector` that extracts the `apiKey` from an HTTP header and assigns static +values for `email` and `label`. + +```java +@Configuration +public class CustomUserDataCollectorConfig { + + @Bean + public UserDataCollector customUserDataCollector() { + return payloadAdapter -> { + // Extract the apiKey from the request headers + String apiKey = payloadAdapter.getRequestHeaders().get("x-user-name"); + + // Build the UserData object + return UserData.builder() + .apiKey(apiKey) + .email("owl@birdfact0ry.abc") + .label("owl-label") + .build(); + }; + } +} \ No newline at end of file diff --git a/packages/java/readme-metrics-spring-boot-starter/pom.xml b/packages/java/readme-metrics-spring-boot-starter/pom.xml index 377871affc..ba743b3648 100644 --- a/packages/java/readme-metrics-spring-boot-starter/pom.xml +++ b/packages/java/readme-metrics-spring-boot-starter/pom.xml @@ -32,7 +32,7 @@ com.readme - readme-metrics + metrics-core ${readme-metrics.version} diff --git a/packages/java/readme-metrics-spring-boot-starter/src/main/java/com/readme/starter/config/DataCollectionAutoConfiguration.java b/packages/java/readme-metrics-spring-boot-starter/src/main/java/com/readme/starter/config/DataCollectionAutoConfiguration.java index b92c91e5ee..008e016a7d 100644 --- a/packages/java/readme-metrics-spring-boot-starter/src/main/java/com/readme/starter/config/DataCollectionAutoConfiguration.java +++ b/packages/java/readme-metrics-spring-boot-starter/src/main/java/com/readme/starter/config/DataCollectionAutoConfiguration.java @@ -1,11 +1,21 @@ package com.readme.starter.config; -import com.readme.dataextraction.RequestDataCollector; -import com.readme.dataextraction.UserDataCollector; +import com.readme.config.CoreConfig; + +import com.readme.dataextraction.payload.requestresponse.RequestDataCollector; +import com.readme.dataextraction.payload.user.UserDataCollector; +import com.readme.datatransfer.DataSender; +import com.readme.datatransfer.HttpDataSender; +import com.readme.datatransfer.OutgoingLogBodyConstructor; +import com.readme.datatransfer.PayloadDataDispatcher; import com.readme.starter.datacollection.DataCollectionFilter; import com.readme.starter.datacollection.ServletDataPayloadAdapter; +import com.readme.starter.datacollection.userinfo.ServletUserDataCollector; +import com.readme.starter.datacollection.userinfo.UserDataExtractor; import lombok.AllArgsConstructor; -import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import lombok.extern.slf4j.Slf4j; +import okhttp3.OkHttpClient; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; @@ -24,20 +34,52 @@ * */ @Configuration -@ConditionalOnClass({UserDataProperties.class}) @ComponentScan(basePackages = {"com.readme.starter"}) @AllArgsConstructor +@Slf4j public class DataCollectionAutoConfiguration { + private ReadmeConfigurationProperties readmeProperties; + @Bean public FilterRegistrationBean metricsFilter( RequestDataCollector requestDataCollector, - UserDataCollector userDataCollector) { + UserDataCollector userDataCollector, + PayloadDataDispatcher payloadDataDispatcher) { FilterRegistrationBean registrationBean = new FilterRegistrationBean<>(); - registrationBean.setFilter(new DataCollectionFilter(requestDataCollector, userDataCollector)); + registrationBean.setFilter(new DataCollectionFilter(userDataCollector, requestDataCollector, payloadDataDispatcher)); registrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE); registrationBean.addUrlPatterns("/*"); return registrationBean; } + @Bean + @ConditionalOnMissingBean(UserDataCollector.class) + public UserDataCollector userDataCollector(UserDataProperties userDataProperties, + UserDataExtractor extractionService) { + log.info("readme-metrics: Creating of default user data collector"); + return new ServletUserDataCollector(userDataProperties, extractionService); + } + + @Bean + public DataSender dataSender() { + String readmeApiKey = readmeProperties.getReadmeApiKey(); + CoreConfig coreConfig = CoreConfig.builder() + .readmeAPIKey(readmeApiKey) + .build(); + OkHttpClient okHttpClient = new OkHttpClient(); + + return new HttpDataSender(okHttpClient, coreConfig); + } + + @Bean + public OutgoingLogBodyConstructor outgoingPayloadConstructor() { + return new OutgoingLogBodyConstructor(); + } + + @Bean + public PayloadDataDispatcher payloadDataDispatcher(DataSender dataSender, + OutgoingLogBodyConstructor outgoingLogConstructor) { + return new PayloadDataDispatcher(dataSender, outgoingLogConstructor); + } } diff --git a/packages/java/readme-metrics-spring-boot-starter/src/main/java/com/readme/starter/config/UserDataProperties.java b/packages/java/readme-metrics-spring-boot-starter/src/main/java/com/readme/starter/config/UserDataProperties.java index 484e50af17..07a0ced8d7 100644 --- a/packages/java/readme-metrics-spring-boot-starter/src/main/java/com/readme/starter/config/UserDataProperties.java +++ b/packages/java/readme-metrics-spring-boot-starter/src/main/java/com/readme/starter/config/UserDataProperties.java @@ -1,6 +1,6 @@ package com.readme.starter.config; -import com.readme.config.FieldMapping; +import com.readme.starter.datacollection.userinfo.FieldMapping; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; diff --git a/packages/java/readme-metrics-spring-boot-starter/src/main/java/com/readme/starter/datacollection/DataCollectionFilter.java b/packages/java/readme-metrics-spring-boot-starter/src/main/java/com/readme/starter/datacollection/DataCollectionFilter.java index 340eefab2d..c5874b4317 100644 --- a/packages/java/readme-metrics-spring-boot-starter/src/main/java/com/readme/starter/datacollection/DataCollectionFilter.java +++ b/packages/java/readme-metrics-spring-boot-starter/src/main/java/com/readme/starter/datacollection/DataCollectionFilter.java @@ -1,8 +1,11 @@ package com.readme.starter.datacollection; -import com.readme.dataextraction.RequestDataCollector; -import com.readme.dataextraction.UserDataCollector; -import com.readme.domain.UserData; +import com.readme.dataextraction.payload.PayloadData; +import com.readme.dataextraction.payload.requestresponse.ApiCallLogData; +import com.readme.dataextraction.payload.requestresponse.RequestDataCollector; +import com.readme.dataextraction.payload.user.UserData; +import com.readme.dataextraction.payload.user.UserDataCollector; +import com.readme.datatransfer.PayloadDataDispatcher; import jakarta.servlet.*; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; @@ -10,61 +13,67 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.web.util.ContentCachingRequestWrapper; import org.springframework.web.util.ContentCachingResponseWrapper; - import java.io.IOException; +import java.util.Date; +import java.util.concurrent.CompletableFuture; -import static org.springframework.http.HttpMethod.GET; +import static com.readme.dataextraction.payload.PayloadData.*; import static org.springframework.http.HttpMethod.OPTIONS; +//TODO Fix: +// 1. Convert IpV6 to V4 ????? +// 2. * Request.entries.pageref is empty +// 5. * Add response content size and mime type +// 6. Handle Basic tokens as well as Bearer ones +// 7. Validate the collected data on readme dashboard +// 8. Fix core and starter tests +// 9. Consider to rename core-metrics (maybe just "core") and starter (maybe just "spring") +// 10. Handle case if SDK user configured getting request user data from body, but GET req doesn't have it +// 11. Validate user data. Collect request data only if user data is valid ? + @AllArgsConstructor @Slf4j public class DataCollectionFilter implements Filter { + private UserDataCollector userDataCollector; + private RequestDataCollector requestDataCollector; - private UserDataCollector userDataCollector; + private PayloadDataDispatcher payloadDispatcher; - //TODO - // 1. Research possibility to collect metrics in a separate thread, as it may produce - // race condition on reading body data stream. - // 2. Problem to solve: if we collect a request/response after doFilter(r,r), it means - // the request dataStream will be red by customer's business logic and will not be available to us. - // On the other hand, if we collect a request before doFilter, the response data is not available yet. @Override public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { - HttpServletRequest request = (HttpServletRequest) req; - HttpServletResponse response = (HttpServletResponse) resp; + ContentCachingRequestWrapper request = new ContentCachingRequestWrapper((HttpServletRequest) req); + ContentCachingResponseWrapper response = new ContentCachingResponseWrapper((HttpServletResponse) resp); + + PayloadDataBuilder payloadDataBuilder = PayloadData.builder(); + payloadDataBuilder.requestStartedDateTime(new Date()); try { if (request.getMethod().equalsIgnoreCase(OPTIONS.name())) { chain.doFilter(req, resp); - } else if (request.getMethod().equalsIgnoreCase(GET.name())) { + } else { + chain.doFilter(request, response); ServletDataPayloadAdapter payload = new ServletDataPayloadAdapter(request, response); - //TODO: Handle case if SDK user configured getting request user data from body, but GET req doesn't have it UserData userData = userDataCollector.collect(payload); - //TODO: Validate user data. Collect request data only if user data is valid ? - requestDataCollector.collect(payload, userData); - chain.doFilter(req, resp); - } else { - ContentCachingRequestWrapper cacheableRequest = - new ContentCachingRequestWrapper(request); - ContentCachingResponseWrapper cacheableResponse = - new ContentCachingResponseWrapper(response); + payloadDataBuilder.userData(userData); - ServletDataPayloadAdapter payload = - new ServletDataPayloadAdapter(cacheableRequest, cacheableResponse); - UserData userData = userDataCollector.collect(payload); + ApiCallLogData apiCallLogData = requestDataCollector.collect(payload); + payloadDataBuilder.apiCallLogData(apiCallLogData); + payloadDataBuilder.responseEndDateTime(new Date()); + + PayloadData payloadData = payloadDataBuilder.build(); + response.copyBodyToResponse(); - requestDataCollector.collect(payload, userData); - chain.doFilter(cacheableRequest, cacheableResponse); + CompletableFuture.runAsync(() -> { + payloadDispatcher.dispatch(payloadData); + }); } } catch (Exception e){ log.error("Error occurred while processing request by readme metrics-sdk: {}", e.getMessage()); - } finally { - chain.doFilter(req, resp); } } diff --git a/packages/java/readme-metrics-spring-boot-starter/src/main/java/com/readme/starter/datacollection/ServletDataPayloadAdapter.java b/packages/java/readme-metrics-spring-boot-starter/src/main/java/com/readme/starter/datacollection/ServletDataPayloadAdapter.java index ff86c21290..bc5b675fb6 100644 --- a/packages/java/readme-metrics-spring-boot-starter/src/main/java/com/readme/starter/datacollection/ServletDataPayloadAdapter.java +++ b/packages/java/readme-metrics-spring-boot-starter/src/main/java/com/readme/starter/datacollection/ServletDataPayloadAdapter.java @@ -6,8 +6,10 @@ import jakarta.servlet.http.HttpServletRequest; import lombok.extern.slf4j.Slf4j; +import org.springframework.http.HttpStatus; +import org.springframework.web.util.ContentCachingRequestWrapper; +import org.springframework.web.util.ContentCachingResponseWrapper; -import java.io.IOException; import java.util.*; import java.util.stream.Collectors; @@ -15,8 +17,8 @@ @AllArgsConstructor public class ServletDataPayloadAdapter implements DataPayloadAdapter { - private HttpServletRequest request; - private HttpServletResponse response; + private ContentCachingRequestWrapper request; + private ContentCachingResponseWrapper response; //TODO Do I need a separate method to get request parameters? @@ -33,15 +35,38 @@ public String getRequestContentType() { @Override public String getRequestBody() { try { - return request.getReader() - .lines() - .collect(Collectors.joining(System.lineSeparator())); - } catch (IOException e) { + return request.getContentAsString(); + } catch (Exception e) { log.error("Error when trying to get request body: {}", e.getMessage()); } return ""; } + @Override + public String getAddress() { + return request.getRemoteAddr(); + } + + @Override + public String getProtocol() { + return request.getProtocol(); + } + + @Override + public String getUrl() { + return request.getRequestURL().toString(); + } + + @Override + public Map getRequestParameters() { + return request.getParameterMap() + .entrySet() + .stream() + .collect(Collectors + .toMap(Map.Entry::getKey, + e -> String.join("", e.getValue()))); + } + /** * Retrieves all request headers from the {@link HttpServletRequest} and returns them * as a map where the header names are normalized to lowercase. @@ -86,7 +111,24 @@ public Map getRequestHeaders() { @Override public String getResponseBody() { - throw new UnsupportedOperationException("Not implemented yet"); + try { + byte[] contentAsByteArray = response.getContentAsByteArray(); + return new String(contentAsByteArray); + } catch (Exception e) { + log.error("Error when trying to get response body: {}", e.getMessage()); + } + return ""; + } + + @Override + public int getStatusCode() { + return response.getStatus(); + } + + @Override + public String getStatusMessage() { + HttpStatus httpStatus = HttpStatus.resolve(response.getStatus()); + return httpStatus != null ? httpStatus.getReasonPhrase() : ""; } /** diff --git a/packages/java/readme-metrics-spring-boot-starter/src/main/java/com/readme/starter/datacollection/ServletRequestDataCollector.java b/packages/java/readme-metrics-spring-boot-starter/src/main/java/com/readme/starter/datacollection/ServletRequestDataCollector.java index 07f9def307..3cff2438c5 100644 --- a/packages/java/readme-metrics-spring-boot-starter/src/main/java/com/readme/starter/datacollection/ServletRequestDataCollector.java +++ b/packages/java/readme-metrics-spring-boot-starter/src/main/java/com/readme/starter/datacollection/ServletRequestDataCollector.java @@ -1,24 +1,49 @@ package com.readme.starter.datacollection; -import com.readme.dataextraction.RequestDataCollector; -import com.readme.domain.UserData; -import com.readme.starter.config.ReadmeConfigurationProperties; + +import com.readme.dataextraction.payload.requestresponse.ApiCallLogData; +import com.readme.dataextraction.payload.requestresponse.RequestData; +import com.readme.dataextraction.payload.requestresponse.RequestDataCollector; +import com.readme.dataextraction.payload.requestresponse.ResponseData; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; + @Slf4j @AllArgsConstructor @Component public class ServletRequestDataCollector implements RequestDataCollector { - private ReadmeConfigurationProperties readmeProperties; - @Override - public void collect(ServletDataPayloadAdapter dataPayload, UserData userData) { - String readmeAPIKey = readmeProperties.getReadmeApiKey(); + public ApiCallLogData collect(ServletDataPayloadAdapter dataPayload) { + return ApiCallLogData + .builder() + .requestData(buildRequestData(dataPayload)) + .responseData(buildResponseData(dataPayload)) + .build(); + } - log.info(">>>>>>>> Sending data to the server with key {}", readmeAPIKey); - log.info(">>>>>>>> and user data: {}", userData); + private RequestData buildRequestData(ServletDataPayloadAdapter dataPayload) { + return RequestData.builder() + .url(dataPayload.getUrl()) + .method(dataPayload.getRequestMethod()) + .protocol(dataPayload.getProtocol()) + .remoteAddress(dataPayload.getAddress()) + .headers(dataPayload.getRequestHeaders()) + .body(dataPayload.getRequestBody()) + .routePath(dataPayload.getUrl()) + .requestParameters(dataPayload.getRequestParameters()) + .build(); } + + private ResponseData buildResponseData(ServletDataPayloadAdapter dataPayload) { + return ResponseData.builder() + .headers(dataPayload.getResponseHeaders()) + .body(dataPayload.getResponseBody()) + .statusCode(dataPayload.getStatusCode()) + .statusMessage(dataPayload.getStatusMessage()) + .build(); + } + } diff --git a/packages/java/readme-metrics/src/main/java/com/readme/config/FieldMapping.java b/packages/java/readme-metrics-spring-boot-starter/src/main/java/com/readme/starter/datacollection/userinfo/FieldMapping.java similarity index 90% rename from packages/java/readme-metrics/src/main/java/com/readme/config/FieldMapping.java rename to packages/java/readme-metrics-spring-boot-starter/src/main/java/com/readme/starter/datacollection/userinfo/FieldMapping.java index 1fdd89d675..d0d2a616ea 100644 --- a/packages/java/readme-metrics/src/main/java/com/readme/config/FieldMapping.java +++ b/packages/java/readme-metrics-spring-boot-starter/src/main/java/com/readme/starter/datacollection/userinfo/FieldMapping.java @@ -1,4 +1,4 @@ -package com.readme.config; +package com.readme.starter.datacollection.userinfo; import lombok.AllArgsConstructor; import lombok.Data; diff --git a/packages/java/readme-metrics-spring-boot-starter/src/main/java/com/readme/starter/datacollection/userinfo/ServletUserDataCollector.java b/packages/java/readme-metrics-spring-boot-starter/src/main/java/com/readme/starter/datacollection/userinfo/ServletUserDataCollector.java index 409fedf80c..d1019374b0 100644 --- a/packages/java/readme-metrics-spring-boot-starter/src/main/java/com/readme/starter/datacollection/userinfo/ServletUserDataCollector.java +++ b/packages/java/readme-metrics-spring-boot-starter/src/main/java/com/readme/starter/datacollection/userinfo/ServletUserDataCollector.java @@ -1,29 +1,26 @@ package com.readme.starter.datacollection.userinfo; -import com.readme.config.FieldMapping; -import com.readme.dataextraction.UserDataCollector; -import com.readme.dataextraction.UserDataExtractor; -import com.readme.dataextraction.UserDataSource; -import com.readme.domain.UserData; + +import com.readme.dataextraction.payload.user.UserData; +import com.readme.dataextraction.payload.user.UserDataCollector; +import com.readme.dataextraction.payload.user.UserDataSource; import com.readme.starter.config.UserDataProperties; import com.readme.starter.datacollection.ServletDataPayloadAdapter; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.context.annotation.DependsOn; -import org.springframework.stereotype.Component; + /** * Responsible for selecting the appropriate {@link UserDataExtractor} * based on the provided configuration in the application settings. - * + *

* This class acts as a bridge between YAML/Properties configuration and * the corresponding strategy for extracting user-related data * (e.g., from JSON body, headers, or JWT tokens). - * + *

* Ensures flexibility and proper encapsulation of the strategy selection logic. */ -@Component @AllArgsConstructor @Slf4j public class ServletUserDataCollector implements UserDataCollector { @@ -79,7 +76,7 @@ private String extractFieldValue(ServletDataPayloadAdapter payload, FieldMapping String fieldName = fieldMapping.getFieldName().toLowerCase(); String fieldValue = extractionService.extractFromHeader(payload, fieldName); - validate(payload, fieldValue); + validate(fieldValue); return fieldValue; } @@ -87,7 +84,7 @@ private String extractFieldValue(ServletDataPayloadAdapter payload, FieldMapping String fieldName = fieldMapping.getFieldName().toLowerCase(); String fieldValue = extractionService.extractFromBody(payload, fieldName); - validate(payload, fieldValue); + validate(fieldValue); return fieldValue; } @@ -95,7 +92,7 @@ private String extractFieldValue(ServletDataPayloadAdapter payload, FieldMapping String fieldName = fieldMapping.getFieldName().toLowerCase(); String fieldValue = extractionService.extractFromJwt(payload, fieldName); - validate(payload, fieldValue); + validate(fieldValue); return fieldValue; } @@ -105,10 +102,9 @@ private String extractFieldValue(ServletDataPayloadAdapter payload, FieldMapping return ""; } - private void validate(ServletDataPayloadAdapter payload, String fieldName) { - String fieldValue = extractionService.extractFromHeader(payload, fieldName); + private void validate(String fieldValue) { if (fieldValue == null || fieldValue.isEmpty()) { - log.error("The {} extraction is not configured properly. The value is empty", fieldName); + log.error("The {} extraction is not configured properly. The value is empty", fieldValue); } } diff --git a/packages/java/readme-metrics-spring-boot-starter/src/main/java/com/readme/starter/datacollection/userinfo/ServletUserDataExtractor.java b/packages/java/readme-metrics-spring-boot-starter/src/main/java/com/readme/starter/datacollection/userinfo/ServletUserDataExtractor.java index 0818af747c..a8bca9e99d 100644 --- a/packages/java/readme-metrics-spring-boot-starter/src/main/java/com/readme/starter/datacollection/userinfo/ServletUserDataExtractor.java +++ b/packages/java/readme-metrics-spring-boot-starter/src/main/java/com/readme/starter/datacollection/userinfo/ServletUserDataExtractor.java @@ -3,10 +3,10 @@ import com.auth0.jwt.interfaces.DecodedJWT; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; -import com.readme.dataextraction.UserDataExtractor; import com.readme.starter.datacollection.ServletDataPayloadAdapter; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.http.HttpMethod; import org.springframework.stereotype.Component; import com.auth0.jwt.JWT; @@ -21,8 +21,8 @@ public class ServletUserDataExtractor implements UserDataExtractor requestHeaders = payload.getRequestHeaders(); @@ -35,19 +35,23 @@ public String extractFromHeader(ServletDataPayloadAdapter payload, String fieldN @Override public String extractFromBody(ServletDataPayloadAdapter payload, String fieldPath) { - if (payload.getRequestContentType().equalsIgnoreCase("application/json")) { - String requestBody = payload.getRequestBody(); - try { - JsonNode currentNode = objectMapper.readTree(requestBody); - if (!fieldPath.startsWith("/")) { - fieldPath = "/" + fieldPath; + if (!payload.getRequestMethod().equalsIgnoreCase(HttpMethod.GET.name())) { + if (payload.getRequestContentType().equalsIgnoreCase("application/json")) { + String requestBody = payload.getRequestBody(); + try { + JsonNode currentNode = objectMapper.readTree(requestBody); + if (!fieldPath.startsWith("/")) { + fieldPath = "/" + fieldPath; + } + return currentNode.at(fieldPath).asText(); + } catch (Exception e) { + log.error("Error when reading the user data from JSON body: {}", e.getMessage()); } - return currentNode.at(fieldPath).asText(); - } catch (Exception e) { - log.error("Error when reading the user data from JSON body: {}", e.getMessage()); } + log.error("The provided body content type {} is not supported to get user data.", payload.getRequestContentType()); + return ""; } - log.error("The provided body content type {} is not supported to get user data.", payload.getRequestContentType()); + log.error("The HTTP method {} is not supported to get user data from body.", payload.getRequestMethod()); return ""; } diff --git a/packages/java/readme-metrics/src/main/java/com/readme/dataextraction/UserDataExtractor.java b/packages/java/readme-metrics-spring-boot-starter/src/main/java/com/readme/starter/datacollection/userinfo/UserDataExtractor.java similarity index 96% rename from packages/java/readme-metrics/src/main/java/com/readme/dataextraction/UserDataExtractor.java rename to packages/java/readme-metrics-spring-boot-starter/src/main/java/com/readme/starter/datacollection/userinfo/UserDataExtractor.java index ccf6746d89..413f878b19 100644 --- a/packages/java/readme-metrics/src/main/java/com/readme/dataextraction/UserDataExtractor.java +++ b/packages/java/readme-metrics-spring-boot-starter/src/main/java/com/readme/starter/datacollection/userinfo/UserDataExtractor.java @@ -1,4 +1,4 @@ -package com.readme.dataextraction; +package com.readme.starter.datacollection.userinfo; /** * Defines a contract for extracting user-related data from an incoming request. diff --git a/packages/java/readme-metrics-spring-boot-starter/src/test/java/com/readme/starter/datacollection/DataCollectionFilterTest.java b/packages/java/readme-metrics-spring-boot-starter/src/test/java/com/readme/starter/datacollection/DataCollectionFilterTest.java new file mode 100644 index 0000000000..0e09979d3f --- /dev/null +++ b/packages/java/readme-metrics-spring-boot-starter/src/test/java/com/readme/starter/datacollection/DataCollectionFilterTest.java @@ -0,0 +1,143 @@ +package com.readme.starter.datacollection; + +import com.readme.dataextraction.payload.requestresponse.RequestDataCollector; +import com.readme.dataextraction.payload.user.UserData; +import com.readme.dataextraction.payload.user.UserDataCollector; +import com.readme.datatransfer.PayloadDataDispatcher; +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.*; +import org.springframework.web.util.ContentCachingRequestWrapper; +import org.springframework.web.util.ContentCachingResponseWrapper; + +import java.io.IOException; + +import static org.mockito.Mockito.*; + +class DataCollectionFilterTest { + + @Mock + private RequestDataCollector requestDataCollector; + + @Mock + private UserDataCollector userDataCollector; + + @Mock + private HttpServletRequest request; + + @Mock + private HttpServletResponse response; + + @Mock + private FilterChain chain; + + @Mock + private PayloadDataDispatcher payloadDataDispatcher; + + private DataCollectionFilter filter; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + filter = new DataCollectionFilter(userDataCollector, requestDataCollector, payloadDataDispatcher); + } + + @Test + void doFilter_OptionsRequest_ShouldPassThroughWithoutProcessing() throws Exception { + when(request.getMethod()).thenReturn("OPTIONS"); + + filter.doFilter(request, response, chain); + + verify(chain).doFilter(request, response); + verifyNoInteractions(requestDataCollector, userDataCollector); + } + + + @Test + void doFilter_GetRequest_ShouldProcessAndCollectData() throws Exception { + when(request.getMethod()).thenReturn("GET"); + testChain(); + } + + @Test + void doFilter_PutRequest_ShouldProcessAndCollectData() throws Exception { + when(request.getMethod()).thenReturn("PUT"); + testChain(); + } + + @Test + void doFilter_PostRequest_ShouldProcessAndCollectData() throws Exception { + when(request.getMethod()).thenReturn("POST"); + testChain(); + } + + @Test + void doFilter_PatchRequest_ShouldProcessAndCollectData() throws Exception { + when(request.getMethod()).thenReturn("PATCH"); + testChain(); + } + + @Test + void doFilter_DeleteRequest_ShouldProcessAndCollectData() throws Exception { + when(request.getMethod()).thenReturn("DELETE"); + testChain(); + } + + + private void testChain() throws IOException, ServletException { + UserData userData = getMockedUserData(); + when(userDataCollector.collect(any(ServletDataPayloadAdapter.class))).thenReturn(userData); + + filter.doFilter(request, response, chain); + + verify(chain).doFilter(any(ContentCachingRequestWrapper.class), any(ContentCachingResponseWrapper.class)); + + ArgumentCaptor payloadCaptor = ArgumentCaptor.forClass(ServletDataPayloadAdapter.class); + + verify(userDataCollector).collect(payloadCaptor.capture()); + verify(requestDataCollector).collect(eq(payloadCaptor.getValue())); + + // TODO Verify response body is copied + // verify(response).getOutputStream(); + } + + private static UserData getMockedUserData() { + return UserData.builder() + .apiKey("Owl") + .email("owl@birdfactory.abc") + .label("owl-label") + .build(); + } + + @Test + void doFilter_UserDataCollectorThrowsException_ShouldHandleExceptionAndContinueFlow() throws Exception { + when(request.getMethod()).thenReturn("POST"); + when(userDataCollector.collect(any(ServletDataPayloadAdapter.class))) + .thenThrow(new RuntimeException("Error in UserDataCollector")); + + filter.doFilter(request, response, chain); + + verify(chain).doFilter(any(ContentCachingRequestWrapper.class), any(ContentCachingResponseWrapper.class)); + verify(requestDataCollector, never()).collect(any()); + verifyNoMoreInteractions(requestDataCollector); + } + + @Test + void doFilter_RequestDataCollectorThrowsException_ShouldHandleExceptionAndContinueFlow() throws Exception { + when(request.getMethod()).thenReturn("POST"); + UserData userData = getMockedUserData(); + + when(userDataCollector.collect(any(ServletDataPayloadAdapter.class))).thenReturn(userData); + doThrow(new RuntimeException("Error in RequestDataCollector")) + .when(requestDataCollector).collect(any()); + + filter.doFilter(request, response, chain); + + verify(chain).doFilter(any(ContentCachingRequestWrapper.class), any(ContentCachingResponseWrapper.class)); + verify(userDataCollector).collect(any(ServletDataPayloadAdapter.class)); + } +} \ No newline at end of file diff --git a/packages/java/readme-metrics-spring-boot-starter/src/test/java/com/readme/starter/datacollection/ServletDataPayloadAdapterTest.java b/packages/java/readme-metrics-spring-boot-starter/src/test/java/com/readme/starter/datacollection/ServletDataPayloadAdapterTest.java index 6b27916da0..9cfe185e9e 100644 --- a/packages/java/readme-metrics-spring-boot-starter/src/test/java/com/readme/starter/datacollection/ServletDataPayloadAdapterTest.java +++ b/packages/java/readme-metrics-spring-boot-starter/src/test/java/com/readme/starter/datacollection/ServletDataPayloadAdapterTest.java @@ -1,15 +1,13 @@ package com.readme.starter.datacollection; -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import org.springframework.web.util.ContentCachingRequestWrapper; +import org.springframework.web.util.ContentCachingResponseWrapper; -import java.io.BufferedReader; import java.io.IOException; -import java.io.StringReader; import java.util.Collections; import java.util.Enumeration; import java.util.List; @@ -18,13 +16,14 @@ import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; + class ServletDataPayloadAdapterTest { @Mock - private HttpServletRequest requestMock; + private ContentCachingRequestWrapper requestMock; @Mock - private HttpServletResponse responseMock; + private ContentCachingResponseWrapper responseMock; private ServletDataPayloadAdapter adapter; @@ -81,20 +80,12 @@ void getRequestContentType_HappyPath_ReturnsContentType() { @Test void getRequestBody_HappyPath_ReturnsRequestBody() throws IOException { String requestBody = "{\"bird\": \"Owl\"}"; - BufferedReader bufferedReader = new BufferedReader(new StringReader(requestBody)); - when(requestMock.getReader()).thenReturn(bufferedReader); + when(requestMock.getContentAsString()).thenReturn(requestBody); String result = adapter.getRequestBody(); assertEquals(requestBody, result); } - @Test - void getRequestBody_WhenIOExceptionOccurs_ReturnsEmptyString() throws IOException { - when(requestMock.getReader()).thenThrow(new IOException("Failed to read request")); - String result = adapter.getRequestBody(); - - assertEquals("", result); - } // --------------------------- RESPONSE -------------------------------- @Test @@ -113,16 +104,6 @@ void getResponseHeaders_HappyPath_ReturnsAllHeaders() { assertEquals("parrot@birdfact0ry.abc", headers.get(userIdHeader)); } - // TODO implement this, once it fails - @Test - void getResponseBody_ThrowsUnsupportedOperationException() { - UnsupportedOperationException exception = assertThrows( - UnsupportedOperationException.class, - adapter::getResponseBody - ); - - assertEquals("Not implemented yet", exception.getMessage()); - } @Test void getResponseHeaders_NoHeaders_ReturnsEmptyMap() { diff --git a/packages/java/readme-metrics-spring-boot-starter/src/test/java/com/readme/starter/datacollection/ServletRequestDataCollectorTest.java b/packages/java/readme-metrics-spring-boot-starter/src/test/java/com/readme/starter/datacollection/ServletRequestDataCollectorTest.java new file mode 100644 index 0000000000..ee6966b341 --- /dev/null +++ b/packages/java/readme-metrics-spring-boot-starter/src/test/java/com/readme/starter/datacollection/ServletRequestDataCollectorTest.java @@ -0,0 +1,106 @@ +package com.readme.starter.datacollection; + +import com.readme.dataextraction.payload.requestresponse.ApiCallLogData; +import com.readme.dataextraction.payload.requestresponse.RequestData; +import com.readme.dataextraction.payload.requestresponse.ResponseData; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.web.util.ContentCachingRequestWrapper; +import org.springframework.web.util.ContentCachingResponseWrapper; + +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +@ExtendWith(SpringExtension.class) +@ContextConfiguration(classes = {ServletRequestDataCollector.class}) +class ServletRequestDataCollectorTest { + + @Autowired + private ServletRequestDataCollector servletRequestDataCollector; + + @MockBean + private ContentCachingRequestWrapper requestMock; + + @MockBean + private ContentCachingResponseWrapper responseMock; + + @Test + void collect_ShouldReturnApiCallLogData_WithCorrectRequestAndResponseData() { + ServletDataPayloadAdapter dataPayload = createStubServletDataPayloadAdapter(); + + ApiCallLogData result = servletRequestDataCollector.collect(dataPayload); + + assertNotNull(result); + assertNotNull(result.getRequestData()); + assertNotNull(result.getResponseData()); + + RequestData requestData = result.getRequestData(); + assertEquals("http://owl-bowl.abc", requestData.getUrl()); + assertEquals("GET", requestData.getMethod()); + assertEquals("HTTP/1.1", requestData.getProtocol()); + assertEquals("127.0.0.1", requestData.getRemoteAddress()); + assertEquals("{}", requestData.getBody()); + assertEquals("http://owl-bowl.abc", requestData.getRoutePath()); + assertEquals(Map.of("param1", "value1"), requestData.getRequestParameters()); + assertEquals(Map.of("authorization", "Bearer token"), requestData.getHeaders()); + + ResponseData responseData = result.getResponseData(); + assertEquals("{\"status\":\"ok\"}", responseData.getBody()); + assertEquals(200, responseData.getStatusCode()); + assertEquals("OK", responseData.getStatusMessage()); + assertEquals(Map.of("content-type", "application/json"), responseData.getHeaders()); + } + + @Test + void collect_ShouldHandleNullHeaders() { + when(requestMock.getHeaderNames()).thenReturn(null); + when(responseMock.getHeaderNames()).thenReturn(null); + + ServletDataPayloadAdapter dataPayloadAdapter = mock(ServletDataPayloadAdapter.class); + ApiCallLogData result = servletRequestDataCollector.collect(dataPayloadAdapter); + + assertNotNull(result); + assertTrue(result.getRequestData().getHeaders().isEmpty()); + assertTrue(result.getResponseData().getHeaders().isEmpty()); + } + + @Test + void collect_ShouldHandleExceptionDuringRequestBodyRead() { + ServletDataPayloadAdapter dataPayload = createStubServletDataPayloadAdapter(); + when(requestMock.getContentAsString()).thenThrow(new RuntimeException("Test exception")); + + ApiCallLogData result = servletRequestDataCollector.collect(dataPayload); + + assertNotNull(result); + assertEquals("", result.getRequestData().getBody()); + } + + private ServletDataPayloadAdapter createStubServletDataPayloadAdapter() { + when(requestMock.getMethod()).thenReturn("GET"); + when(requestMock.getContentType()).thenReturn("application/json"); + when(requestMock.getContentAsString()).thenReturn("{}"); + when(requestMock.getRemoteAddr()).thenReturn("127.0.0.1"); + when(requestMock.getProtocol()).thenReturn("HTTP/1.1"); + when(requestMock.getRequestURL()).thenReturn(new StringBuffer("http://owl-bowl.abc")); + when(requestMock.getParameterMap()).thenReturn(Map.of("param1", new String[]{"value1"})); + when(requestMock.getHeaderNames()).thenReturn(Collections.enumeration(List.of("authorization"))); + when(requestMock.getHeader("authorization")).thenReturn("Bearer token"); + + when(responseMock.getContentAsByteArray()).thenReturn("{\"status\":\"ok\"}".getBytes()); + when(responseMock.getStatus()).thenReturn(200); + when(responseMock.getHeaderNames()).thenReturn(Set.of("content-type")); + when(responseMock.getHeader("content-type")).thenReturn("application/json"); + + return new ServletDataPayloadAdapter(requestMock, responseMock); + } +} \ No newline at end of file diff --git a/packages/java/readme-metrics-spring-boot-starter/src/test/java/com/readme/starter/datacollection/userinfo/ServletUserDataCollectorTest.java b/packages/java/readme-metrics-spring-boot-starter/src/test/java/com/readme/starter/datacollection/userinfo/ServletUserDataCollectorTest.java index 0aa639b1b5..40cf7b0dd8 100644 --- a/packages/java/readme-metrics-spring-boot-starter/src/test/java/com/readme/starter/datacollection/userinfo/ServletUserDataCollectorTest.java +++ b/packages/java/readme-metrics-spring-boot-starter/src/test/java/com/readme/starter/datacollection/userinfo/ServletUserDataCollectorTest.java @@ -1,9 +1,7 @@ package com.readme.starter.datacollection.userinfo; -import com.readme.config.FieldMapping; -import com.readme.dataextraction.UserDataExtractor; -import com.readme.dataextraction.UserDataSource; -import com.readme.domain.UserData; +import com.readme.dataextraction.payload.user.UserData; +import com.readme.dataextraction.payload.user.UserDataSource; import com.readme.starter.config.UserDataProperties; import com.readme.starter.datacollection.ServletDataPayloadAdapter; import org.junit.jupiter.api.BeforeEach; diff --git a/packages/java/readme-metrics-spring-boot-starter/src/test/java/com/readme/starter/datacollection/userinfo/ServletUserDataExtractorTest.java b/packages/java/readme-metrics-spring-boot-starter/src/test/java/com/readme/starter/datacollection/userinfo/ServletUserDataExtractorTest.java index 4ee4b7505b..d7469f23b9 100644 --- a/packages/java/readme-metrics-spring-boot-starter/src/test/java/com/readme/starter/datacollection/userinfo/ServletUserDataExtractorTest.java +++ b/packages/java/readme-metrics-spring-boot-starter/src/test/java/com/readme/starter/datacollection/userinfo/ServletUserDataExtractorTest.java @@ -10,6 +10,7 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.http.HttpMethod; import java.io.IOException; import java.security.KeyPair; @@ -20,6 +21,7 @@ import java.util.Map; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.*; @ExtendWith(MockitoExtension.class) class ServletUserDataExtractorTest { @@ -131,6 +133,7 @@ void extractFromBody_happyPath() throws IOException { String body = "{\"" + fieldName + "\":\"" + expectedValue + "\",\"anotherField\":\"anotherValue\"}"; Mockito.when(payload.getRequestBody()).thenReturn(body); + Mockito.when(payload.getRequestMethod()).thenReturn("POST"); Mockito.when(payload.getRequestContentType()).thenReturn("application/json"); String result = extractor.extractFromBody(payload, fieldName); @@ -144,6 +147,7 @@ void extractFromBody_fieldNotFound() { String body = "{\"anotherField\":\"anotherValue\"}"; Mockito.when(payload.getRequestBody()).thenReturn(body); + Mockito.when(payload.getRequestMethod()).thenReturn("POST"); Mockito.when(payload.getRequestContentType()).thenReturn("application/json"); String result = extractor.extractFromBody(payload, fieldName); @@ -156,12 +160,22 @@ void extractFromBody_invalidJson() { String expectedValue = ""; Mockito.when(payload.getRequestBody()).thenReturn(body); + Mockito.when(payload.getRequestMethod()).thenReturn("POST"); Mockito.when(payload.getRequestContentType()).thenReturn("application/json"); String result = extractor.extractFromBody(payload, "fieldName"); assertEquals(expectedValue, result); } + @Test + void extractFromBody_HttpMethodGet_ReturnsEmptyString() { + Mockito.when(payload.getRequestMethod()).thenReturn("GET"); + + String result = extractor.extractFromBody(payload, "/fieldName"); + + assertEquals("", result); + verifyNoMoreInteractions(payload); + } private Algorithm createSigningKeyPair() throws NoSuchAlgorithmException { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); diff --git a/packages/java/readme-metrics/.gitignore b/packages/java/readme-metrics/.gitignore index 429723c57c..8aca59ce68 100644 --- a/packages/java/readme-metrics/.gitignore +++ b/packages/java/readme-metrics/.gitignore @@ -5,10 +5,7 @@ target/ ../* ### IntelliJ IDEA ### -.idea/modules.xml -.idea/jarRepositories.xml -.idea/compiler.xml -.idea/libraries/ +.idea/ *.iws *.iml *.ipr @@ -26,4 +23,5 @@ target/ .vscode/ ### Mac OS ### -*.DS_Store \ No newline at end of file +*.DS_Store +/.idea/ diff --git a/packages/java/readme-metrics/.idea/.gitignore b/packages/java/readme-metrics/.idea/.gitignore deleted file mode 100644 index 13566b81b0..0000000000 --- a/packages/java/readme-metrics/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/packages/java/readme-metrics/.idea/encodings.xml b/packages/java/readme-metrics/.idea/encodings.xml deleted file mode 100644 index aa00ffab78..0000000000 --- a/packages/java/readme-metrics/.idea/encodings.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - \ No newline at end of file diff --git a/packages/java/readme-metrics/.idea/misc.xml b/packages/java/readme-metrics/.idea/misc.xml deleted file mode 100644 index fdc35ea816..0000000000 --- a/packages/java/readme-metrics/.idea/misc.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - - - \ No newline at end of file diff --git a/packages/java/readme-metrics/.idea/uiDesigner.xml b/packages/java/readme-metrics/.idea/uiDesigner.xml deleted file mode 100644 index 2b63946d5b..0000000000 --- a/packages/java/readme-metrics/.idea/uiDesigner.xml +++ /dev/null @@ -1,124 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/packages/java/readme-metrics/.idea/vcs.xml b/packages/java/readme-metrics/.idea/vcs.xml deleted file mode 100644 index c2365ab11f..0000000000 --- a/packages/java/readme-metrics/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/packages/java/readme-metrics/pom.xml b/packages/java/readme-metrics/pom.xml index a5ffdb85bc..9e80441f8d 100644 --- a/packages/java/readme-metrics/pom.xml +++ b/packages/java/readme-metrics/pom.xml @@ -5,15 +5,15 @@ 4.0.0 com.readme - readme-metrics + metrics-core 0.0.1 - 8 + 11 UTF-8 1.18.34 - 5.11.0 - 4.13.2 + 5.14.2 + 5.11.4 4.12.0 @@ -28,6 +28,24 @@ okhttp ${okhttp.version} + + + org.slf4j + slf4j-api + 1.7.36 + + + com.fasterxml.jackson.core + jackson-annotations + 2.18.2 + + + com.fasterxml.jackson.core + jackson-databind + 2.18.2 + + + org.mockito mockito-core @@ -35,16 +53,16 @@ test - junit - junit - ${junit.version} + org.mockito + mockito-junit-jupiter + ${mockito-core.version} test - - org.slf4j - slf4j-api - 1.7.26 + org.junit.jupiter + junit-jupiter-engine + ${junit.version} + test @@ -58,6 +76,11 @@ ${maven.compiler.release} + + org.apache.maven.plugins + maven-surefire-plugin + 3.1.2 + \ No newline at end of file diff --git a/packages/java/readme-metrics/src/main/java/com/readme/ReadmeMetrics.java b/packages/java/readme-metrics/src/main/java/com/readme/ReadmeMetrics.java deleted file mode 100644 index e91ee081a3..0000000000 --- a/packages/java/readme-metrics/src/main/java/com/readme/ReadmeMetrics.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.readme; - -import com.readme.config.CoreConfig; -import com.readme.datatransfer.DataSender; -import com.readme.datatransfer.HttpDataSender; -import com.readme.domain.RequestPayload; -import okhttp3.OkHttpClient; - -public class ReadmeMetrics { - - //TODO Don't forget to refactor all this mess - public static void main(String[] args) { - - OkHttpClient okHttpClient = new OkHttpClient(); - - CoreConfig coreConfig = CoreConfig.builder() - .readmeAPIKey("apikey") - .build(); - - DataSender d = new HttpDataSender(okHttpClient, coreConfig); - - //TODO List of work to build the body dinamically: - // 1. Generate the logID - // 2. Initialize startTime - RequestPayload r = RequestPayload.builder() - .body("[{\"_id\":\"ff783c0a-d49c-4930-8bbd-2e8937b35bad\",\"_version\":3,\"group\":{\n" + - " \"id\":\"sha512-u2GbQ83jIqNa+a8v110+8IDztQQr4joL1xSE+wFH51zSOA1qQKPwOC8t2n2LWJQA1mX4ZLZ45SEokITzLed/ow==?-key\",\n" + - " \"label\":\"Owlbertic\",\"email\":\"owlbertic@example.com\"},\n" + - " \"clientIPAddress\":\"127.0.0.1\",\"development\":false,\"request\":{\n" + - " \"log\":{\"version\":\"1.2\",\"creator\":{\"name\":\"readme-metrics (JAVA)\",\"version\":\"7.0.0-alpha.7\",\"comment\":\"arm64-darwin24.1.0/22.9.0\"},\n" + - " \"entries\":[{\"pageref\":\"http://127.0.0.1/\",\"startedDateTime\":\"2024-11-07T22:32:27.863Z\",\"time\":4318,\"request\":{\n" + - " \"method\":\"GET\",\"url\":\"http://127.0.0.1:8000/\",\"httpVersion\":\"HTTP/1.1\",\"headers\":[\n" + - " {\"name\":\"host\",\"value\":\"127.0.0.1:8000\"},\n" + - " {\"name\":\"connection\",\"value\":\"keep-alive\"},\n" + - " {\"name\":\"sec-ch-ua\",\"value\":\"\\\"Chromium\\\";v=\\\"130\\\", \\\"Google Chrome\\\";v=\\\"130\\\", \\\"Not?A_Brand\\\";v=\\\"99\\\"\"},\n" + - " {\"name\":\"sec-ch-ua-mobile\",\"value\":\"?0\"},{\"name\":\"sec-ch-ua-platform\",\"value\":\"\\\"macOS\\\"\"},\n" + - " {\"name\":\"accept-language\",\"value\":\"uk-UA,uk;q=0.9\"},\n" + - " {\"name\":\"upgrade-insecure-requests\",\"value\":\"1\"},\n" + - " {\"name\":\"user-agent\",\"value\":\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36\"},\n" + - " {\"name\":\"accept\",\"value\":\"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7\"},\n" + - " {\"name\":\"sec-fetch-site\",\"value\":\"JAVA\"},\n" + - " {\"name\":\"sec-fetch-mode\",\"value\":\"navigate\"},\n" + - " {\"name\":\"sec-fetch-user\",\"value\":\"?1\"},\n" + - " {\"name\":\"sec-fetch-dest\",\"value\":\"document\"},\n" + - " {\"name\":\"accept-encoding\",\"value\":\"gzip, deflate, br, zstd\"},\n" + - " {\"name\":\"if-none-match\",\"value\":\"W/\\\"1a-iEQ9RXvkycqsT4vWvcdHrxZT8OE\\\"\"}],\n" + - " \"queryString\":[],\"cookies\":[],\"headersSize\":-1,\"bodySize\":-1},\n" + - " \"response\":{\"status\":304,\"statusText\":\"Not Modified\",\n" + - " \"headers\":[\n" + - " {\"name\":\"x-powered-by\",\"value\":\"Express\"},\n" + - " {\"name\":\"etag\",\"value\":\"W/\\\"1a-iEQ9RXvkycqsT4vWvcdHrxZT8OE\\\"\"}],\n" + - " \"content\":{\"text\":\"\\\"\\\"\",\"size\":0,\"mimeType\":\"text/plain\"},\n" + - " \"httpVersion\":\"\",\"cookies\":[],\"redirectURL\":\"\",\"headersSize\":0,\"bodySize\":0},\"cache\":{},\"timings\":{\"wait\":0,\"receive\":4318}}]}}}]\n") - .build(); - - System.out.println(d.send(r)); - } - -} \ No newline at end of file diff --git a/packages/java/readme-metrics/src/main/java/com/readme/dataextraction/ApiKeyMasker.java b/packages/java/readme-metrics/src/main/java/com/readme/dataextraction/ApiKeyMasker.java new file mode 100644 index 0000000000..d5e9bcc2f9 --- /dev/null +++ b/packages/java/readme-metrics/src/main/java/com/readme/dataextraction/ApiKeyMasker.java @@ -0,0 +1,26 @@ +package com.readme.dataextraction; + +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Base64; + +public class ApiKeyMasker { + + public static String mask(String apiKey) { + try { + String base64Hash = Base64.getEncoder() + .encodeToString(MessageDigest + .getInstance("SHA-512") + .digest(apiKey.getBytes(StandardCharsets.UTF_8))); + + String last4Digits = apiKey.substring(apiKey.length() - 4); + return "sha512-" + base64Hash + "?" + last4Digits; + } catch (NoSuchAlgorithmException e) { + throw new RuntimeException("SHA-512 algorithm not available", e); + } catch (StringIndexOutOfBoundsException e) { + throw new IllegalArgumentException("API key must be at least 4 characters long", e); + } + } + +} \ No newline at end of file diff --git a/packages/java/readme-metrics/src/main/java/com/readme/dataextraction/DataPayloadAdapter.java b/packages/java/readme-metrics/src/main/java/com/readme/dataextraction/DataPayloadAdapter.java index 789ae3df4f..2bae3bf0a0 100644 --- a/packages/java/readme-metrics/src/main/java/com/readme/dataextraction/DataPayloadAdapter.java +++ b/packages/java/readme-metrics/src/main/java/com/readme/dataextraction/DataPayloadAdapter.java @@ -18,11 +18,16 @@ public interface DataPayloadAdapter { String getRequestMethod(); String getRequestContentType(); - - String getRequestBody(); Map getRequestHeaders(); + String getRequestBody(); + String getAddress(); + String getProtocol(); + String getUrl(); + Map getRequestParameters(); - String getResponseBody(); - Map getResponseHeaders(); + Map getResponseHeaders(); + String getResponseBody(); + int getStatusCode(); + String getStatusMessage(); } diff --git a/packages/java/readme-metrics/src/main/java/com/readme/dataextraction/LogOptions.java b/packages/java/readme-metrics/src/main/java/com/readme/dataextraction/LogOptions.java new file mode 100644 index 0000000000..e5bd959d91 --- /dev/null +++ b/packages/java/readme-metrics/src/main/java/com/readme/dataextraction/LogOptions.java @@ -0,0 +1,16 @@ +package com.readme.dataextraction; + +import lombok.Data; + +import java.util.List; + +@Data +public class LogOptions { + + private List allowlist; + private List denylist; + private Boolean development; + private Boolean fireAndForget; + private Object logger; + +} \ No newline at end of file diff --git a/packages/java/readme-metrics/src/main/java/com/readme/dataextraction/payload/PayloadData.java b/packages/java/readme-metrics/src/main/java/com/readme/dataextraction/payload/PayloadData.java new file mode 100644 index 0000000000..539011c3e1 --- /dev/null +++ b/packages/java/readme-metrics/src/main/java/com/readme/dataextraction/payload/PayloadData.java @@ -0,0 +1,19 @@ +package com.readme.dataextraction.payload; + +import com.readme.dataextraction.payload.requestresponse.ApiCallLogData; +import com.readme.dataextraction.payload.user.UserData; +import lombok.Builder; +import lombok.Value; + +import java.util.Date; + +@Builder +@Value +public class PayloadData { + + UserData userData; + ApiCallLogData apiCallLogData; + Date requestStartedDateTime; + Date responseEndDateTime; + +} diff --git a/packages/java/readme-metrics/src/main/java/com/readme/dataextraction/payload/requestresponse/ApiCallLogData.java b/packages/java/readme-metrics/src/main/java/com/readme/dataextraction/payload/requestresponse/ApiCallLogData.java new file mode 100644 index 0000000000..f07e4cf07a --- /dev/null +++ b/packages/java/readme-metrics/src/main/java/com/readme/dataextraction/payload/requestresponse/ApiCallLogData.java @@ -0,0 +1,15 @@ +package com.readme.dataextraction.payload.requestresponse; + +import lombok.Builder; +import lombok.Value; + +import java.util.Map; + +@Value +@Builder +public class ApiCallLogData { + + RequestData requestData; + ResponseData responseData; + +} diff --git a/packages/java/readme-metrics/src/main/java/com/readme/dataextraction/payload/requestresponse/RequestData.java b/packages/java/readme-metrics/src/main/java/com/readme/dataextraction/payload/requestresponse/RequestData.java new file mode 100644 index 0000000000..4f192ed70b --- /dev/null +++ b/packages/java/readme-metrics/src/main/java/com/readme/dataextraction/payload/requestresponse/RequestData.java @@ -0,0 +1,22 @@ +package com.readme.dataextraction.payload.requestresponse; + +import lombok.Builder; +import lombok.Value; + +import java.util.Map; + +@Value +@Builder +public class RequestData { + + String body; + String routePath; + String remoteAddress; + String protocol; + String url; + String method; + Map headers; + Map requestParameters; + + +} diff --git a/packages/java/readme-metrics/src/main/java/com/readme/dataextraction/RequestDataCollector.java b/packages/java/readme-metrics/src/main/java/com/readme/dataextraction/payload/requestresponse/RequestDataCollector.java similarity index 80% rename from packages/java/readme-metrics/src/main/java/com/readme/dataextraction/RequestDataCollector.java rename to packages/java/readme-metrics/src/main/java/com/readme/dataextraction/payload/requestresponse/RequestDataCollector.java index d8945f3926..d040cec5ea 100644 --- a/packages/java/readme-metrics/src/main/java/com/readme/dataextraction/RequestDataCollector.java +++ b/packages/java/readme-metrics/src/main/java/com/readme/dataextraction/payload/requestresponse/RequestDataCollector.java @@ -1,6 +1,4 @@ -package com.readme.dataextraction; - -import com.readme.domain.UserData; +package com.readme.dataextraction.payload.requestresponse; /** * A generic interface for collecting metrics from various frameworks or APIs. @@ -19,10 +17,10 @@ public interface RequestDataCollector { /** - *

`collect` method is intended to be implemented to extract, process, and - * send metrics data from the given input object. The exact implementation + *

`collect` method is intended to be implemented to extract and process + * metrics data from the given input object. The exact implementation * will vary depending on the framework or API used.

*/ - void collect(T t, UserData userData); + ApiCallLogData collect(T t); } diff --git a/packages/java/readme-metrics/src/main/java/com/readme/dataextraction/payload/requestresponse/ResponseData.java b/packages/java/readme-metrics/src/main/java/com/readme/dataextraction/payload/requestresponse/ResponseData.java new file mode 100644 index 0000000000..f86bd02275 --- /dev/null +++ b/packages/java/readme-metrics/src/main/java/com/readme/dataextraction/payload/requestresponse/ResponseData.java @@ -0,0 +1,18 @@ +package com.readme.dataextraction.payload.requestresponse; + +import lombok.Builder; +import lombok.Value; + +import java.util.Map; + +@Value +@Builder +public class ResponseData { + + String body; + Map headers; + int statusCode; + String statusMessage; + + +} diff --git a/packages/java/readme-metrics/src/main/java/com/readme/domain/UserData.java b/packages/java/readme-metrics/src/main/java/com/readme/dataextraction/payload/user/UserData.java similarity index 75% rename from packages/java/readme-metrics/src/main/java/com/readme/domain/UserData.java rename to packages/java/readme-metrics/src/main/java/com/readme/dataextraction/payload/user/UserData.java index 62a3818e00..eadf78c8b7 100644 --- a/packages/java/readme-metrics/src/main/java/com/readme/domain/UserData.java +++ b/packages/java/readme-metrics/src/main/java/com/readme/dataextraction/payload/user/UserData.java @@ -1,4 +1,4 @@ -package com.readme.domain; +package com.readme.dataextraction.payload.user; import lombok.Builder; import lombok.Value; diff --git a/packages/java/readme-metrics/src/main/java/com/readme/dataextraction/UserDataCollector.java b/packages/java/readme-metrics/src/main/java/com/readme/dataextraction/payload/user/UserDataCollector.java similarity index 94% rename from packages/java/readme-metrics/src/main/java/com/readme/dataextraction/UserDataCollector.java rename to packages/java/readme-metrics/src/main/java/com/readme/dataextraction/payload/user/UserDataCollector.java index 6cbf2a79ed..642b610668 100644 --- a/packages/java/readme-metrics/src/main/java/com/readme/dataextraction/UserDataCollector.java +++ b/packages/java/readme-metrics/src/main/java/com/readme/dataextraction/payload/user/UserDataCollector.java @@ -1,6 +1,4 @@ -package com.readme.dataextraction; - -import com.readme.domain.UserData; +package com.readme.dataextraction.payload.user; /** * Interface for extracting user-related data, such as API key, label, and email, diff --git a/packages/java/readme-metrics/src/main/java/com/readme/dataextraction/UserDataField.java b/packages/java/readme-metrics/src/main/java/com/readme/dataextraction/payload/user/UserDataField.java similarity index 83% rename from packages/java/readme-metrics/src/main/java/com/readme/dataextraction/UserDataField.java rename to packages/java/readme-metrics/src/main/java/com/readme/dataextraction/payload/user/UserDataField.java index fd0660ae5a..1ab3ebb0f4 100644 --- a/packages/java/readme-metrics/src/main/java/com/readme/dataextraction/UserDataField.java +++ b/packages/java/readme-metrics/src/main/java/com/readme/dataextraction/payload/user/UserDataField.java @@ -1,4 +1,4 @@ -package com.readme.dataextraction; +package com.readme.dataextraction.payload.user; import lombok.Getter; diff --git a/packages/java/readme-metrics/src/main/java/com/readme/dataextraction/UserDataSource.java b/packages/java/readme-metrics/src/main/java/com/readme/dataextraction/payload/user/UserDataSource.java similarity index 82% rename from packages/java/readme-metrics/src/main/java/com/readme/dataextraction/UserDataSource.java rename to packages/java/readme-metrics/src/main/java/com/readme/dataextraction/payload/user/UserDataSource.java index 153f466ab9..8ae16eccb3 100644 --- a/packages/java/readme-metrics/src/main/java/com/readme/dataextraction/UserDataSource.java +++ b/packages/java/readme-metrics/src/main/java/com/readme/dataextraction/payload/user/UserDataSource.java @@ -1,4 +1,4 @@ -package com.readme.dataextraction; +package com.readme.dataextraction.payload.user; import lombok.Getter; diff --git a/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/DataSender.java b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/DataSender.java index 930cdc3565..aef174f09a 100644 --- a/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/DataSender.java +++ b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/DataSender.java @@ -1,11 +1,11 @@ package com.readme.datatransfer; -import com.readme.domain.RequestPayload; +import java.util.List; public interface DataSender { String README_METRICS_URL = "https://metrics.readme.io/v1/request"; - int send(RequestPayload requestPayload); + int send(List payloadData); } diff --git a/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/HttpDataSender.java b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/HttpDataSender.java index 75ee64dbfd..a863d97b24 100644 --- a/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/HttpDataSender.java +++ b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/HttpDataSender.java @@ -1,12 +1,14 @@ package com.readme.datatransfer; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; import com.readme.config.CoreConfig; -import com.readme.domain.RequestPayload; -import com.readme.exception.EmptyRequestBodyException; +import lombok.extern.slf4j.Slf4j; import okhttp3.*; import java.io.IOException; import java.util.Base64; +import java.util.List; /** * Implementation of the {@link DataSender} interface that sends metrics data to a remote server @@ -17,6 +19,8 @@ *

The default endpoint for sending metrics is {@code https://metrics.readme.io/v1/request}. * */ + +@Slf4j public class HttpDataSender implements DataSender { public static final String APPLICATION_JSON_TYPE = "application/json"; @@ -30,22 +34,33 @@ public HttpDataSender(OkHttpClient client, CoreConfig coreConfig) { } @Override - public int send(RequestPayload requestPayload) { - if (requestPayload != null && requestPayload.getBody() != null && !requestPayload.getBody().isEmpty()) { + public int send(List payloadData) { + if (payloadData != null) { String encodedReadmeApiKey = getEncodedReadmeApiKey(); - Request request = createRequest(requestPayload, encodedReadmeApiKey); - - try (Response response = client.newCall(request).execute()) { + Response response = null; + try { + Request request = createRequest(payloadData, encodedReadmeApiKey); + response = client.newCall(request).execute(); return response.code(); - } catch (IOException e) { - throw new RuntimeException(e); + } catch (JsonProcessingException e) { + log.error("Error while building outgoing payload: ", e); + } catch (Exception e) { + log.error("Error while sending collected data: ", e); + } finally { + if (response != null) { + response.close(); + } } } - throw new EmptyRequestBodyException(); + return 0; } - private static Request createRequest(RequestPayload requestPayload, String encodedReadmeApiKey) { - RequestBody body = RequestBody.create(requestPayload.getBody(), MediaType.get(APPLICATION_JSON_TYPE)); + private static Request createRequest(List payloadData, String encodedReadmeApiKey) throws JsonProcessingException { + ObjectMapper objectMapper = new ObjectMapper(); + String outgoingLogBody = objectMapper.writeValueAsString(payloadData); + RequestBody body = RequestBody + .create(outgoingLogBody, MediaType.get(APPLICATION_JSON_TYPE)); + return new Request.Builder() .url(README_METRICS_URL) .header("Accept", APPLICATION_JSON_TYPE) diff --git a/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/OutgoingLogBody.java b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/OutgoingLogBody.java new file mode 100644 index 0000000000..c11e481809 --- /dev/null +++ b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/OutgoingLogBody.java @@ -0,0 +1,42 @@ +package com.readme.datatransfer; + +import com.readme.datatransfer.har.Group; +import com.readme.datatransfer.har.Har; +import lombok.Builder; +import lombok.Value; + +import java.util.UUID; + +@Value +@Builder +public class OutgoingLogBody { + + UUID id; + int version; + String clientIPAddress; + boolean development; + Group group; + + /** + * This field represents a HAR (HTTP Archive) request structure. + * + * The HAR model used here, along with all related fields and classes in its hierarchy, + * has been custom-implemented. The decision to use custom models instead of relying + * on third-party libraries was made due to the following reasons: + * + * 1. Some existing HAR libraries either do not support quite modern Java versions (e.g., Java 17-) + * or lack active maintenance, which introduces compatibility challenges. + * + * 2. The older versions of those libraries contain security vulnerabilities in their transitive + * dependencies, which could pose risks if included in the project. + * + * 3. Third-party HAR libraries often bring in unnecessary dependencies, increasing the + * overall size and complexity of the project and introducing undesired external ties. + * However, all we need is to have only models (POJO) classes + * + * By using custom implementations, this project ensures compatibility, security, and + * minimal dependencies while adhering to the HAR specification. + */ + Har request; + +} \ No newline at end of file diff --git a/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/OutgoingLogBodyConstructor.java b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/OutgoingLogBodyConstructor.java new file mode 100644 index 0000000000..2efab88478 --- /dev/null +++ b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/OutgoingLogBodyConstructor.java @@ -0,0 +1,187 @@ +package com.readme.datatransfer; + +import com.readme.dataextraction.LogOptions; +import com.readme.dataextraction.payload.PayloadData; +import com.readme.dataextraction.payload.requestresponse.RequestData; +import com.readme.dataextraction.payload.requestresponse.ResponseData; +import com.readme.dataextraction.payload.user.UserData; +import com.readme.datatransfer.har.*; + +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.stream.Collectors; + +import static com.readme.dataextraction.ApiKeyMasker.mask; + +public class OutgoingLogBodyConstructor { + + public OutgoingLogBody construct( + PayloadData payloadData, + LogOptions logOptions + ) { + UserData userData = payloadData.getUserData(); + RequestData requestData = payloadData.getApiCallLogData().getRequestData(); + ResponseData responseData = payloadData.getApiCallLogData().getResponseData(); + + + HarEntry harEntry = assembleHarEntry(payloadData, logOptions, requestData, responseData); + HarLog harLog = assembleHarLog(harEntry); + Group group = assembleGroup(userData); + + Har har = new Har(harLog); + return OutgoingLogBody.builder() + .id(UUID.randomUUID()) + .version(3) + .clientIPAddress(requestData.getRemoteAddress()) + .development(logOptions != null && Boolean.TRUE.equals(logOptions.getDevelopment())) + .group(group) + .request(har) + .build(); + + } + + private static Group assembleGroup(UserData userData) { + String maskedApiKey = mask(userData.getApiKey()); + return Group.builder() + .id(maskedApiKey) + .label(userData.getLabel()) + .email(userData.getEmail()) + .build(); + } + + private HarEntry assembleHarEntry(PayloadData payloadData, LogOptions logOptions, RequestData requestData, ResponseData responseData) { + int serverTime = getServerTime(payloadData); + + return HarEntry.builder() + .pageRef(requestData.getRoutePath() != null ? requestData.getRoutePath() + : constructUrl(requestData.getUrl(), requestData.getHeaders().get("host"), requestData.getProtocol())) + .startedDateTime(payloadData.getRequestStartedDateTime()) + .time(serverTime) + .request(processRequest(requestData, logOptions)) + .response(processResponse(responseData, logOptions)) + .cache(HarCache.builder().build()) //TODO Decide if it is required to do something here + .timings(HarTiming.builder() + .waitTime(0) + .receive(serverTime) + .build()) + .build(); + } + + private static HarLog assembleHarLog(HarEntry harEntry) { + String systemInformation = new StringBuilder(System.getProperty("os.arch")) + .append("-") + .append(System.getProperty("os.name")) + .append(System.getProperty("os.version")) + .append("/") + .append(System.getProperty("java.version")) + .toString(); + + HarCreatorBrowser harCreatorBrowser = HarCreatorBrowser.builder() + .name("readme-metrics (java)") + .version("1.0.0") //TODO correct version from POM + .comment(systemInformation) + .build(); + + return HarLog.builder() + .version("1.2") //TODO check if correct + .creator(harCreatorBrowser) + .entries(Collections.singletonList(harEntry)) + .build(); + } + + private HarRequest processRequest(RequestData requestData, LogOptions logOptions) { + Map headers = requestData.getHeaders(); + String requestBody = requestData.getBody(); + String protocol = requestData.getProtocol(); + + String requestParams = getRequestParametersAsString(requestData.getRequestParameters()); + List harQueryParameterList = getHarQueryParameterList(requestData.getRequestParameters()); + + HarRequest.HarRequestBuilder harRequestBuilder = HarRequest.builder() + .httpVersion(protocol) + .method(HttpMethod.valueOf(requestData.getMethod())) + .url(requestData.getUrl() + "?" + requestParams) + .queryString(harQueryParameterList) + .headers(convertHeaders(headers)); + + if(!requestData.getMethod().equals(HttpMethod.GET.name())) { + if (requestBody == null) { + throw new IllegalArgumentException("Request Body is null"); + } + HarPostData harPostData = assembleHarPostData(requestBody, headers.get("content-type")); + harRequestBuilder.postData(harPostData); + } + + return harRequestBuilder.build(); + } + + private static List getHarQueryParameterList(Map requestParameters) { + return requestParameters.entrySet().stream() + .map(entry -> HarQueryParam.builder() + .name(entry.getKey()) + .value(entry.getValue()) + .build()) + .collect(Collectors.toList()); + } + + private static String getRequestParametersAsString(Map requestParameters) { + return requestParameters.entrySet() + .stream() + .map(entry -> entry.getKey() + "=" + entry.getValue()) + .collect(Collectors.joining("&")); + } + + private HarPostData assembleHarPostData(String body, String mimeType) { + return HarPostData.builder() + .mimeType(mimeType) + .text(body) + .build(); + } + + private HarResponse processResponse(ResponseData responseData, LogOptions logOptions) { + Map headers = responseData.getHeaders(); + String body = responseData.getBody(); + + String contentType = headers.get("content-type"); + String contentLength = headers.get("content-length"); + HarContent content = HarContent.builder() + .mimeType(contentType) + .text(body) + .size(contentLength != null ? Long.parseLong(contentLength) : body.length()) + .build(); + return HarResponse.builder() + .status(responseData.getStatusCode()) + .statusText(responseData.getStatusMessage()) + .headers(convertHeaders(headers)) + .content(content) + .build(); + } + + + private List convertHeaders(Map headers) { + return headers.entrySet().stream() + .map(entry -> + HarHeader.builder() + .name(entry.getKey()) + .value(entry.getValue()). + build()) + .collect(Collectors.toList()); + } + + private String constructUrl(String url, String host, String proto) { + try { + return new URI(proto, host, url, null).toString(); + } catch (URISyntaxException e) { + return ""; + } + } + + private static int getServerTime(PayloadData payloadData) { + return (int) (payloadData.getResponseEndDateTime().getTime() - payloadData.getRequestStartedDateTime().getTime()); + } + +} \ No newline at end of file diff --git a/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/PayloadDataDispatcher.java b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/PayloadDataDispatcher.java new file mode 100644 index 0000000000..a1efd6b1c9 --- /dev/null +++ b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/PayloadDataDispatcher.java @@ -0,0 +1,31 @@ +package com.readme.datatransfer; + +import com.readme.dataextraction.LogOptions; +import com.readme.dataextraction.payload.PayloadData; +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +import java.util.Collections; + +@Slf4j +@AllArgsConstructor +public class PayloadDataDispatcher { + + private DataSender dataSender; + + private OutgoingLogBodyConstructor payloadConstructor; + + public void dispatch(PayloadData payloadData) { + + try { + LogOptions logOptions = new LogOptions(); //TODO implement LogOptions + + OutgoingLogBody outgoingLogBody = payloadConstructor.construct(payloadData, logOptions); + dataSender.send(Collections.singletonList(outgoingLogBody)); + } catch (Exception e) { + log.error("Error occurred on data dispatch phase: {}", e.getMessage()); + } + } + + +} diff --git a/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/Group.java b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/Group.java new file mode 100644 index 0000000000..53804c37eb --- /dev/null +++ b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/Group.java @@ -0,0 +1,12 @@ +package com.readme.datatransfer.har; + +import lombok.Builder; +import lombok.Value; + +@Value +@Builder +public class Group { + String id; + String label; + String email; +} \ No newline at end of file diff --git a/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/Har.java b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/Har.java new file mode 100644 index 0000000000..301bef8eab --- /dev/null +++ b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/Har.java @@ -0,0 +1,39 @@ +package com.readme.datatransfer.har; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.Value; + + +/** + * This class represents a HAR (HTTP Archive) request structure. + * + * The HAR model used here, along with all related fields and classes in its hierarchy, + * has been custom-implemented. The decision to use custom models instead of relying + * on third-party libraries was made due to the following reasons: + * + * 1. Some existing HAR libraries either do not support quite modern Java versions (e.g., Java 17-) + * or lack active maintenance, which introduces compatibility challenges. + * + * 2. The older versions of those libraries contain security vulnerabilities in their transitive + * dependencies, which could pose risks if included in the project. + * + * 3. Third-party HAR libraries often bring in unnecessary dependencies, increasing the + * overall size and complexity of the project and introducing undesired external ties. + * However, all we need is to have only models (POJO) classes + * + * By using custom implementations, this project ensures compatibility, security, and + * minimal dependencies while adhering to the HAR specification. + */ +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonIgnoreProperties(ignoreUnknown = true) +@Value +@AllArgsConstructor +public class Har { + + HarLog log; + +} diff --git a/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarCache.java b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarCache.java new file mode 100644 index 0000000000..b02ffe232f --- /dev/null +++ b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarCache.java @@ -0,0 +1,63 @@ +package com.readme.datatransfer.har; + +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.Builder; +import lombok.Data; + +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + + +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonIgnoreProperties(ignoreUnknown = true) +@Data +@Builder +public class HarCache { + + private HarCacheInfo beforeRequest; + private HarCacheInfo afterRequest; + private String comment; + private Map additional; + + @JsonAnyGetter + public Map getAdditional() { + return additional; + } + + @JsonAnySetter + public void setAdditionalField(String key, Object value) { + if (additional == null) { + additional = new HashMap<>(8); + } + this.additional.put(key, value); + } + + @JsonInclude(JsonInclude.Include.NON_NULL) + @JsonIgnoreProperties(ignoreUnknown = true) + @Data + @Builder + public static final class HarCacheInfo { + + private Date expires; + private Date lastAccess; + private String eTag; + private int hitCount; + private String comment; + private final Map additional = new HashMap<>(); + + @JsonAnyGetter + public Map getAdditional() { + return additional; + } + + @JsonAnySetter + public void setAdditionalField(String key, Object value) { + this.additional.put(key, value); + } + + } +} diff --git a/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarContent.java b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarContent.java new file mode 100644 index 0000000000..3c515e28ac --- /dev/null +++ b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarContent.java @@ -0,0 +1,40 @@ +package com.readme.datatransfer.har; + +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.Builder; +import lombok.Data; + +import java.util.HashMap; +import java.util.Map; + +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonIgnoreProperties(ignoreUnknown = true) +@Builder +@Data +public class HarContent { + + private long size; + private long compression; + private String mimeType; + private String text; + private String encoding; + private String comment; + private Map additional; + + @JsonAnyGetter + public Map getAdditional() { + return additional; + } + + @JsonAnySetter + public void setAdditionalField(String key, Object value) { + if (additional == null) { + additional = new HashMap<>(8); + } + this.additional.put(key, value); + } + +} diff --git a/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarCookie.java b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarCookie.java new file mode 100644 index 0000000000..51e5620f2b --- /dev/null +++ b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarCookie.java @@ -0,0 +1,43 @@ +package com.readme.datatransfer.har; + +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.Builder; +import lombok.Data; + +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonIgnoreProperties(ignoreUnknown = true) +@Data +@Builder +public class HarCookie { + + private String name; + private String value; + private String path; + private String domain; + private Date expires; + private Boolean httpOnly; + private Boolean secure; + private String comment; + private Map additional; + + @JsonAnyGetter + public Map getAdditional() { + return additional; + } + + @JsonAnySetter + public void setAdditionalField(String key, Object value) { + if (additional == null) { + additional = new HashMap<>(8); + } + this.additional.put(key, value); + } + +} diff --git a/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarCreatorBrowser.java b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarCreatorBrowser.java new file mode 100644 index 0000000000..ddf5e57692 --- /dev/null +++ b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarCreatorBrowser.java @@ -0,0 +1,38 @@ +package com.readme.datatransfer.har; + + +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.Builder; +import lombok.Data; + +import java.util.HashMap; +import java.util.Map; + +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonIgnoreProperties(ignoreUnknown = true) +@Data +@Builder +public class HarCreatorBrowser { + + private String name; + private String version; + private String comment; + private Map additional; + + @JsonAnyGetter + public Map getAdditional() { + return additional; + } + + @JsonAnySetter + public void setAdditionalField(String key, Object value) { + if (additional == null) { + additional = new HashMap<>(8); + } + this.additional.put(key, value); + } + +} diff --git a/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarEntry.java b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarEntry.java new file mode 100644 index 0000000000..e7a2954d17 --- /dev/null +++ b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarEntry.java @@ -0,0 +1,48 @@ +package com.readme.datatransfer.har; + + +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.Builder; +import lombok.Data; + +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + + +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonIgnoreProperties(ignoreUnknown = true) +@Data +@Builder +public class HarEntry { + + private String pageRef; + private Date startedDateTime; + private int time; + private HarRequest request; + private HarResponse response; + private HarCache cache; + private HarTiming timings; + private String serverIPAddress; + private String connection; + private String comment; + private Map additional; + + @JsonAnyGetter + public Map getAdditional() { + return additional; + } + + @JsonAnySetter + public void setAdditionalField(String key, Object value) { + if (additional == null) { + additional = new HashMap<>(8); + } + this.additional.put(key, value); + } + + +} diff --git a/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarHeader.java b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarHeader.java new file mode 100644 index 0000000000..7bc11baf9a --- /dev/null +++ b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarHeader.java @@ -0,0 +1,37 @@ +package com.readme.datatransfer.har; + +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.Builder; +import lombok.Data; + +import java.util.HashMap; +import java.util.Map; + +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonIgnoreProperties(ignoreUnknown = true) +@Builder +@Data +public class HarHeader { + + private String name; + private String value; + private String comment; + private Map additional; + + @JsonAnyGetter + public Map getAdditional() { + return additional; + } + + @JsonAnySetter + public void setAdditionalField(String key, Object value) { + if (additional == null) { + additional = new HashMap<>(8); + } + this.additional.put(key, value); + } + +} diff --git a/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarLog.java b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarLog.java new file mode 100644 index 0000000000..576d3fb400 --- /dev/null +++ b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarLog.java @@ -0,0 +1,44 @@ +package com.readme.datatransfer.har; + + +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.Builder; +import lombok.Data; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + + +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonIgnoreProperties(ignoreUnknown = true) +@Builder +@Data +public class HarLog { + + private String version; + private HarCreatorBrowser creator; + private HarCreatorBrowser browser; + private List pages; + private List entries; + private String comment; + private Map additional; + + @JsonAnyGetter + public Map getAdditional() { + return additional; + } + + @JsonAnySetter + public void setAdditionalField(String key, Object value) { + if (additional == null) { + additional = new HashMap<>(8); + } + this.additional.put(key, value); + } + +} diff --git a/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarPage.java b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarPage.java new file mode 100644 index 0000000000..255c711a50 --- /dev/null +++ b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarPage.java @@ -0,0 +1,42 @@ +package com.readme.datatransfer.har; + + +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.Builder; +import lombok.Data; + +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonIgnoreProperties(ignoreUnknown = true) +@Data +@Builder +public class HarPage { + + private Date startedDateTime; + private String id; + private String title; + private HarPageTiming pageTimings; + private String comment; + private Map additional; + + @JsonAnyGetter + public Map getAdditional() { + return additional; + } + + @JsonAnySetter + public void setAdditionalField(String key, Object value) { + if (additional == null) { + additional = new HashMap<>(8); + } + this.additional.put(key, value); + } + + +} diff --git a/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarPageTiming.java b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarPageTiming.java new file mode 100644 index 0000000000..677ec8a9cd --- /dev/null +++ b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarPageTiming.java @@ -0,0 +1,42 @@ +package com.readme.datatransfer.har; + +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.Builder; +import lombok.Data; + +import java.util.HashMap; +import java.util.Map; + +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonIgnoreProperties(ignoreUnknown = true) +@Builder +@Data +public class HarPageTiming { + + protected static final int DEFAULT_TIME = -1; + + @Builder.Default + private int onContentLoad = DEFAULT_TIME; + @Builder.Default + private int onLoad = DEFAULT_TIME; + private String comment; + private Map additional; + + @JsonAnyGetter + public Map getAdditional() { + return additional; + } + + @JsonAnySetter + public void setAdditionalField(String key, Object value) { + if (additional == null) { + additional = new HashMap<>(8); + } + this.additional.put(key, value); + } + + +} diff --git a/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarPostData.java b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarPostData.java new file mode 100644 index 0000000000..ba3692ee09 --- /dev/null +++ b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarPostData.java @@ -0,0 +1,41 @@ +package com.readme.datatransfer.har; + +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.Builder; +import lombok.Data; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonIgnoreProperties(ignoreUnknown = true) +@Builder +@Data +public class HarPostData { + + private String mimeType; + @Builder.Default + private List params = new ArrayList<>(4); + private String text; + private String comment; + private Map additional; + + @JsonAnyGetter + public Map getAdditional() { + return additional; + } + + @JsonAnySetter + public void setAdditionalField(String key, Object value) { + if (additional == null) { + additional = new HashMap<>(8); + } + this.additional.put(key, value); + } + +} diff --git a/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarPostDataParam.java b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarPostDataParam.java new file mode 100644 index 0000000000..03db7d26af --- /dev/null +++ b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarPostDataParam.java @@ -0,0 +1,39 @@ +package com.readme.datatransfer.har; + +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.Builder; +import lombok.Data; + +import java.util.HashMap; +import java.util.Map; + +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonIgnoreProperties(ignoreUnknown = true) +@Data +@Builder +public class HarPostDataParam { + + private String name; + private String value; + private String fileName; + private String contentType; + private String comment; + private Map additional; + + @JsonAnyGetter + public Map getAdditional() { + return additional; + } + + @JsonAnySetter + public void setAdditionalField(String key, Object value) { + if (additional == null) { + additional = new HashMap<>(8); + } + this.additional.put(key, value); + } + +} diff --git a/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarQueryParam.java b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarQueryParam.java new file mode 100644 index 0000000000..516c55a382 --- /dev/null +++ b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarQueryParam.java @@ -0,0 +1,37 @@ +package com.readme.datatransfer.har; + +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.Builder; +import lombok.Data; + +import java.util.HashMap; +import java.util.Map; + +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonIgnoreProperties(ignoreUnknown = true) +@Builder +@Data +public class HarQueryParam { + + private String name; + private String value; + private String comment; + private Map additional; + + @JsonAnyGetter + public Map getAdditional() { + return additional; + } + + @JsonAnySetter + public void setAdditionalField(String key, Object value) { + if (additional == null) { + additional = new HashMap<>(8); + } + this.additional.put(key, value); + } + +} diff --git a/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarRequest.java b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarRequest.java new file mode 100644 index 0000000000..466c3f1610 --- /dev/null +++ b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarRequest.java @@ -0,0 +1,49 @@ +package com.readme.datatransfer.har; + +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.Builder; +import lombok.Data; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonIgnoreProperties(ignoreUnknown = true) +@Data +@Builder +public class HarRequest { + + protected static final long DEFAULT_SIZE = -1L; + + private HttpMethod method; + private String url; + private String httpVersion; + private List cookies; + private List headers; + private List queryString; + private HarPostData postData; + @Builder.Default + private long headersSize = DEFAULT_SIZE; + @Builder.Default + private long bodySize = DEFAULT_SIZE; + private String comment; + private Map additional; + + @JsonAnyGetter + public Map getAdditional() { + return additional; + } + + @JsonAnySetter + public void setAdditionalField(String key, Object value) { + if (additional == null) { + additional = new HashMap<>(8); + } + this.additional.put(key, value); + } + +} diff --git a/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarResponse.java b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarResponse.java new file mode 100644 index 0000000000..c2059e225b --- /dev/null +++ b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarResponse.java @@ -0,0 +1,50 @@ +package com.readme.datatransfer.har; + +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.Builder; +import lombok.Data; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + + +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonIgnoreProperties(ignoreUnknown = true) +@Data +@Builder +public class HarResponse { + + protected static final long DEFAULT_SIZE = -1L; + + private int status; + private String statusText; + private String httpVersion; + private List cookies; + private List headers; + private HarContent content; + private String redirectURL; + @Builder.Default + private long headersSize = DEFAULT_SIZE; + @Builder.Default + private long bodySize = DEFAULT_SIZE; + private String comment; + private Map additional; + + @JsonAnyGetter + public Map getAdditional() { + return additional; + } + + @JsonAnySetter + public void setAdditionalField(String key, Object value) { + if (additional == null) { + additional = new HashMap<>(8); + } + this.additional.put(key, value); + } + +} diff --git a/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarTiming.java b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarTiming.java new file mode 100644 index 0000000000..2acb99a3b1 --- /dev/null +++ b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HarTiming.java @@ -0,0 +1,45 @@ +package com.readme.datatransfer.har; + +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.Builder; +import lombok.Data; + +import java.util.HashMap; +import java.util.Map; + +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonIgnoreProperties(ignoreUnknown = true) +@Data +@Builder +public class HarTiming { + + protected static final int DEFAULT_TIME = -1; + + private int blocked; + private int dns; + private int connect; + private int send; + private int waitTime; + private int receive; + private int ssl; + private String comment; + private Map additional; + + @JsonAnyGetter + public Map getAdditional() { + return additional; + } + + @JsonAnySetter + public void setAdditionalField(String key, Object value) { + if (additional == null) { + additional = new HashMap<>(8); + } + this.additional.put(key, value); + } + + +} diff --git a/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HttpMethod.java b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HttpMethod.java new file mode 100644 index 0000000000..d7f69a39a9 --- /dev/null +++ b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HttpMethod.java @@ -0,0 +1,5 @@ +package com.readme.datatransfer.har; + +public enum HttpMethod { + GET, POST, PUT, HEAD, PROPFIND, OPTIONS, REPORT, DELETE, CONNECT, TRACE, CCM_POST, PATCH; +} diff --git a/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HttpStatus.java b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HttpStatus.java new file mode 100644 index 0000000000..65234eb3c5 --- /dev/null +++ b/packages/java/readme-metrics/src/main/java/com/readme/datatransfer/har/HttpStatus.java @@ -0,0 +1,59 @@ +package com.readme.datatransfer.har; + +import java.util.HashMap; +import java.util.Map; + +public enum HttpStatus { + + UNKNOWN_HTTP_STATUS(0), + + OK(200), CREATED(201), ACCEPTED(202), NO_CONTENT(204), RESET_CONTENT(205), + PARTIAL_CONTENT(206), MULTI_STATUS(207), ALREADY_REPORTED(208), IM_USED(226), + + MOVED_PERMANENTLY(301), FOUND(302), SEE_OTHER(303), NOT_MODIFIED(304), + USE_PROXY(305), TEMPORARY_REDIRECT(307), PERMANENT_REDIRECT(308), + + BAD_REQUEST(400), UNAUTHORIZED(401), PAYMENT_REQUIRED(402), FORBIDDEN(403), + NOT_FOUND(404), METHOD_NOT_ALLOWED(405), NOT_ACCEPTABLE(406), + PROXY_AUTHENTICATION_REQUIRED(407), REQUEST_TIMEOUT(408), + CONFLICT(409), GONE(410), LENGTH_REQUIRED(411), PRECONDITION_FAILED(412), + REQUEST_ENTITY_TOO_LARGE(413), REQUEST_URI_TOO_LONG(414), + UNSUPPORTED_MEDIA_TYPE(415), REQUESTED_RANGE_NOT_SATISFIABLE(416), + EXPECTATION_FAILED(417), MISDIRECTED_REQUEST(421), + UNPROCESSABLE_ENTITY(422), LOCKED(423), FAILED_DEPENDENCY(424), + UPGRADE_REQUIRED(426), PRECONDITION_REQUIRED(428), TOO_MANY_REQUESTS(429), + REQUEST_HEADER_FIELDS_TOO_LARGE(431), UNAVAILABLE_FOR_LEGAL_REASONS(451), + + INTERNAL_SERVER_ERROR(500), NOT_IMPLEMENTED(501), + BAD_GATEWAY(502), SERVICE_UNAVAILABLE(503), GATEWAY_TIMEOUT(504), + HTTP_VERSION_NOT_SUPPORTED(505), VARIANT_ALSO_NEGOTIATES(506), + INSUFFICIENT_STORAGE(507), LOOP_DETECTED(508), + NOT_EXTENDED(510), NETWORK_AUTHENTICATION_REQUIRED(511); + + private static final Map CODE_MAP = new HashMap<>(); + + static { + for (HttpStatus status : HttpStatus.values()) { + CODE_MAP.put(status.getCode(), status); + } + } + + private int code; + + private HttpStatus(int code) { + this.code = code; + } + + public int getCode() { + return code; + } + + public static HttpStatus byCode(int code) { + HttpStatus status = CODE_MAP.get(code); + if (status == null) { + return UNKNOWN_HTTP_STATUS; + } + return status; + } + +} diff --git a/packages/java/readme-metrics/src/main/java/com/readme/domain/RequestPayload.java b/packages/java/readme-metrics/src/main/java/com/readme/domain/RequestPayload.java deleted file mode 100644 index 9044a06e61..0000000000 --- a/packages/java/readme-metrics/src/main/java/com/readme/domain/RequestPayload.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.readme.domain; - -import lombok.Builder; -import lombok.Value; - - -@Value -@Builder -public class RequestPayload { - - String body; - -} diff --git a/packages/java/readme-metrics/src/main/java/com/readme/exception/EmptyRequestBodyException.java b/packages/java/readme-metrics/src/main/java/com/readme/exception/EmptyRequestBodyException.java deleted file mode 100644 index f04593a1a0..0000000000 --- a/packages/java/readme-metrics/src/main/java/com/readme/exception/EmptyRequestBodyException.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.readme.exception; - -public class EmptyRequestBodyException extends RuntimeException { - - public EmptyRequestBodyException() { - super("The request body cannot be empty"); - } -} diff --git a/packages/java/readme-metrics/src/test/java/com/readme/datatransfer/HttpDataSenderTest.java b/packages/java/readme-metrics/src/test/java/com/readme/datatransfer/HttpDataSenderTest.java index e687eb7d6a..fb791c9bdc 100644 --- a/packages/java/readme-metrics/src/test/java/com/readme/datatransfer/HttpDataSenderTest.java +++ b/packages/java/readme-metrics/src/test/java/com/readme/datatransfer/HttpDataSenderTest.java @@ -1,46 +1,99 @@ package com.readme.datatransfer; import com.readme.config.CoreConfig; -import com.readme.domain.RequestPayload; -import com.readme.exception.EmptyRequestBodyException; +import com.readme.datatransfer.har.Group; +import com.readme.datatransfer.har.Har; +import com.readme.datatransfer.har.HarLog; import okhttp3.*; import org.jetbrains.annotations.NotNull; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; import java.io.IOException; +import java.util.Base64; +import java.util.Collections; +import java.util.List; +import java.util.UUID; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThrows; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.*; public class HttpDataSenderTest { + private OkHttpClient mockClient; + private CoreConfig mockCoreConfig; + private HttpDataSender httpDataSender; + + @BeforeEach + public void setUp() { + mockClient = mock(OkHttpClient.class); + mockCoreConfig = mock(CoreConfig.class); + when(mockCoreConfig.getReadmeAPIKey()).thenReturn("testApiKey"); + httpDataSender = new HttpDataSender(mockClient, mockCoreConfig); + } + + @Test + public void send_ShouldReturnResponseCode_WhenRequestIsSuccessful() throws IOException { + Response mockResponse = mockResponse(); + Call mockCall = mock(Call.class); + when(mockCall.execute()).thenReturn(mockResponse); + when(mockClient.newCall(any(Request.class))).thenReturn(mockCall); + List payload = Collections.singletonList(createTestOutgoingLogBody()); + + int result = httpDataSender.send(payload); + + assertEquals(200, result); + } + @Test - public void testSendOnSuccess() throws IOException { - OkHttpClient client = mock(OkHttpClient.class); - Call call = mock(Call.class); - Response response = mockResponse(); - RequestPayload requestPayload = mockRequestMetadata(); + public void send_ShouldReturnZero_WhenPayloadIsNull() { + int result = httpDataSender.send(null); + + assertEquals(0, result); + verifyNoInteractions(mockClient); + } - when(client.newCall(any(Request.class))).thenReturn(call); - when(call.execute()).thenReturn(response); - HttpDataSender httpDataSender = new HttpDataSender(client, mockCoreConfig()); + @Test + public void send_ShouldReturnZero_WhenIOExceptionOccurs() throws IOException { + Call mockCall = mock(Call.class); + when(mockClient.newCall(any(Request.class))).thenReturn(mockCall); + doThrow(new IOException("Test exception")).when(mockCall).execute(); + List payload = Collections.singletonList(createTestOutgoingLogBody()); - assertEquals(200, httpDataSender.send(requestPayload)); + int result = httpDataSender.send(payload); + assertEquals(0, result); } @Test - public void testSendOnBodyDoesntExist() throws IOException { - OkHttpClient client = mock(OkHttpClient.class); - Call call = mock(Call.class); - Response response = mockResponse(); - RequestPayload requestPayload = RequestPayload.builder().build(); + public void send_ShouldAddAuthorizationHeader() throws IOException { + Response mockResponse = mock(Response.class); + when(mockResponse.code()).thenReturn(200); + Call mockCall = mock(Call.class); + when(mockCall.execute()).thenReturn(mockResponse); + when(mockClient.newCall(any(Request.class))).thenReturn(mockCall); + + List payload = Collections.singletonList(createTestOutgoingLogBody()); + ArgumentCaptor requestCaptor = ArgumentCaptor.forClass(Request.class); - when(client.newCall(any(Request.class))).thenReturn(call); - when(call.execute()).thenReturn(response); - HttpDataSender httpDataSender = new HttpDataSender(client, mockCoreConfig()); + httpDataSender.send(payload); - assertThrows(EmptyRequestBodyException.class, () -> httpDataSender.send(requestPayload)); + verify(mockClient).newCall(requestCaptor.capture()); + Request capturedRequest = requestCaptor.getValue(); + String authHeader = capturedRequest.header("Authorization"); + String expectedAuthHeader = "Basic " + Base64.getEncoder().encodeToString("testApiKey:".getBytes()); + assertEquals(expectedAuthHeader, authHeader); + } + + private OutgoingLogBody createTestOutgoingLogBody() { + return OutgoingLogBody.builder() + .id(UUID.randomUUID()) + .version(1) + .clientIPAddress("127.0.0.1") + .development(true) + .group(Group.builder().build()) + .request(new Har(HarLog.builder().build())) + .build(); } @NotNull @@ -54,17 +107,5 @@ private static Response mockResponse() { .build(); } - // TODO move to separate class V V V V V - private static RequestPayload mockRequestMetadata() { - return RequestPayload.builder() - .body("body") - .build(); - } - - private static CoreConfig mockCoreConfig() { - return CoreConfig.builder() - .readmeAPIKey("apikey") - .build(); - } } diff --git a/packages/java/readme-metrics/src/test/java/com/readme/datatransfer/OutgoingLogBodyConstructorTest.java b/packages/java/readme-metrics/src/test/java/com/readme/datatransfer/OutgoingLogBodyConstructorTest.java new file mode 100644 index 0000000000..9775f2f52b --- /dev/null +++ b/packages/java/readme-metrics/src/test/java/com/readme/datatransfer/OutgoingLogBodyConstructorTest.java @@ -0,0 +1,105 @@ +package com.readme.datatransfer; + +import com.readme.dataextraction.LogOptions; +import com.readme.dataextraction.payload.PayloadData; +import com.readme.dataextraction.payload.requestresponse.ApiCallLogData; +import com.readme.dataextraction.payload.requestresponse.RequestData; +import com.readme.dataextraction.payload.requestresponse.ResponseData; +import com.readme.dataextraction.payload.user.UserData; +import com.readme.datatransfer.har.Group; +import com.readme.datatransfer.har.Har; +import com.readme.datatransfer.har.HarEntry; +import com.readme.datatransfer.har.HarLog; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.Date; +import java.util.Map; + +import static com.readme.dataextraction.ApiKeyMasker.mask; +import static org.junit.jupiter.api.Assertions.*; + +@ExtendWith(MockitoExtension.class) +class OutgoingLogBodyConstructorTest { + + @InjectMocks + private OutgoingLogBodyConstructor outgoingLogBodyConstructor; + + @Test + void construct_ShouldBuildOutgoingLogBody_Success() { + PayloadData payloadData = createStubPayloadData(); + LogOptions logOptions = createStubLogOptions(); + + OutgoingLogBody result = outgoingLogBodyConstructor.construct(payloadData, logOptions); + + assertNotNull(result); + assertEquals(payloadData.getApiCallLogData().getRequestData().getRemoteAddress(), result.getClientIPAddress()); + assertEquals(3, result.getVersion()); + assertTrue(result.isDevelopment()); + + Group group = result.getGroup(); + assertNotNull(group); + assertEquals(mask(payloadData.getUserData().getApiKey()), group.getId()); + assertEquals(payloadData.getUserData().getLabel(), group.getLabel()); + assertEquals(payloadData.getUserData().getEmail(), group.getEmail()); + + Har har = result.getRequest(); + assertNotNull(har); + HarLog harLog = har.getLog(); + assertNotNull(harLog); + assertEquals("1.2", harLog.getVersion()); + assertEquals(1, harLog.getEntries().size()); + + HarEntry harEntry = harLog.getEntries().get(0); + assertEquals(payloadData.getApiCallLogData().getRequestData().getRoutePath(), harEntry.getPageRef()); + assertEquals(payloadData.getApiCallLogData().getResponseData().getStatusCode(), harEntry.getResponse().getStatus()); + } + + private PayloadData createStubPayloadData() { + UserData userData = UserData.builder() + .apiKey("owlApiKey") + .email("owl@birdfact0ry.abc") + .label("Owl Label") + .build(); + + RequestData requestData = RequestData.builder() + .body("{\"key\":\"value\"}") + .routePath("/owl/path") + .remoteAddress("127.0.0.1") + .protocol("HTTP/1.1") + .url("http://owl-bowl.abc/api") + .method("POST") + .headers(Map.of("host", "owl-bowl.abc", "content-type", "application/json")) + .requestParameters(Map.of("param1", "value1")) + .build(); + + ResponseData responseData = ResponseData.builder() + .body("{\"response\":\"ok\"}") + .headers(Map.of("content-type", "application/json", "content-length", "100")) + .statusCode(200) + .statusMessage("OK") + .build(); + + ApiCallLogData apiCallLogData = ApiCallLogData.builder() + .requestData(requestData) + .responseData(responseData) + .build(); + + return PayloadData.builder() + .userData(userData) + .apiCallLogData(apiCallLogData) + .requestStartedDateTime(new Date(System.currentTimeMillis() - 1000)) + .responseEndDateTime(new Date()) + .build(); + } + + private LogOptions createStubLogOptions() { + LogOptions logOptions = new LogOptions(); + logOptions.setDevelopment(true); + return logOptions; + } +} \ No newline at end of file