* ========================LICENSE_END===================================
*/
- package org.onap.ccsdk.oran.a1policymanagementservice.service.v3;
+package org.onap.ccsdk.oran.a1policymanagementservice.service.v3;
- import java.lang.invoke.MethodHandles;
- import java.util.Base64;
+import java.lang.invoke.MethodHandles;
+import java.util.Base64;
- import org.onap.ccsdk.oran.a1policymanagementservice.models.v2.PolicyInfo;
- import org.onap.ccsdk.oran.a1policymanagementservice.models.v3.PolicyObjectInformation;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.http.HttpHeaders;
- import org.springframework.stereotype.Service;
- import org.springframework.web.server.ServerWebExchange;
+import org.onap.ccsdk.oran.a1policymanagementservice.models.v2.PolicyInfo;
+import org.onap.ccsdk.oran.a1policymanagementservice.models.v3.PolicyObjectInformation;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.http.HttpHeaders;
+import org.springframework.stereotype.Service;
+import org.springframework.web.server.ServerWebExchange;
- import com.google.gson.JsonObject;
- import com.google.gson.JsonParser;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
- @Service
- public class TokenService {
- private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+@Service
+public class TokenService {
+ private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
- // Prefix used to identify Bearer tokens in the Authorization header
- private static final String BEARER_PREFIX = "Bearer ";
+ // Prefix used to identify Bearer tokens in the Authorization header
+ private static final String BEARER_PREFIX = "Bearer ";
- /**
- * Retrieves the service ID for version 3 (v3) of the API, which uses PolicyObjectInformation.
- *
- * @param policyInfoValue The PolicyObjectInformation object containing the policy details.
- * @param exchange The ServerWebExchange object that contains request and response information.
- * @return The service ID, either from the policy information or derived from the client ID in the token.
- */
- public String getServiceId(PolicyObjectInformation policyInfoValue, ServerWebExchange exchange) {
- String serviceId = policyInfoValue.getServiceId();
- String clientId = extractClientIdFromToken(exchange);
+ /**
+ * Retrieves the service ID for version 3 (v3) of the API, which uses PolicyObjectInformation.
+ *
+ * @param policyInfoValue The PolicyObjectInformation object containing the policy details.
+ * @param exchange The ServerWebExchange object that contains request and response information.
+ * @return The service ID, either from the policy information or derived from the client ID in the token.
+ */
+ public String getServiceId(PolicyObjectInformation policyInfoValue, ServerWebExchange exchange) {
+ String serviceId = policyInfoValue.getServiceId();
+ String clientId = extractClientIdFromToken(exchange);
- // If the service ID from the policy is blank, use the client ID from the token instead
- if (serviceId.isBlank()) {
- if (clientId != null && !clientId.isBlank()) {
- serviceId = clientId;
- }
- }
- // Return the determined service ID
- logger.debug("ServiceID extracted from token: " + serviceId);
- return serviceId;
- }
+ // If the service ID from the policy is blank, use the client ID from the token instead
+ if (serviceId.isBlank() && clientId != null && !clientId.isBlank()) {
+ serviceId = clientId;
+ }
+ // Return the determined service ID
+ logger.debug("ServiceID extracted from token: {}", serviceId);
+ return serviceId;
+ }
- /**
- * Retrieves the service ID for version 2 (v2) of the API, which uses PolicyInfo.
- *
- * @param policyInfoValue The PolicyInfo object containing the policy details.
- * @param exchange The ServerWebExchange object that contains request and response information.
- * @return The service ID, either from the policy information or derived from the client ID in the token.
- */
- public String getServiceId(PolicyInfo policyInfoValue, ServerWebExchange exchange) {
- String serviceId = policyInfoValue.getServiceId();
- String clientId = extractClientIdFromToken(exchange);
+ /**
+ * Retrieves the service ID for version 2 (v2) of the API, which uses PolicyInfo.
+ *
+ * @param policyInfoValue The PolicyInfo object containing the policy details.
+ * @param exchange The ServerWebExchange object that contains request and response information.
+ * @return The service ID, either from the policy information or derived from the client ID in the token.
+ */
+ public String getServiceId(PolicyInfo policyInfoValue, ServerWebExchange exchange) {
+ String serviceId = policyInfoValue.getServiceId();
+ String clientId = extractClientIdFromToken(exchange);
- // If the service ID from the policy is blank, use the client ID from the token instead
- if (serviceId.isBlank()) {
- if (clientId != null && !clientId.isBlank()) {
- serviceId = clientId;
- }
+ // If the service ID from the policy is blank, use the client ID from the token instead
+ if (serviceId.isBlank() && clientId != null && !clientId.isBlank()) {
+ serviceId = clientId;
}
// Return the determined service ID
- logger.debug("ServiceID extracted from token: " + serviceId);
+ logger.debug("ServiceID extracted from token: {}", serviceId);
return serviceId;
- }
+ }
- /**
- * Extracts the client ID from the Bearer token present in the Authorization header.
- *
- * @param exchange The ServerWebExchange object that contains request and response information.
- * @return The client ID extracted from the token, or null if the token is invalid or missing.
- */
- private String extractClientIdFromToken(ServerWebExchange exchange) {
- HttpHeaders headers = exchange.getRequest().getHeaders();
- String authHeader = headers.getFirst(HttpHeaders.AUTHORIZATION);
+ /**
+ * Extracts the client ID from the Bearer token present in the Authorization header.
+ *
+ * @param exchange The ServerWebExchange object that contains request and response information.
+ * @return The client ID extracted from the token, or null if the token is invalid or missing.
+ */
+ private String extractClientIdFromToken(ServerWebExchange exchange) {
+ HttpHeaders headers = exchange.getRequest().getHeaders();
+ String authHeader = headers.getFirst(HttpHeaders.AUTHORIZATION);
- // Check if the Authorization header exists and contains a Bearer token
+ // Check if the Authorization header exists and contains a Bearer token
if (authHeader != null && authHeader.startsWith(BEARER_PREFIX)) {
String token = authHeader.substring(BEARER_PREFIX.length());
return decodeClientId(token);
logger.debug("Authorization header is missing or does not contain a Bearer token");
}
return null;
- }
+ }
- /**
- * Decodes the client ID from the JWT token.
- *
- * @param token The JWT token string.
- * @return The client ID extracted from the token, or null if decoding fails.
- */
- private String decodeClientId(String token) {
- try {
- // Split the JWT token to get the payload part
- String[] chunks = token.split("\\.");
- Base64.Decoder decoder = Base64.getUrlDecoder();
- String payload = new String(decoder.decode(chunks[1]));
- JsonObject jsonObject = JsonParser.parseString(payload).getAsJsonObject();
+ /**
+ * Decodes the client ID from the JWT token.
+ *
+ * @param token The JWT token string.
+ * @return The client ID extracted from the token, or null if decoding fails.
+ */
+ private String decodeClientId(String token) {
+ try {
+ // Split the JWT token to get the payload part
+ String[] chunks = token.split("\\.");
+ Base64.Decoder decoder = Base64.getUrlDecoder();
+ String payload = new String(decoder.decode(chunks[1]));
+ JsonObject jsonObject = JsonParser.parseString(payload).getAsJsonObject();
- // Return the client ID from the payload
- return jsonObject.get("client_id").getAsString();
- } catch (Exception e) {
- // Log an error if decoding fails
- logger.error("Error decoding client ID from token", e);
- return null;
- }
- }
- }
+ // Return the client ID from the payload
+ return jsonObject.get("client_id").getAsString();
+ } catch (Exception e) {
+ // Log an error if decoding fails
+ logger.error("Error decoding client ID from token", e);
+ return null;
+ }
+ }
+}
import java.lang.invoke.MethodHandles;
import java.nio.file.Path;
+import java.time.Duration;
+import static org.awaitility.Awaitility.await;
import static org.junit.jupiter.api.Assertions.assertTrue;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
testHelperTest.testSuccessResponse(responseMono, HttpStatus.CREATED, responseBody ->
responseBody.contains("{\"scope\":{\"ueId\":\"ue5100\",\"qosId\":\"qos5100\"},\"qosObjectives\":{\"priorityLevel\":5100.0}}"));
testHelperTest.testSuccessHeader(responseMono, "location", headerValue -> headerValue.contains(testHelperTest.baseUrl() + "/a1-policy-management/v1/policies/"));
- assertTrue(capturedOutput.getOut().contains("Request received with path: /a1-policy-management/v1/policies"));
- assertTrue(capturedOutput.getOut().contains("the Status code of the response: 201 CREATED"));
- assertTrue(capturedOutput.getOut().contains("the response is:"));
+
+ await().atMost(Duration.ofSeconds(5))
+ .pollInterval(Duration.ofMillis(50))
+ .untilAsserted(() -> {
+ assertTrue(capturedOutput.getOut().contains("Request received with path: /a1-policy-management/v1/policies"));
+ assertTrue(capturedOutput.getOut().contains("the Status code of the response: 201 CREATED"));
+ assertTrue(capturedOutput.getOut().contains("the response is:"));
+ });
}
@Test
@DisplayName("test verify entry exit log for health actuator is present")
- void testHealthActuatorFilterIncluded(CapturedOutput capturedOutput) throws Exception {
+ void testHealthActuatorFilterIncluded(CapturedOutput capturedOutput) {
String url = "/actuator/health";
Mono<ResponseEntity<String>> responseGetHealthMono =
testHelperTest.restClient(testHelperTest.baseUrl(), false).getForEntity(url);
testHelperTest.testSuccessResponse(responseGetHealthMono, HttpStatus.OK, responseBody -> responseBody.contains("UP"));
- assertTrue(capturedOutput.getOut().contains("Request received with path: /actuator/health"));
- assertTrue(capturedOutput.getOut().contains("the response is:"));
+
+ await().atMost(Duration.ofSeconds(5))
+ .pollInterval(Duration.ofMillis(50))
+ .untilAsserted(() -> {
+ assertTrue(capturedOutput.getOut().contains("Request received with path: /actuator/health"));
+ assertTrue(capturedOutput.getOut().contains("the response is:"));
+ });
}
}