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

Commit

Permalink
Merge pull request #15 from ApplETS/dev
Browse files Browse the repository at this point in the history
Update : Fix Cooptel
  • Loading branch information
ttauveron authored Jan 14, 2017
2 parents 21e7f0c + fe05e74 commit cc8ac5a
Show file tree
Hide file tree
Showing 14 changed files with 539 additions and 27 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
tmp/**
tmp/**/*
*.tmp
*.bak
*.swp
*~.nib
local.properties
Expand Down
3 changes: 1 addition & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ dependencies {
testCompile 'org.glassfish.jersey.test-framework.providers:jersey-test-framework-provider-grizzly2:2.23.2'

testCompile 'org.mockito:mockito-all:1.10.19'

testCompile 'com.squareup.okhttp3:mockwebserver:3.4.1'
testRuntime "org.slf4j:slf4j-api:1.7.10"
// compile 'org.glassfish.jersey.media:jersey-media-moxy:2.23.2'

}

Expand Down
Binary file added docker-dir/db/applets_api_db.bak
Binary file not shown.
4 changes: 4 additions & 0 deletions setup_db_standalone.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

docker build -t applets_api_db docker-dir/db
docker run --name applets_api_db -p 5432:5432 -e POSTGRES_PASSWORD=postgres -d applets_api_db
2 changes: 2 additions & 0 deletions src/main/java/applets/etsmtl/ca/MyApplicationBinder.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package applets.etsmtl.ca;

