/*-
* ============LICENSE_START======================================================================
* Copyright (C) 2020-2021 Nokia. All rights reserved.
+ * Copyright (C) 2023 Nordix Foundation.
* ===============================================================================================
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
import org.oran.datafile.exceptions.DatafileTaskException;
import org.oran.datafile.exceptions.NonRetryableDatafileTaskException;
import org.oran.datafile.model.FileServerData;
+import org.oran.datafile.oauth2.SecurityContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
private Disposable disposableClient;
protected HttpClient client;
+ private final SecurityContext securityContext;
- public DfcHttpClient(FileServerData fileServerData) {
+ public DfcHttpClient(SecurityContext securityContext, FileServerData fileServerData) {
this.fileServerData = fileServerData;
+ this.securityContext = securityContext;
}
@Override
public void open() throws DatafileTaskException {
logger.trace("Setting httpClient for file download.");
- String authorizationContent = getAuthorizationContent();
- this.client =
- HttpClient.create(pool).keepAlive(true).headers(h -> h.add("Authorization", authorizationContent));
-
- logger.trace("httpClient, auth header was set.");
- }
-
- protected String getAuthorizationContent() throws DatafileTaskException {
- String jwtToken = HttpUtils.getJWTToken(fileServerData);
- if (!jwtToken.isEmpty()) {
- return HttpUtils.jwtAuthContent(jwtToken);
- }
- if (!HttpUtils.isBasicAuthDataFilled(fileServerData)) {
- throw new DatafileTaskException("Not sufficient basic auth data for file.");
+ final String authorizationContent = this.securityContext.getBearerAuthToken();
+ this.client = HttpClient.create(pool).keepAlive(true);
+ if (!authorizationContent.isEmpty()) {
+ this.client = this.client.headers(h -> h.add("Authorization", "Bearer " + authorizationContent));
+ logger.trace("httpClient, auth header was set.");
+ } else if (!this.fileServerData.password.isEmpty()) {
+ String basicAuthContent =
+ HttpUtils.basicAuthContent(this.fileServerData.userId, this.fileServerData.password);
+ this.client = this.client.headers(h -> h.add("Authorization", basicAuthContent));
}
- return HttpUtils.basicAuthContent(this.fileServerData.userId, this.fileServerData.password);
}
@Override
/*-
* ============LICENSE_START======================================================================
* Copyright (C) 2021 Nokia. All rights reserved.
+ * Copyright (C) 2023 Nordix Foundation.
* ===============================================================================================
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
import org.oran.datafile.exceptions.DatafileTaskException;
import org.oran.datafile.exceptions.NonRetryableDatafileTaskException;
import org.oran.datafile.model.FileServerData;
+import org.oran.datafile.oauth2.SecurityContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
private final FileServerData fileServerData;
private final PoolingHttpClientConnectionManager connectionManager;
+ private final SecurityContext securityContext;
- public DfcHttpsClient(FileServerData fileServerData, PoolingHttpClientConnectionManager connectionManager) {
+ public DfcHttpsClient(SecurityContext securityContext, FileServerData fileServerData,
+ PoolingHttpClientConnectionManager connectionManager) {
this.fileServerData = fileServerData;
this.connectionManager = connectionManager;
+ this.securityContext = securityContext;
}
@Override
logger.trace("Prepare to collectFile {}", localFile);
HttpGet httpGet = new HttpGet(HttpUtils.prepareHttpsUri(fileServerData, remoteFile));
- String authorizationContent = getAuthorizationContent();
+ String authorizationContent = this.securityContext.getBearerAuthToken();
if (!authorizationContent.isEmpty()) {
+ httpGet.addHeader("Authorization", "Bearer " + authorizationContent);
+ } else if (!this.fileServerData.password.isEmpty()) {
+ authorizationContent = HttpUtils.basicAuthContent(this.fileServerData.userId, this.fileServerData.password);
httpGet.addHeader("Authorization", authorizationContent);
}
try {
logger.trace("HTTPS collectFile OK");
}
- private String getAuthorizationContent() throws DatafileTaskException {
- String jwtToken = HttpUtils.getJWTToken(fileServerData);
- if (shouldUseBasicAuth(jwtToken)) {
- return HttpUtils.basicAuthContent(this.fileServerData.userId, this.fileServerData.password);
- }
- return HttpUtils.jwtAuthContent(jwtToken);
- }
-
- private boolean shouldUseBasicAuth(String jwtToken) throws DatafileTaskException {
- return basicAuthValidNotPresentOrThrow() && jwtToken.isEmpty();
- }
-
- protected boolean basicAuthValidNotPresentOrThrow() throws DatafileTaskException {
- if (isAuthDataEmpty()) {
- return false;
- }
- if (HttpUtils.isBasicAuthDataFilled(fileServerData)) {
- return true;
- }
- throw new DatafileTaskException("Not sufficient basic auth data for file.");
- }
-
- private boolean isAuthDataEmpty() {
- return this.fileServerData.userId.isEmpty() && this.fileServerData.password.isEmpty();
- }
-
- protected HttpResponse makeCall(HttpGet httpGet) throws IOException, DatafileTaskException {
+ HttpResponse makeCall(HttpGet httpGet) throws IOException, DatafileTaskException {
try {
HttpResponse httpResponse = executeHttpClient(httpGet);
if (isResponseOk(httpResponse)) {
}
}
- protected CloseableHttpResponse executeHttpClient(HttpGet httpGet) throws IOException {
+ CloseableHttpResponse executeHttpClient(HttpGet httpGet) throws IOException {
return httpsClient.execute(httpGet);
}
- protected boolean isResponseOk(HttpResponse httpResponse) {
+ boolean isResponseOk(HttpResponse httpResponse) {
return getResponseCode(httpResponse) == 200;
}
return httpResponse.getStatusLine().getStatusCode();
}
- protected boolean isErrorInConnection(HttpResponse httpResponse) {
+ boolean isErrorInConnection(HttpResponse httpResponse) {
return getResponseCode(httpResponse) >= 400;
}
- protected void processResponse(HttpResponse response, Path localFile) throws IOException {
+ void processResponse(HttpResponse response, Path localFile) throws IOException {
logger.trace("Starting to process response.");
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
logger.trace("Transmission was successful - {} bytes downloaded.", numBytes);
}
- protected long writeFile(Path localFile, InputStream stream) throws IOException {
+ long writeFile(Path localFile, InputStream stream) throws IOException {
return Files.copy(stream, localFile, StandardCopyOption.REPLACE_EXISTING);
}
private static final Logger logger = LoggerFactory.getLogger(HttpUtils.class);
public static final int HTTP_DEFAULT_PORT = 80;
public static final int HTTPS_DEFAULT_PORT = 443;
- public static final String JWT_TOKEN_NAME = "access_token";
- public static final String AUTH_JWT_WARN = "Both JWT token and Basic auth data present. Omitting basic auth info.";
- public static final String AUTH_JWT_ERROR =
- "More than one JWT token present in the queryParameters. Omitting JWT token.";
private HttpUtils() {
}
return statusCode >= 200 && statusCode < 300;
}
- public static boolean isBasicAuthDataFilled(final FileServerData fileServerData) {
- return !fileServerData.userId.isEmpty() && !fileServerData.password.isEmpty();
- }
-
public static String basicAuthContent(String username, String password) {
return "Basic " + Base64.getEncoder().encodeToString((username + ":" + password).getBytes());
}
- public static String jwtAuthContent(String token) {
- return "Bearer " + token;
- }
-
/**
* Prepare uri to retrieve file from xNF using HTTP connection. If JWT token was
* included
*/
public static String prepareUri(String scheme, FileServerData fileServerData, String remoteFile, int defaultPort) {
int port = fileServerData.port != null ? fileServerData.port : defaultPort;
- String query = rewriteQueryWithoutToken(fileServerData.queryParameters);
+ String query = queryParametersAsString(fileServerData.queryParameters);
String fragment = fileServerData.uriRawFragment;
if (!query.isEmpty()) {
query = "?" + query;
}
/**
- * Returns JWT token string (if single exist) from the queryParameters.
- *
- * @param fileServerData file server data which contain queryParameters where
- * JWT token may exist
- * @return JWT token value if single token entry exist or empty string
- * elsewhere.
- * If JWT token key has no value, empty string will be returned.
- */
- public static String getJWTToken(FileServerData fileServerData) {
-
- if (fileServerData.queryParameters.isEmpty()) {
- return "";
- }
- boolean jwtTokenKeyPresent = HttpUtils.isQueryWithSingleJWT(fileServerData.queryParameters);
- if (!jwtTokenKeyPresent) {
- return "";
- }
- String token = HttpUtils.getJWTToken(fileServerData.queryParameters);
- if (HttpUtils.isBasicAuthDataFilled(fileServerData)) {
- logger.warn(HttpUtils.AUTH_JWT_WARN);
- }
- return token;
- }
-
- /**
- * Checks if the queryParameters contains single JWT token entry. Valid
- * queryParameters
- * contains only one token entry.
- *
- * @param query queryParameters
- * @return true if queryParameters contains single token
- */
- public static boolean isQueryWithSingleJWT(List<NameValuePair> query) {
- if (query == null) {
- return false;
- }
- int i = getJWTTokenCount(query);
- if (i == 0) {
- return false;
- }
- if (i > 1) {
- logger.error(AUTH_JWT_ERROR);
- return false;
- }
- return true;
- }
-
- /**
- * Returns the number of JWT token entries. Valid queryParameters contains only
- * one token entry.
*
- * @param queryElements elements of the queryParameters
- * @return true if queryParameters contains single JWT token entry
- */
- public static int getJWTTokenCount(List<NameValuePair> queryElements) {
- int i = 0;
- for (NameValuePair element : queryElements) {
- if (element.getName().equals(JWT_TOKEN_NAME)) {
- i++;
- }
- }
- return i;
- }
-
- private static String getJWTToken(List<NameValuePair> query) {
- for (NameValuePair element : query) {
- if (!element.getName().equals(JWT_TOKEN_NAME)) {
- continue;
- }
- if (element.getValue() != null) {
- return element.getValue();
- }
- return "";
- }
- return "";
- }
-
- /**
- * Rewrites HTTP queryParameters without JWT token
*
* @param query list of NameValuePair of elements sent in the queryParameters
* @return String representation of queryParameters elements which were provided
* in the input
- * Empty string is possible when queryParameters is empty or contains
- * only access_token key.
+ * Empty string is possible when queryParameters is empty.
*/
- public static String rewriteQueryWithoutToken(List<NameValuePair> query) {
+ private static String queryParametersAsString(List<NameValuePair> query) {
if (query.isEmpty()) {
return "";
}
StringBuilder sb = new StringBuilder();
for (NameValuePair nvp : query) {
- if (nvp.getName().equals(JWT_TOKEN_NAME)) {
- continue;
- }
sb.append(nvp.getName());
if (nvp.getValue() != null) {
sb.append("=");
import org.oran.datafile.model.FileData;
import org.oran.datafile.model.FilePublishInformation;
import org.oran.datafile.model.FileReadyMessage;
+import org.oran.datafile.oauth2.SecurityContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
private final DataStore dataStore;
+ private final SecurityContext securityContext;
+
/**
* Constructor for task registration in Datafile Workflow.
*
* @param applicationConfiguration - application configuration
*/
- public CollectAndReportFiles(AppConfig applicationConfiguration) {
+ public CollectAndReportFiles(SecurityContext securityContext, AppConfig applicationConfiguration) {
this.appConfig = applicationConfiguration;
this.kafkaSender = KafkaSender.create(kafkaSenderOptions());
+ this.securityContext = securityContext;
initCerts();
this.dataStore = DataStore.create(applicationConfiguration);
}
private Flux<SenderResult<Integer>> sendDataToKafkaStream(Flux<SenderRecord<String, String, Integer>> dataToSend) {
-
return kafkaSender.send(dataToSend) //
.doOnError(e -> logger.error("Send to kafka failed", e));
}
}
protected FileCollector createFileCollector() {
- return new FileCollector(appConfig, counters);
+ return new FileCollector(securityContext, appConfig, counters);
}
private Mono<FilePublishInformation> fetchFile(FileData fileData) {
import org.oran.datafile.model.FileData;
import org.oran.datafile.model.FilePublishInformation;
import org.oran.datafile.model.FileReadyMessage;
+import org.oran.datafile.oauth2.SecurityContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
private static final Logger logger = LoggerFactory.getLogger(FileCollector.class);
private final AppConfig appConfig;
private final Counters counters;
+ private final SecurityContext securityContext;
/**
* Constructor.
*
* @param appConfig application configuration
*/
- public FileCollector(AppConfig appConfig, Counters counters) {
+ public FileCollector(SecurityContext securityContext, AppConfig appConfig, Counters counters) {
this.appConfig = appConfig;
this.counters = counters;
+ this.securityContext = securityContext;
}
/**
}
protected FileCollectClient createHttpClient(FileData fileData) {
- return new DfcHttpClient(fileData.fileServerData());
+ return new DfcHttpClient(securityContext, fileData.fileServerData());
}
protected FileCollectClient createHttpsClient(FileData fileData) throws DatafileTaskException {
- return new DfcHttpsClient(fileData.fileServerData(), HttpsClientConnectionManagerUtil.instance());
+ return new DfcHttpsClient(securityContext, fileData.fileServerData(),
+ HttpsClientConnectionManagerUtil.instance());
}
}
import org.oran.datafile.model.FilePublishInformation;
import org.oran.datafile.model.FileReadyMessage;
import org.oran.datafile.model.FileReadyMessage.MessageMetaData;
+import org.oran.datafile.oauth2.SecurityContext;
import org.oran.datafile.tasks.CollectAndReportFiles;
import org.oran.datafile.tasks.FileCollector;
import org.oran.datafile.tasks.KafkaTopicListener;
@Autowired
CollectAndReportFiles scheduledTask;
+ static final SecurityContext securityContext = new SecurityContext("");
+
private static KafkaReceiver kafkaReceiver;
private static class KafkaReceiver {
final AppConfig appConfig;
public FileCollectorMock(AppConfig appConfig) {
- super(appConfig, new Counters());
+ super(securityContext, appConfig, new Counters());
this.appConfig = appConfig;
}
@Override // (override fetchFile to disable the actual file fetching)
public Mono<FilePublishInformation> collectFile(FileData fileData, long numRetries, Duration firstBackoff) {
- FileCollector fc = new FileCollector(this.appConfig, new Counters());
+ FileCollector fc = new FileCollector(securityContext, this.appConfig, new Counters());
FilePublishInformation i = fc.createFilePublishInformation(fileData);
try {
final AppConfig appConfig;
public CollectAndReportFilesMock(AppConfig appConfig) {
- super(appConfig);
+ super(securityContext, appConfig);
this.appConfig = appConfig;
}
import java.io.ByteArrayInputStream;
import java.io.InputStream;
-import java.net.URISyntaxException;
import java.nio.file.Path;
-import org.apache.hc.core5.net.URIBuilder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
-import org.mockito.ArgumentMatchers;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.oran.datafile.exceptions.DatafileTaskException;
import org.oran.datafile.model.FileServerData;
+import org.oran.datafile.oauth2.SecurityContext;
import reactor.core.publisher.Flux;
import reactor.netty.http.client.HttpClientConfig;
private static final String PASSWORD = "123";
private static final String XNF_ADDRESS = "127.0.0.1";
private static final int PORT = 80;
- private static final String JWT_PASSWORD = "thisIsThePassword";
- private static String ACCESS_TOKEN = "access_token";
@Mock
private Path pathMock;
@BeforeEach
public void setup() {
- dfcHttpClientSpy = spy(new DfcHttpClient(createFileServerData()));
+ SecurityContext ctx = new SecurityContext("");
+ dfcHttpClientSpy = spy(new DfcHttpClient(ctx, createFileServerData()));
}
@Test
assertEquals(HttpUtils.basicAuthContent(USERNAME, PASSWORD), config.headers().get("Authorization"));
}
- @Test
- void openConnection_failedBasicAuthSetupThrowException() {
- FileServerData serverData =
- FileServerData.builder().serverAddress(XNF_ADDRESS).userId(USERNAME).password("").port(PORT).build();
-
- DfcHttpClient dfcHttpClientSpy = spy(new DfcHttpClient(serverData));
-
- assertThatThrownBy(() -> dfcHttpClientSpy.open())
- .hasMessageContaining("Not sufficient basic auth data for file.");
- }
-
@Test
void collectFile_AllOk() throws Exception {
String REMOTE_FILE = "any";
verify(dfcHttpClientSpy, times(1)).isDownloadFailed(any());
}
- @Test
- void collectFile_AllOkWithJWTToken() throws Exception {
- dfcHttpClientSpy = spy(new DfcHttpClient(fileServerDataWithJWTToken()));
- String REMOTE_FILE = "any";
- Flux<InputStream> fis = Flux.just(new ByteArrayInputStream("ReturnedString".getBytes()));
-
- dfcHttpClientSpy.open();
- HttpClientConfig config = dfcHttpClientSpy.client.configuration();
- assertEquals(HttpUtils.jwtAuthContent(JWT_PASSWORD), config.headers().get("Authorization"));
-
- when(dfcHttpClientSpy.getServerResponse(any())).thenReturn(fis);
- doReturn(false).when(dfcHttpClientSpy).isDownloadFailed(any());
-
- dfcHttpClientSpy.collectFile(REMOTE_FILE, pathMock);
- dfcHttpClientSpy.close();
-
- verify(dfcHttpClientSpy, times(1)).getServerResponse(ArgumentMatchers.eq(REMOTE_FILE));
- verify(dfcHttpClientSpy, times(1)).processDataFromServer(any(), any(), any());
- verify(dfcHttpClientSpy, times(1)).isDownloadFailed(any());
- }
-
@Test
void collectFile_No200ResponseWriteToErrorMessage() throws DatafileTaskException {
String ERROR_RESPONSE = "This is unexpected message";
return FileServerData.builder().serverAddress(XNF_ADDRESS).userId(USERNAME).password(PASSWORD).port(PORT)
.build();
}
-
- private FileServerData fileServerDataWithJWTToken() throws URISyntaxException {
- String query = "?" + ACCESS_TOKEN + "=" + JWT_PASSWORD;
-
- return FileServerData.builder().serverAddress(XNF_ADDRESS).userId("").password("").port(PORT)
- .queryParameters(new URIBuilder(query).getQueryParams()).build();
- }
}
*/
package org.oran.datafile.http;
-import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import org.oran.datafile.exceptions.DatafileTaskException;
import org.oran.datafile.exceptions.NonRetryableDatafileTaskException;
import org.oran.datafile.model.FileServerData;
+import org.oran.datafile.oauth2.SecurityContext;
@ExtendWith(MockitoExtension.class)
class DfcHttpsClientTest {
@BeforeEach
public void setup() {
- dfcHttpsClientSpy = spy(new DfcHttpsClient(createFileServerData(), connectionManager));
- }
-
- @Test
- void fileServerData_properLocationBasicAuth() throws Exception {
- boolean result = dfcHttpsClientSpy.basicAuthValidNotPresentOrThrow();
- assertEquals(true, result);
- }
-
- @Test
- void fileServerData_properLocationNoBasicAuth() throws Exception {
- dfcHttpsClientSpy = spy(new DfcHttpsClient(emptyUserInFileServerData(), connectionManager));
-
- boolean result = dfcHttpsClientSpy.basicAuthValidNotPresentOrThrow();
- assertEquals(false, result);
- }
-
- @Test
- void fileServerData_improperAuthDataExceptionOccurred() throws Exception {
- dfcHttpsClientSpy = spy(new DfcHttpsClient(invalidUserInFileServerData(), connectionManager));
-
- assertThrows(DatafileTaskException.class, () -> dfcHttpsClientSpy.basicAuthValidNotPresentOrThrow());
+ SecurityContext ctx = new SecurityContext("");
+ dfcHttpsClientSpy = spy(new DfcHttpsClient(ctx, createFileServerData(), connectionManager));
}
@Test
@Test
void dfcHttpsClient_flow_successfulCallWithJWTAndResponseProcessing() throws Exception {
FileServerData serverData = jWTTokenInFileServerData();
- dfcHttpsClientSpy = spy(new DfcHttpsClient(serverData, connectionManager));
+ SecurityContext ctx = new SecurityContext("");
+ dfcHttpsClientSpy = spy(new DfcHttpsClient(ctx, serverData, connectionManager));
doReturn(HttpClientResponseHelper.APACHE_RESPONSE_OK).when(dfcHttpsClientSpy)
.executeHttpClient(any(HttpGet.class));
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.net.URISyntaxException;
-import java.util.List;
-import org.apache.hc.core5.http.NameValuePair;
import org.apache.hc.core5.net.URIBuilder;
import org.junit.jupiter.api.Test;
import org.oran.datafile.model.FileServerData;
private static final String XNF_ADDRESS = "127.0.0.1";
private static final int PORT = 443;
- private static final String JWT_PASSWORD = "thisIsThePassword";
- private static final String ACCESS_TOKEN = "access_token";
- private static final String ANOTHER_TOKEN = "another_token";
- private static final String ANOTHER_DATA = "another_data";
private static final String FRAGMENT = "thisIsTheFragment";
private static final String USERNAME = "bob";
private static final String PASSWORD = "123";
assertFalse(HttpUtils.isSuccessfulResponseCodeWithDataRouter(404));
}
- @Test
- void isSingleQueryWithJWT_validToken() throws URISyntaxException {
- assertTrue(HttpUtils.isQueryWithSingleJWT(validTokenSingleQueryData()));
- assertTrue(HttpUtils.isQueryWithSingleJWT(validTokenDoubleQueryData()));
- }
-
- @Test
- void isSingleQueryWithJWT_invalidToken() throws URISyntaxException {
- assertFalse(HttpUtils.isQueryWithSingleJWT(validQueryNoToken()));
- assertFalse(HttpUtils.isQueryWithSingleJWT(queryDataDoubleToken()));
- assertFalse(HttpUtils.isQueryWithSingleJWT(null));
- }
-
- @Test
- void getJWTToken_jWTTokenPresent() throws URISyntaxException {
- assertEquals(JWT_PASSWORD, HttpUtils.getJWTToken(fileServerDataWithJWTToken()));
- assertEquals(JWT_PASSWORD, HttpUtils.getJWTToken(fileServerDataWithJWTTokenLongQueryAndFragment()));
- }
-
- @Test
- void getJWTToken_JWTTokenNotPresent() throws URISyntaxException {
- assertEquals("", HttpUtils.getJWTToken(fileServerDataQueryWithoutToken()));
- }
-
@Test
void prepareUri_UriWithoutPort() {
FileServerData serverData =
assertTrue(retrievedUri.startsWith("http://" + XNF_ADDRESS + ":80"));
}
- @Test
- void prepareUri_verifyUriWithTokenAndFragment() throws URISyntaxException {
- String file = "/file";
- String expected = "http://" + XNF_ADDRESS + ":" + PORT + file + "?" + ANOTHER_TOKEN + "=" + ANOTHER_DATA + "&"
- + ANOTHER_TOKEN + "=" + ANOTHER_DATA + "&" + ANOTHER_TOKEN + "=" + ANOTHER_DATA + "#" + FRAGMENT;
- assertEquals(expected,
- HttpUtils.prepareUri("http", fileServerDataWithJWTTokenLongQueryAndFragment(), file, 443));
- }
-
@Test
void prepareUri_verifyUriWithoutTokenAndWithoutFragment() throws URISyntaxException {
String file = "/file";
assertEquals(expected, HttpUtils.prepareUri("http", fileServerDataNoTokenNoFragment(), file, 443));
}
- private List<NameValuePair> validTokenSingleQueryData() throws URISyntaxException {
- String query = "?" + ACCESS_TOKEN + "=" + JWT_PASSWORD;
- return new URIBuilder(query).getQueryParams();
- }
-
- private List<NameValuePair> validTokenDoubleQueryData() throws URISyntaxException {
- StringBuilder doubleQuery = new StringBuilder();
- doubleQuery.append("?" + ANOTHER_TOKEN + "=" + ANOTHER_DATA + "&");
- doubleQuery.append(ACCESS_TOKEN + "=" + JWT_PASSWORD);
- return new URIBuilder(doubleQuery.toString()).getQueryParams();
- }
-
- private List<NameValuePair> validQueryNoToken() throws URISyntaxException {
- String query = "?" + ANOTHER_TOKEN + "=" + JWT_PASSWORD;
- return new URIBuilder(query).getQueryParams();
- }
-
- private List<NameValuePair> queryDataDoubleToken() throws URISyntaxException {
- StringBuilder doubleToken = new StringBuilder();
- doubleToken.append("?" + ACCESS_TOKEN + "=" + JWT_PASSWORD + "&");
- doubleToken.append(ACCESS_TOKEN + "=" + JWT_PASSWORD + "&");
- doubleToken.append(ANOTHER_TOKEN + "=" + ANOTHER_DATA);
- return new URIBuilder(doubleToken.toString()).getQueryParams();
- }
-
- private FileServerData fileServerDataWithJWTToken() throws URISyntaxException {
- String query = "?" + ACCESS_TOKEN + "=" + JWT_PASSWORD;
-
- return FileServerData.builder().serverAddress(XNF_ADDRESS).userId("").password("").port(PORT)
- .queryParameters(new URIBuilder(query).getQueryParams()).build();
- }
-
- private FileServerData fileServerDataWithJWTTokenLongQueryAndFragment() throws URISyntaxException {
- StringBuilder query = new StringBuilder();
- query.append("?" + ANOTHER_TOKEN + "=" + ANOTHER_DATA + "&");
- query.append(ANOTHER_TOKEN + "=" + ANOTHER_DATA + "&");
- query.append(ACCESS_TOKEN + "=" + JWT_PASSWORD + "&");
- query.append(ANOTHER_TOKEN + "=" + ANOTHER_DATA);
-
- return FileServerData.builder().serverAddress(XNF_ADDRESS).userId("").password("").port(PORT)
- .queryParameters(new URIBuilder(query.toString()).getQueryParams()).uriRawFragment(FRAGMENT).build();
- }
-
- private FileServerData fileServerDataQueryWithoutToken() throws URISyntaxException {
- StringBuilder query = new StringBuilder();
- query.append("?" + ANOTHER_TOKEN + "=" + ANOTHER_DATA);
-
- return FileServerData.builder().serverAddress(XNF_ADDRESS).userId("").password("").port(PORT)
- .queryParameters(new URIBuilder(query.toString()).getQueryParams()).build();
- }
-
private FileServerData fileServerDataNoTokenNoFragment() throws URISyntaxException {
return FileServerData.builder().serverAddress(XNF_ADDRESS).userId("").password("").port(PORT)
.queryParameters(new URIBuilder("").getQueryParams()).uriRawFragment("").build();
import org.oran.datafile.model.FileData;
import org.oran.datafile.model.FilePublishInformation;
import org.oran.datafile.model.FileReadyMessage;
+import org.oran.datafile.oauth2.SecurityContext;
import reactor.test.StepVerifier;
public class FileCollectorTest {
private Counters counters;
+ static final SecurityContext securityContext = new SecurityContext("");
+
FileReadyMessage.Event event(String location) {
FileReadyMessage.MessageMetaData messageMetaData = FileReadyMessage.MessageMetaData.builder() //
.lastEpochMicrosec(LAST_EPOCH_MICROSEC) //
@Test
public void whenFtpesFile_returnCorrectResponse() throws Exception {
- FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock, counters));
+ FileCollector collectorUndetTest = spy(new FileCollector(securityContext, appConfigMock, counters));
doReturn(ftpesClientMock).when(collectorUndetTest).createFtpesClient(any());
FileData fileData = createFileData(FTPES_LOCATION_NO_PORT);
@Test
public void whenSftpFile_returnCorrectResponse() throws Exception {
- FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock, counters));
+ FileCollector collectorUndetTest = spy(new FileCollector(securityContext, appConfigMock, counters));
doReturn(sftpClientMock).when(collectorUndetTest).createSftpClient(any());
FileData fileData = createFileData(SFTP_LOCATION_NO_PORT);
@Test
public void whenHttpFile_returnCorrectResponse() throws Exception {
- FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock, counters));
+ FileCollector collectorUndetTest = spy(new FileCollector(securityContext, appConfigMock, counters));
doReturn(dfcHttpClientMock).when(collectorUndetTest).createHttpClient(any());
FileData fileData = createFileData(HTTP_LOCATION_NO_PORT);
@Test
public void whenHttpsFile_returnCorrectResponse() throws Exception {
- FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock, counters));
+ FileCollector collectorUndetTest = spy(new FileCollector(securityContext, appConfigMock, counters));
doReturn(dfcHttpsClientMock).when(collectorUndetTest).createHttpsClient(any());
FileData fileData = createFileData(HTTPS_LOCATION_NO_PORT);
@Test
public void whenFtpesFileAlwaysFail_retryAndFail() throws Exception {
- FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock, counters));
+ FileCollector collectorUndetTest = spy(new FileCollector(securityContext, appConfigMock, counters));
doReturn(ftpesClientMock).when(collectorUndetTest).createFtpesClient(any());
FileData fileData = createFileData(FTPES_LOCATION);
@Test
public void whenFtpesFileAlwaysFail_failWithoutRetry() throws Exception {
- FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock, counters));
+ FileCollector collectorUndetTest = spy(new FileCollector(securityContext, appConfigMock, counters));
doReturn(ftpesClientMock).when(collectorUndetTest).createFtpesClient(any());
FileData fileData = createFileData(FTPES_LOCATION);
@Test
public void whenFtpesFileFailOnce_retryAndReturnCorrectResponse() throws Exception {
- FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock, counters));
+ FileCollector collectorUndetTest = spy(new FileCollector(securityContext, appConfigMock, counters));
doReturn(ftpesClientMock).when(collectorUndetTest).createFtpesClient(any());
doThrow(new DatafileTaskException("Unable to collect file.")).doNothing().when(ftpesClientMock)
.collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION);