|
| 1 | +package cc.arduino.contributions; |
| 2 | + |
| 3 | +import cc.arduino.contributions.libraries.LibrariesIndex; |
| 4 | +import cc.arduino.utils.MultiStepProgress; |
| 5 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 6 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 7 | +import com.fasterxml.jackson.module.mrbean.MrBeanModule; |
| 8 | +import org.junit.After; |
| 9 | +import org.junit.Before; |
| 10 | +import org.junit.Test; |
| 11 | +import processing.app.helpers.FileUtils; |
| 12 | + |
| 13 | +import java.io.File; |
| 14 | +import java.io.FileInputStream; |
| 15 | +import java.io.InputStream; |
| 16 | +import java.net.URL; |
| 17 | + |
| 18 | +import static org.junit.Assert.assertTrue; |
| 19 | + |
| 20 | +public class GzippedJsonDownloaderTest { |
| 21 | + |
| 22 | + private File tempFolder; |
| 23 | + private File tempFile; |
| 24 | + private DownloadableContributionsDownloader downloader; |
| 25 | + |
| 26 | + @Before |
| 27 | + public void setUp() throws Exception { |
| 28 | + tempFolder = FileUtils.createTempFolder(); |
| 29 | + tempFile = File.createTempFile("test", ".json"); |
| 30 | + downloader = new DownloadableContributionsDownloader(tempFolder); |
| 31 | + } |
| 32 | + |
| 33 | + @After |
| 34 | + public void tearDown() throws Exception { |
| 35 | + FileUtils.recursiveDelete(tempFolder); |
| 36 | + FileUtils.recursiveDelete(tempFile); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testJsonDownload() throws Exception { |
| 41 | + new GZippedJsonDownloader(downloader, new URL("http://downloads.arduino.cc/libraries/library_index.json"), new URL("http://downloads.arduino.cc/libraries/library_index.json.gz")).download(tempFile, new MultiStepProgress(1), ""); |
| 42 | + |
| 43 | + InputStream indexIn = new FileInputStream(tempFile); |
| 44 | + ObjectMapper mapper = new ObjectMapper(); |
| 45 | + mapper.registerModule(new MrBeanModule()); |
| 46 | + mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); |
| 47 | + mapper.configure(DeserializationFeature.EAGER_DESERIALIZER_FETCH, true); |
| 48 | + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
| 49 | + LibrariesIndex librariesIndex = mapper.readValue(indexIn, LibrariesIndex.class); |
| 50 | + |
| 51 | + assertTrue(librariesIndex != null); |
| 52 | + } |
| 53 | +} |
0 commit comments