import applets.etsmtl.ca.news.db.EventDAO;
import applets.etsmtl.ca.news.db.NouvellesDAO;
import applets.etsmtl.ca.news.db.SourceDAO;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
Expand All @@ -13,6 +14,7 @@ public class MyApplicationBinder extends AbstractBinder {
protected void configure() {
bind(NouvellesDAO.class).to(NouvellesDAO.class);
bind(SourceDAO.class).to(SourceDAO.class);
bind(EventDAO.class).to(EventDAO.class);

}
}
11 changes: 3 additions & 8 deletions src/main/java/applets/etsmtl/ca/cooptel/CooptelResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public ConsommationGlobal getInternetStats(@QueryParam("phase") String phase, @Q
Elements tables = doc.select("table[border=1]");

Pattern detailsPattern = Pattern.compile("<td>(.*)<\\/td><td>(.*)<\\/td><td align=\"RIGHT\">(.*)<\\/td><td align=\"RIGHT\">(.*)<\\/td>");
Pattern totalPattern = Pattern.compile("<td>Quota permis pour la p&eacute;riode<\\/td>.*<td align=\"RIGHT\">(.*)<\\/td>.*<\\/tr>");
Pattern totalPattern = Pattern.compile("<td>Quota permis pour la p&eacute;riode \\(Mo\\)<\\/td>.*<td align=\"RIGHT\">(.*)<\\/td>");
Matcher m = null;

ArrayList<ConsommationDate> consommationDates = new ArrayList<ConsommationDate>();
Expand All @@ -88,14 +88,9 @@ public ConsommationGlobal getInternetStats(@QueryParam("phase") String phase, @Q

ConsommationGlobal global = new ConsommationGlobal();

String s = tables.get(1)
.children()
.html()
.replace("\n", "");

m = totalPattern.matcher(
tables.get(1)
.children()
tables.get(1).select("tbody > tr")
.first()
.html()
.replace("\n", ""));

Expand Down
13 changes: 10 additions & 3 deletions src/main/java/applets/etsmtl/ca/news/EventsResources.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import applets.etsmtl.ca.news.model.Event;
import applets.etsmtl.ca.news.model.Source;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
Expand All @@ -19,14 +20,21 @@
@Path("events")
public class EventsResources {

private final EventDAO eventDAO;
private final SourceDAO sourceDAO;

@Inject
public EventsResources(EventDAO eventDAO, SourceDAO sourceDAO) {
this.eventDAO = eventDAO;
this.sourceDAO = sourceDAO;
}

@GET
@Path("list/{id}")
@Produces({MediaType.APPLICATION_JSON})
public List<Event> getEvents(@PathParam("id") String id) {
List<Event> events = new ArrayList<Event>();

EventDAO eventDAO = new EventDAO();
SourceDAO sourceDAO = new SourceDAO();
Source source = sourceDAO.find(id);

events.addAll(eventDAO.findFollowingEvents(id));
Expand All @@ -39,7 +47,6 @@ public List<Event> getEvents(@PathParam("id") String id) {
@Produces({MediaType.APPLICATION_JSON})
public List<Source> getSources() {

SourceDAO sourceDAO = new SourceDAO();
List<Source> sources = sourceDAO.findByType("facebook");
return sources;
}
Expand Down
1 change: 0 additions & 1 deletion src/main/java/applets/etsmtl/ca/news/NewsResources.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public List<Nouvelle> getNouvelles(@PathParam("key") String key) {
@Path("sources")
@Produces({MediaType.APPLICATION_JSON})
public List<Source> getSources() {
SourceDAO sourceDAO = new SourceDAO();
List<Source> sources = sourceDAO.findAll();
return sources;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
@Path("partners")
public class PartnersResource {

String PARTNERS_URL = "https://clubapplets.ca/partenaires/";
public static String PARTNERS_URL = "https://clubapplets.ca/partenaires/";
String COL_MD_REGEX = "^col-sm-([0-9])$";


Expand All @@ -44,7 +44,7 @@ public ArrayList<Partner> getPartners() {
OkHttpClient client = new OkHttpClient();

Request request = new Request.Builder()
.url("https://clubapplets.ca/partenaires/")
.url(PARTNERS_URL)
.get()
.build();

Expand Down
14 changes: 5 additions & 9 deletions src/main/resources/news/sources.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@
"key": "avioncargo",
"value": "126837807493669"
},
{
"key": "clubcycliste",
"value": "360076647398495"
},
{
"key": "football",
"value": "460299537349517"
Expand Down Expand Up @@ -138,10 +134,6 @@
"key": "eclipse",
"value": "166160473418379"
},
{
"key": "turbulence",
"value": "134552903275289"
},
{
"key": "preci",
"value": "224658708833"
Expand Down Expand Up @@ -205,6 +197,10 @@
{
"key": "cedille",
"value": "502961616461106"
},
{
"key": "lheuristique",
"value": "694397997368489"
}
],
"twitter": [
Expand All @@ -213,4 +209,4 @@
"value": "ets_ddst"
}
]
}
}
90 changes: 90 additions & 0 deletions src/test/java/applets/etsmtl/ca/news/EventsResourcesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package news;

import applets.etsmtl.ca.news.EventsResources;
import applets.etsmtl.ca.news.db.EventDAO;
import applets.etsmtl.ca.news.db.SourceDAO;
import applets.etsmtl.ca.news.model.Event;
import applets.etsmtl.ca.news.model.Source;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import javax.ws.rs.core.Application;
import javax.ws.rs.core.GenericType;
import java.util.ArrayList;
import java.util.List;

import static org.mockito.Mockito.when;

/**
* Created by gnut3ll4 on 8/21/16.
*/
public class EventsResourcesTest extends JerseyTest {

@Mock
private EventDAO eventDAO;

@Mock
private SourceDAO sourceDAO;

@Override
protected Application configure() {
MockitoAnnotations.initMocks(this);
EventsResources resource = new EventsResources(eventDAO, sourceDAO);
ResourceConfig config = new ResourceConfig();
config.register(resource);
return config;
}

@Test
public void testGetEvents() {
String sourceKey = "aeets", message = "Ceci est un test";

Event event = new Event();
event.setId_source(sourceKey);
event.setNom(message);

ArrayList<Event> list = new ArrayList<>();
list.add(event);
list.add(event);
when(eventDAO.findFollowingEvents(sourceKey)).thenReturn(list);

Source source = new Source();
source.setKey(sourceKey);
when(sourceDAO.find(sourceKey)).thenReturn(source);

List<Event> response = target("/events/list/" + sourceKey).request().get(new GenericType<List<Event>>() {
});

Assert.assertTrue(message.equals(response.get(0).getNom()));
Assert.assertTrue(response.size() == list.size());
}

@Test
public void testGetSources() {
ArrayList<Source> sources = new ArrayList<>();

Source source1 = new Source();
source1.setName("source1");
source1.setType("facebook");

sources.add(source1);
sources.add(source1);

when(sourceDAO.findByType("facebook")).thenReturn(sources);

List<Source> response = target("/events/sources").request().get(new GenericType<List<Source>>() {
});

Assert.assertTrue("source1".equals(response.get(0).getName()));
Assert.assertTrue(response.size() == sources.size());

}


}


23 changes: 22 additions & 1 deletion src/test/java/applets/etsmtl/ca/news/NewsResourcesTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package applets.etsmtl.ca.news;
package news;

import applets.etsmtl.ca.news.NewsResources;
import applets.etsmtl.ca.news.db.NouvellesDAO;
import applets.etsmtl.ca.news.db.SourceDAO;
import applets.etsmtl.ca.news.model.Nouvelle;
Expand Down Expand Up @@ -60,6 +61,26 @@ public void testGetNouvelles() {
Assert.assertTrue(response.size() == list.size());
}

@Test
public void testGetSources() {
ArrayList<Source> sources = new ArrayList<>();

Source source1 = new Source();
source1.setName("source1");

sources.add(source1);
sources.add(source1);

when(sourceDAO.findAll()).thenReturn(sources);

List<Source> response = target("/news/sources").request().get(new GenericType<List<Source>>() {
});

Assert.assertTrue("source1".equals(response.get(0).getName()));
Assert.assertTrue(response.size() == sources.size());

}


}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package partners;

import applets.etsmtl.ca.news.model.Event;
import applets.etsmtl.ca.partners.Partner;
import applets.etsmtl.ca.partners.PartnersResource;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Assert;
import org.junit.Test;

import javax.ws.rs.core.Application;
import javax.ws.rs.core.GenericType;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

/**
* Created by gnut3ll4 on 8/21/16.
*/
public class PartnersResourcesTest extends JerseyTest {

@Override
protected Application configure() {
return new ResourceConfig(PartnersResource.class);
}

@Test
public void testGetPartners() {
MockWebServer server = new MockWebServer();

try {
final String filePath = getClass().getResource("/clubapplets_partners_body.html").getPath();
String partnersHtml = readFile(filePath, StandardCharsets.UTF_8);

server.enqueue(new MockResponse().setBody(partnersHtml));
server.start();
String url = server.url("/partners").toString();
PartnersResource.PARTNERS_URL = url;

List<Partner> response = target("/partners").request().get(new GenericType<List<Partner>>() {
});

Assert.assertTrue(response.size() > 0);

PartnersResource.PARTNERS_URL = "";

//Test caching
response = target("/partners").request().get(new GenericType<List<Partner>>() {
});
Assert.assertTrue(response.size() > 0);

server.shutdown();
} catch (IOException e) {
e.printStackTrace();
}
}

String readFile(String path, Charset encoding) throws IOException {
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}


}


Loading

0 comments on commit cc8ac5a

Please sign in to comment.