Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
package org.eclipse.sw360.http;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.http.HttpRequest.BodyPublisher;
import java.net.http.HttpRequest.BodyPublishers;
import java.nio.file.Path;
Expand Down Expand Up @@ -61,9 +61,7 @@ public void string(String str, String mediaType) {
public void file(Path path, String mediaType) {
try {
initBody(BodyPublishers.ofFile(path));
Path fileNamePath = path.getFileName();
fileName = (fileNamePath != null) ? fileNamePath.toString() : null;
} catch (FileNotFoundException e) {
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) Bosch.IO GmbH 2020.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.sw360.http;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.eclipse.sw360.http.utils.HttpConstants;
import org.junit.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;

/**
* Test class for {@code NewRequestBodyBuilderImpl} file body behavior.
*/
public class NewRequestBodyBuilderImplTest {

@Test
public void testFileBodyWithExistingFile() throws IOException {
NewRequestBodyBuilderImpl bodyBuilder = new NewRequestBodyBuilderImpl(mock(ObjectMapper.class));
Path tempFile = Files.createTempFile("new-request-body", ".txt");
try {
Files.writeString(tempFile, "file-body-content");

bodyBuilder.file(tempFile, HttpConstants.CONTENT_OCTET_STREAM);

assertThat(bodyBuilder.getBody()).isNotNull();
} finally {
Files.deleteIfExists(tempFile);
}
}

@Test
public void testFileBodyWithMissingFile() {
NewRequestBodyBuilderImpl bodyBuilder = new NewRequestBodyBuilderImpl(mock(ObjectMapper.class));
Path missingFile = Path.of("target", "does-not-exist", "missing-upload.bin");

try {
bodyBuilder.file(missingFile, HttpConstants.CONTENT_OCTET_STREAM);
fail("No exception thrown!");
} catch (IllegalStateException ex) {
assertThat(ex.getCause()).isInstanceOf(IOException.class);
}
}
}
Loading