/******************************************************************************
 *                                                                            *
 *                    Copyright 2017 Subterranean Security                    *
 *                                                                            *
 *  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                                   *
 *                                                                            *
 *      http://www.apache.org/licenses/LICENSE-2.0                            *
 *                                                                            *
 *  Unless required by applicable law or agreed to in writing, software       *
 *  distributed under the License is distributed on an "AS IS" BASIS,         *
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  *
 *  See the License for the specific language governing permissions and       *
 *  limitations under the License.                                            *
 *                                                                            *
 *****************************************************************************/
package com.sandpolis.core.util;

import static com.sandpolis.core.util.NetUtil.checkPort;
import static com.sandpolis.core.util.NetUtil.download;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

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

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

class NetUtilTest {

	@Test
	@DisplayName("Download a small file into memory")
	void download_1() throws IOException {
		byte[] file = download("https://github.com/Subterranean-Security/Sandpolis/blob/master/.gitignore");

		assertNotNull(file);
		assertTrue(file.length > 0);
	}

	@Test
	@DisplayName("Download a small file to the filesystem")
	void download_2(@TempDir Path temp) throws IOException {
		download("https://github.com/Subterranean-Security/Sandpolis/blob/master/.gitignore",
				temp.resolve("test.txt").toFile());

		assertTrue(Files.exists(temp.resolve("test.txt")));
		assertTrue(Files.size(temp.resolve("test.txt")) > 0);
	}

	@Test
	@DisplayName("Try to download a file that is too big for this method")
	void download_3() throws IOException {
		assertThrows(IllegalArgumentException.class,
				() -> download("http://old-releases.ubuntu.com/releases/11.04/ubuntu-11.04-desktop-amd64.iso"));
	}

	@Test
	@DisplayName("Check some well-known ports")
	void checkPort_1() {
		assertTrue(checkPort("www.google.com", 80));
		assertFalse(checkPort("www.google.com", 81));
	}

}

@@ -1771,16 +1771,46 @@
 eption;%0A
+import java.net.ServerSocket;%0A
 import j
@@ -2930,18 +2930,17 @@
 ome 
-well-known
+localhost
  por
@@ -2963,16 +2963,35 @@
 Port_1()
+ throws IOException
  %7B%0A%09%09ass
@@ -2993,19 +2993,20 @@
 %09%09assert
-Tru
+Fals
 e(checkP
@@ -3014,30 +3014,133 @@
 rt(%22
-www.google.com%22, 80));
+127.0.0.1%22, 8923));%0A%0A%09%09try (ServerSocket socket = new ServerSocket(8923)) %7B%0A%09%09%09assertTrue(checkPort(%22127.0.0.1%22, 8923));%0A%09%09%7D%0A
 %0A%09%09a
@@ -3165,27 +3165,24 @@
 rt(%22
-www.google.com
+127.0.0.1
 %22, 8
-1
+923
 ));%0A

Use localhost ports in checkPort_1 to increase test reliability