Skip to content

Commit

Permalink
Merge pull request #58
Browse files Browse the repository at this point in the history
weblocopenercore-308
  • Loading branch information
benchdoos authored Oct 28, 2023
2 parents a186722 + 97d62d9 commit abc2fbe
Show file tree
Hide file tree
Showing 9 changed files with 311 additions and 21 deletions.
28 changes: 26 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>17</java.version>
<weblocopener.core.version>2.0.1</weblocopener.core.version>
<weblocopener.core.version>2.0.2</weblocopener.core.version>
<copyright>Copyright © 2016-2023 Eugene Zrazhevsky</copyright>
<org.mapstruct.version>1.5.3.Final</org.mapstruct.version>
<lombok.version>1.18.26</lombok.version>
Expand All @@ -53,6 +53,10 @@

<application.mainClass>com.github.benchdoos.weblocopener.Main</application.mainClass>
<log4j.version>2.17.2</log4j.version>
<wiremock.version>3.0.1</wiremock.version>
<mockito-junit-jupiter.version>4.5.1</mockito-junit-jupiter.version>
<junit-jupiter.version>5.9.2</junit-jupiter.version>
<easy-random.version>5.0.0</easy-random.version>
</properties>

<scm>
Expand Down Expand Up @@ -486,9 +490,29 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.9.2</version>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${mockito-junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock</artifactId>
<version>${wiremock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jeasy</groupId>
<artifactId>easy-random-core</artifactId>
<version>${easy-random.version}</version>
<scope>test</scope>
</dependency>


</dependencies>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

@Log4j2
@Setter
Expand All @@ -23,7 +23,7 @@ public class FileDownloader implements ActionListenerSupport {

/** Initial buffer size. */
private static final int BUFFER_SIZE = 8192;
final List<ActionListener<Integer>> listeners = new CopyOnWriteArrayList<>();
final Set<ActionListener<Integer>> listeners = new CopyOnWriteArraySet<>();
private final URL link;
private final File file;
private Long totalFileSize;
Expand Down Expand Up @@ -80,7 +80,11 @@ private void createFolderIfNotExist(final File file) {

@Override
public void addListener(final ActionListener actionListener) {
listeners.add(actionListener);
if (!listeners.contains(actionListener)) {
listeners.add(actionListener);
return;
}
log.warn("ActionListener already registered for this FileDownloader");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.github.benchdoos.weblocopener.base;

import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
import org.jeasy.random.EasyRandom;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.io.TempDir;

import java.io.IOException;
import java.net.ServerSocket;
import java.nio.file.Path;

import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;

@Tag("weblocopener-unit-test")
public abstract class BaseUnitTest {

@TempDir
public static Path tempDir;
public static final WireMockConfiguration wiremockConfig =
wireMockConfig().port(findAvailablePort(8080));

@RegisterExtension
public static WireMockExtension wireMockServer = WireMockExtension.newInstance()
.options(wiremockConfig)
.build();

public final EasyRandom easyRandom = new EasyRandom();


static {
WireMock.configureFor(new WireMock(wireMockServer));
}


private static int findAvailablePort(int desiredPort) {
int port = desiredPort;
while (port < 65536) {
try {
ServerSocket serverSocket = new ServerSocket(port);
serverSocket.close();
return port;
} catch (final IOException e) {
// Port is not available, try the next one
port++;
}
}
throw new RuntimeException("No available port found.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.github.benchdoos.weblocopener.base;

public abstract class WiremockBaseUnitTest extends BaseUnitTest {
public String wiremockAddress = wireMockServer.baseUrl();


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.github.benchdoos.weblocopener.base.utils;

import java.net.URL;

public class FileUtils {

public URL getResourceURI(final String resourcePath) {
final ClassLoader classLoader = FileUtils.class.getClassLoader();
return classLoader.getResource(resourcePath);
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.benchdoos.weblocopener.update;

import com.github.benchdoos.weblocopener.config.BaseUnitTest;
import com.github.benchdoos.weblocopener.base.BaseUnitTest;
import com.github.benchdoos.weblocopener.utils.UpdateHelperUtil;
import com.github.benchdoos.weblocopenercore.domain.version.UpdateInfo;
import org.apache.commons.collections.CollectionUtils;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
package com.github.benchdoos.weblocopener.utils;

import com.github.benchdoos.weblocopener.base.WiremockBaseUnitTest;
import com.github.benchdoos.weblocopenercore.service.actions.ActionListener;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Objects;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.status;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;

class FileDownloaderTest extends WiremockBaseUnitTest {

@AfterEach
void fileCleanUp() {
//noinspection ResultOfMethodCallIgnored
Arrays.stream(Objects.requireNonNull(tempDir.toFile().listFiles())).forEach(File::delete);
}

@Test
void downloadMustSucceed() throws IOException {

final String url = "1.txt";

final String testBody = "Test body of downloading file";


final File file = new File(tempDir.toFile(), url);
assertThat(file).doesNotExist();


wireMockServer.stubFor(
get(urlEqualTo("/" + url))
.willReturn(aResponse()
.withBodyFile("1.txt")
.withHeader("Content-Type", "text/plain")
)
);

final FileDownloader fileDownloader = new FileDownloader(new URL(wiremockAddress + "/" + url), file);

assertThatCode(fileDownloader::download).doesNotThrowAnyException();
assertThat(file).exists();
assertThat(FileUtils.readFileToString(file, StandardCharsets.UTF_8)).isEqualTo(testBody);

}

@Test
void downloadMustSucceedAndListenersWorkFine() throws IOException, URISyntaxException {

final String url = "1.txt";

final File file = new File(tempDir.toFile(), url);
assertThat(file).doesNotExist();


wireMockServer.stubFor(
get(urlEqualTo("/" + url))
.willReturn(aResponse()
.withBodyFile("1.txt")
.withHeader("Content-Type", "text/plain")
)
);

final FileDownloader fileDownloader = new FileDownloader(new URL(wiremockAddress + "/" + url), file);
final URL resourceURI = new com.github.benchdoos.weblocopener.base.utils.FileUtils().getResourceURI("__files/1.txt");
fileDownloader.setTotalFileSize(new File(resourceURI.toURI()).length()); // important

final ActionListener<?> actionListener1 = Mockito.mock(ActionListener.class);
final ActionListener<?> actionListener2 = Mockito.mock(ActionListener.class);
fileDownloader.addListener(actionListener1);
fileDownloader.addListener(actionListener2);


assertThatCode(fileDownloader::download).doesNotThrowAnyException();
assertThat(file).exists();

Mockito.verify(actionListener1, Mockito.atLeastOnce()).actionPerformed(Mockito.any());
Mockito.verify(actionListener2, Mockito.atLeastOnce()).actionPerformed(Mockito.any());
}


@Test
void downloadMustFail() throws IOException {

final String url = "1.txt";


final File file = new File(tempDir.toFile(), url);
assertThat(file).doesNotExist();


wireMockServer.stubFor(
get(urlEqualTo("/" + url))
.willReturn(status(404))
);

final FileDownloader fileDownloader = new FileDownloader(new URL(wiremockAddress + "/" + url), file);

assertThatCode(fileDownloader::download).isExactlyInstanceOf(FileNotFoundException.class);
assertThat(file).doesNotExist();

}

@Test
void addListener() throws MalformedURLException {
final FileDownloader fileDownloader = new FileDownloader(new URL("http://localhost/1.zip"), new File("1.zip"));
final ActionListener<?> actionListener = Mockito.mock(ActionListener.class);

fileDownloader.addListener(actionListener);
//checking if the same listener added twice
fileDownloader.addListener(actionListener);

assertThat(fileDownloader.getListeners()).size().isOne();

// Mockito.verify(actionListener, Mockito.atLeastOnce()).actionPerformed(Mockito.any());

}

@Test
void removeListener() throws MalformedURLException {
final FileDownloader fileDownloader = new FileDownloader(new URL("http://localhost/1.zip"), new File("1.zip"));
final ActionListener<?> actionListener1 = Mockito.mock(ActionListener.class);
final ActionListener<?> actionListener2 = Mockito.mock(ActionListener.class);

fileDownloader.addListener(actionListener1);
fileDownloader.addListener(actionListener2);

assertThat(fileDownloader.getListeners()).size().isEqualTo(2);

fileDownloader.removeListener(actionListener1);
assertThat(fileDownloader.getListeners()).size().isOne();

assertThat(fileDownloader.getListeners().stream().toList().get(0)).isEqualTo(actionListener2);
}

@Test
void removeAllListeners() throws MalformedURLException {
final FileDownloader fileDownloader = new FileDownloader(new URL("http://localhost/1.zip"), new File("1.zip"));
final ActionListener<?> actionListener1 = Mockito.mock(ActionListener.class);
final ActionListener<?> actionListener2 = Mockito.mock(ActionListener.class);

fileDownloader.addListener(actionListener1);
fileDownloader.addListener(actionListener2);

assertThat(fileDownloader.getListeners()).size().isEqualTo(2);

fileDownloader.removeAllListeners();

assertThat(fileDownloader.getListeners()).size().isZero();
}

@Test
void getListeners() throws MalformedURLException {
final FileDownloader fileDownloader = new FileDownloader(new URL("http://localhost/1.zip"), new File("1.zip"));
final ActionListener<Integer> actionListener1 = Mockito.mock(ActionListener.class);
final ActionListener<Integer> actionListener2 = Mockito.mock(ActionListener.class);

fileDownloader.addListener(actionListener1);
fileDownloader.addListener(actionListener2);

assertThat(fileDownloader.getListeners()).size().isEqualTo(2);

assertThat(fileDownloader.getListeners()).contains(actionListener1, actionListener2);
}

@Test
void getLink() throws MalformedURLException {
final URL expectedLink = new URL("http://localhost/1.zip");
final FileDownloader fileDownloader = new FileDownloader(expectedLink, new File("1.zip"));
assertThat(fileDownloader.getLink()).isEqualTo(expectedLink);
}

@Test
void getFile() throws MalformedURLException {
final File expectedFile = new File("1.zip");
final FileDownloader fileDownloader = new FileDownloader(new URL("http://localhost/1.zip"), expectedFile);
assertThat(fileDownloader.getFile()).isEqualTo(expectedFile);
}

@Test
void getSetTotalFileSize() throws MalformedURLException {
final FileDownloader fileDownloader = new FileDownloader(new URL("http://localhost/1.zip"), new File("1.zip"));
final long expectedSize = 1024L;
fileDownloader.setTotalFileSize(expectedSize);

assertThat(fileDownloader.getTotalFileSize()).isEqualTo(expectedSize);
}
}
1 change: 1 addition & 0 deletions src/test/resources/__files/1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Test body of downloading file

0 comments on commit abc2fbe

Please sign in to comment.