Skip to content

Commit

Permalink
Fixed #1571: Add test for InstagramProfileScraper (#1572)
Browse files Browse the repository at this point in the history
* Add Unit test for InstagramProfileScraper

* Fix Codacy:Remove unused imports

* Add more testing parameters
  • Loading branch information
simsausaurabh authored and singhpratyush committed Apr 12, 2018
1 parent ac50649 commit bcd0f3f
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
4 changes: 3 additions & 1 deletion test/org/loklak/TestRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.loklak.harvester.YoutubeScraperTest;
import org.loklak.api.search.GithubProfileScraperTest;
import org.loklak.api.search.QuoraProfileScraperTest;
import org.loklak.api.search.InstagramProfileScraperTest;

/*
TestRunner for harvesters
Expand All @@ -15,7 +16,8 @@
TwitterScraperTest.class,
YoutubeScraperTest.class,
GithubProfileScraperTest.class,
QuoraProfileScraperTest.class
QuoraProfileScraperTest.class,
InstagramProfileScraperTest.class
})
public class TestRunner {
}
Expand Down
66 changes: 66 additions & 0 deletions test/org/loklak/api/search/InstagramProfileScraperTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.loklak.api.search;

import java.io.BufferedReader;
import java.io.IOException;
import org.junit.Test;
import org.loklak.api.search.InstagramProfileScraper;
import org.loklak.data.DAO;
import org.loklak.http.ClientConnection;
import org.json.JSONArray;
import org.json.JSONObject;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertNotNull;

/**
* These unit-tests test org.loklak.api.search.InstagramProfileScraper.java
*/

public class InstagramProfileScraperTest {

@Test
public void instagramProfileScraperUserTest() {

InstagramProfileScraper instagramScraper = new InstagramProfileScraper();
String url = "https://www.instagram.com/fossasia/";
BufferedReader br = null;

try {
ClientConnection connection = new ClientConnection(url);
//Check Network issue
assertThat(connection.getStatusCode(), is(200));
br = instagramScraper.getHtml(connection);
} catch (IOException e) {
DAO.log("InstagramProfileScraperTest.instagramProfileScraperUserTest() failed to connect to network. url:" + url);
}

JSONArray instaProfile = new JSONArray();
instaProfile = instagramScraper.scrapeInstagram(br, url);

String hostname = "www.instagram.com";
for(int i=0; i<instaProfile.length(); i++) {
JSONObject json = (JSONObject)instaProfile.get(i);
JSONObject entry_data = json.getJSONObject("entry_data");
JSONObject config = json.getJSONObject("config");
try {
assertNotNull(config);
assertNotNull(instaProfile);
assertNotNull(entry_data);
assertTrue(json.has("activity_counts"));
assertTrue(json.has("country_code"));
assertTrue(json.has("platform"));
assertTrue(json.has("language_code"));
assertTrue(json.has("gatekeepers"));
assertTrue(entry_data.has("ProfilePage"));
assertTrue(config.has("viewer"));
assertTrue(config.has("csrf_token"));
assertEquals(json.getString("hostname"), hostname);
} catch (Exception e) {
DAO.log("InstagramProfileScraperTest.instagramProfileScraperUserTest() assert error");
}
}
}
}

0 comments on commit bcd0f3f

Please sign in to comment.