Skip to content
This repository has been archived by the owner on Dec 5, 2020. It is now read-only.

Commit

Permalink
IndexSite step + unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Sep 18, 2016
1 parent 0d19ade commit da90dea
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 21 deletions.
Expand Up @@ -26,15 +26,11 @@
package com.amihaiemil.charles.github;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import javax.json.JsonObject;

import org.slf4j.Logger;

import com.amihaiemil.charles.steps.Step;

/**
Expand Down Expand Up @@ -192,7 +188,7 @@ private Step indexSteps(Command com, CommandCategory category, boolean singlePag
.authorOwnerCheck(new AuthorOwnerCheck(com, repo, this.logger))
.repoNameCheck(new RepoNameCheck(repo, this.logger))
.ghPagesBranchCheck(new GhPagesBranchCheck(repo, this.logger))
.indexSteps(new IndexSteps(com, repo, followup, singlePage))
.indexSteps(new IndexSteps(com, repo, followup, this.logger, singlePage))
.build();

return indexWithPreconditions;
Expand Down
Expand Up @@ -25,8 +25,18 @@

package com.amihaiemil.charles.github;

import java.util.List;

import javax.json.JsonObject;

import org.slf4j.Logger;

import com.amihaiemil.charles.DataExportException;
import com.amihaiemil.charles.GraphCrawl;
import com.amihaiemil.charles.IgnoredPatterns;
import com.amihaiemil.charles.Repository;
import com.amihaiemil.charles.WebCrawl;
import com.amihaiemil.charles.WebPage;
import com.amihaiemil.charles.steps.IndexSite;
import com.amihaiemil.charles.steps.Step;

Expand All @@ -49,6 +59,11 @@ public class IndexSteps implements Step {
*/
private JsonObject repoJson;

/**
* Action's logger.
*/
private Logger logger;

/**
* To perform after the index command has been executed successfully.
*/
Expand All @@ -60,10 +75,11 @@ public class IndexSteps implements Step {
* Constructor.
* @param com Command.
*/
public IndexSteps(Command com, JsonObject repo, Step followup, boolean singlePage) {
public IndexSteps(Command com, JsonObject repo, Step followup, Logger logger, boolean singlePage) {
this.com = com;
this.repoJson = repo;
this.followup = followup;
this.logger = logger;
this.singlePage = singlePage;
}

Expand Down Expand Up @@ -101,15 +117,26 @@ public boolean perform() {
*/
Step indexStep(Command com, String repoName, boolean ghPages, boolean singlePage) {
if(!singlePage) {
if(!ghPages) {
return new IndexSite("http://" + repoName);
String url = "http://" + com.authorLogin() + ".github.io/";
if(!ghPages) {
url = "http://" + repoName;
}
String phantomJsExecPath = System.getProperty("phantomjsExec");
if(phantomJsExecPath == null || "".equals(phantomJsExecPath)) {
phantomJsExecPath = "/usr/local/bin/phantomjs";
}
return new IndexSite("http://" + com.authorLogin() + ".github.io/" + repoName);
WebCrawl siteCrawl = new GraphCrawl(
url, phantomJsExecPath, new IgnoredPatterns(),
new Repository() {
@Override
public void export(List<WebPage> arg0) throws DataExportException {
// TODO to be replaced with AmazonEsRepository once it's implemented.
}
}, 20
);
return new IndexSite(siteCrawl, logger);
}
if(!ghPages) { //here return IndexPage(...)
return new IndexSite("http://" + repoName);
}
return new IndexSite("http://" + com.authorLogin() + ".github.io/" + repoName);
return null; //here return IndexPage(...)

}

Expand Down
Expand Up @@ -25,6 +25,11 @@

package com.amihaiemil.charles.steps;

import org.slf4j.Logger;

import com.amihaiemil.charles.DataExportException;
import com.amihaiemil.charles.WebCrawl;

/**
* Step to index a website.
* @author Mihai Andronache (amihaiemil@gmail.com)
Expand All @@ -33,22 +38,34 @@
*
*/
public class IndexSite implements Step {
/**
* Url to the index page of the website.
*/
private String url;


/**
* Action's logger.
*/
private Logger logger;

/**
* The Web crawl used.
*/
private WebCrawl siteCrawl;

/**
* Constructor.
* @param url The website's url.
*/
public IndexSite(String url) {
this.url = url;
public IndexSite(WebCrawl crawl, Logger logger) {
this.logger = logger;
this.siteCrawl = crawl;
}

@Override
public boolean perform() {
// TODO Auto-generated method stub
try {
this.siteCrawl.crawl();
} catch (DataExportException e) {
logger.error("Exception while crawling the website: " + e.getMessage(), e);
return false;
}
return true;
}

Expand Down
@@ -0,0 +1,66 @@
/*
* Copyright (c) 2016, Mihai Emil Andronache
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1)Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2)Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3)Neither the name of charles-github-ejb nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.amihaiemil.charles.steps;

import org.junit.Test;

import static org.junit.Assert.*;

import org.mockito.Mockito;
import org.slf4j.Logger;

import com.amihaiemil.charles.DataExportException;
import com.amihaiemil.charles.WebCrawl;

/**
* Unit tests for {@link IndexSite}
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 1.0.0
*
*/
public class IndexSiteTestCase {

/**
* IndexSite can perform ok.
*/
@Test
public void performsOk() {
IndexSite is = new IndexSite( Mockito.mock(WebCrawl.class), Mockito.mock(Logger.class));
assertTrue(is.perform());
}

/**
* IndexSite can perform ok.
* @throws Exception - If something goes wrong.
*/
@Test
public void webCrawlThrowsException() throws Exception {
WebCrawl crawl = Mockito.mock(WebCrawl.class);
Mockito.doThrow(new DataExportException("Expected exception; it's ok")).when(crawl).crawl();
IndexSite is = new IndexSite(crawl, Mockito.mock(Logger.class));
assertFalse(is.perform());
}
}

0 comments on commit da90dea

Please sign in to comment.