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();
- }
}