Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Events/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ version '0.1-SNAPSHOT'

apply plugin: 'java'

sourceCompatibility = 1.8

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pourquoi avoir enlevé cette ligne ?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

J'ai enlevé car mon IDE m'a indiqué que la variable en question n'était pas utilisé

repositories {
mavenCentral()
Expand All @@ -12,4 +11,9 @@ repositories {
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile group: 'org.assertj', name: 'assertj-core', version: '3.3.0'
testCompile group: 'org.mockito', name: 'mockito-core', version: '1.10.19'
testCompile group: 'com.github.fakemongo', name: 'fongo', version: '2.0.6'
testCompile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.21'

compile group: 'org.mongodb', name: 'mongo-java-driver', version: '3.2.0'
}
2 changes: 1 addition & 1 deletion Events/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Sun Feb 07 12:17:39 CET 2016
#Wed Apr 13 19:27:49 CEST 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.github.swcraftlyon.meetup.domain;

import com.github.swcraftlyon.meetup.domain.entities.Event;


public interface Planner {

void record(Event event);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.github.swcraftlyon.meetup;
package com.github.swcraftlyon.meetup.domain.entities;


public class Event {

private final String title;

public Event(String title) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.github.swcraftlyon.meetup.domain.services;

import com.github.swcraftlyon.meetup.domain.Planner;
import com.github.swcraftlyon.meetup.domain.entities.Event;


public class EventManager implements IEventManager {

private final Planner planner;

public EventManager(Planner planner) {
this.planner = planner;
}

@Override
public Event initialiseAnEvent(String eventTitle) {
Event event = new Event(eventTitle);
planner.record(event);
return event;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.github.swcraftlyon.meetup.domain.services;

import com.github.swcraftlyon.meetup.domain.entities.Event;


public interface IEventManager {

Event initialiseAnEvent(String eventTitle);
}
28 changes: 28 additions & 0 deletions Events/src/main/java/com/github/swcraftlyon/meetup/infra/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.github.swcraftlyon.meetup.infra;

import com.github.swcraftlyon.meetup.domain.Planner;
import com.github.swcraftlyon.meetup.domain.entities.Event;
import com.github.swcraftlyon.meetup.domain.services.EventManager;
import com.github.swcraftlyon.meetup.domain.services.IEventManager;
import com.github.swcraftlyon.meetup.infra.adapters.repositories.PlannerRepository;
import com.github.swcraftlyon.meetup.infra.dao.EventDAO;
import com.github.swcraftlyon.meetup.infra.dao.mongo.MongoConfiguration;
import com.github.swcraftlyon.meetup.infra.dao.mongo.MongoEventDao;
import com.github.swcraftlyon.meetup.infra.dao.mongo.MongoLauncher;


public class Main {

public static void main(String[] args) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

main inutile dans le contexte

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Il faut bien un endroit où on peut instancier nos objets ? Ça devrait être remplacer par quoi ? Merci d'avance pour ta réponse :)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The answer will be interesting, a java program needs a main to be launched, unless we use a library or framework that has a hidden main.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In our case, we haven't any framework yet? I thought I understood that we don't want to use any framework. at least libraries maybe!?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uniquement des tests. Pas besoin de framework ou de main.

MongoConfiguration mongoConfiguration = new MongoConfiguration("localhost", "27017", "MeetupDB");
//Infra
EventDAO eventDAO = new MongoEventDao(MongoLauncher.getMongoDatabase(mongoConfiguration));
//Adapter
Planner planner = new PlannerRepository(eventDAO);
// Domain Model
IEventManager eventManager = new EventManager(planner);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ligne vide

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tu parles de la lignes 24 ?

Event event = eventManager.initialiseAnEvent("Mix-it Lyon");
System.out.println("Event : " + event.getTitle() + " is created.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.github.swcraftlyon.meetup.infra.adapters.repositories;

import com.github.swcraftlyon.meetup.domain.entities.Event;
import com.github.swcraftlyon.meetup.domain.Planner;
import com.github.swcraftlyon.meetup.infra.dao.EventDAO;
import org.bson.Document;


public class PlannerRepository implements Planner {

private final EventDAO eventDAO;

public PlannerRepository(EventDAO eventDAO) {
this.eventDAO = eventDAO;
}

@Override
public void record(Event event) {
Document document = new Document();
document.append("title", event.getTitle());

eventDAO.save(document);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.github.swcraftlyon.meetup.infra.dao;

import java.util.List;

public interface EventDAO<T> {

void save(T object);

List<T> find();

T findByKey(String key, String valueOfKey);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.github.swcraftlyon.meetup.infra.dao.mongo;

public class MongoConfiguration {

private String dataBaseName;
private String dataBaseHost;
private String dataBasePort;

public MongoConfiguration(String dataBaseHost, String dataBasePort, String dataBaseName) {
this.dataBaseName = dataBaseName;
this.dataBaseHost = dataBaseHost;
this.dataBasePort = dataBasePort;
}

public String getDataBaseName() {
return dataBaseName;
}

public String getDataBaseHost() {
return dataBaseHost;
}

public String getDataBasePort() {
return dataBasePort;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.github.swcraftlyon.meetup.infra.dao.mongo;

import com.github.swcraftlyon.meetup.infra.dao.EventDAO;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import org.bson.Document;

import java.util.ArrayList;
import java.util.List;


public class MongoEventDao implements EventDAO<Document> {


public static final String EVENT_COLLECTION = "event_collection";
private MongoCollection<Document> eventCollection;

public MongoEventDao(final MongoDatabase mongoDatabase) {
eventCollection = mongoDatabase.getCollection(EVENT_COLLECTION);
}

@Override
public void save(Document document) {
eventCollection.insertOne(document);
}

@Override
public List<Document> find() {
List<Document> documentsToReturn = new ArrayList<Document>();
final FindIterable<Document> documents = eventCollection.find();
for (Document document : documents) {
documentsToReturn.add(document);
}
return documentsToReturn;
}

@Override
public Document findByKey(String key, String value) {
return eventCollection.find(Filters.eq(key, value)).first();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.github.swcraftlyon.meetup.infra.dao.mongo;


import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoDatabase;

public class MongoLauncher{

private static MongoClient mongoClient;
private static MongoDatabase mongoDatabase;

public static synchronized MongoClient getMongoClient(MongoConfiguration mongoConfiguration){
if (mongoClient==null){
String uri = new String("mongodb://").concat(mongoConfiguration.getDataBaseHost())
.concat(":").concat(mongoConfiguration.getDataBasePort());

mongoClient = new MongoClient(new MongoClientURI(uri));
}
return mongoClient;
}

public static synchronized MongoDatabase getMongoDatabase(MongoConfiguration mongoConfiguration) {
if(mongoDatabase == null){
mongoDatabase = getMongoClient(mongoConfiguration).getDatabase(mongoConfiguration.getDataBaseName());
}
return mongoDatabase;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.github.swcraftlyon.meetup.domain;

import com.github.swcraftlyon.meetup.domain.entities.Event;
import com.github.swcraftlyon.meetup.domain.services.EventManager;
import com.github.swcraftlyon.meetup.domain.services.IEventManager;
import org.junit.Assert;
import org.mockito.Mockito;
import org.junit.Test;



public class EventManagerTest {

@Test
public void shouldCreateAnEvent() throws Exception {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

les méthodes de test doivent avoir la forme should...when...

le when est très important.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tu peux stp me donner un exemple dans ce cas précis ?

Planner planner = Mockito.mock(Planner.class);
IEventManager eventManager = new EventManager(planner);
Event event = eventManager.initialiseAnEvent("Mix-it Lyon");
Assert.assertEquals(event.getTitle(),"Mix-it Lyon");
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.swcraftlyon.meetup;
package com.github.swcraftlyon.meetup.domain.entities;

import org.junit.Test;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.github.swcraftlyon.meetup.infra.dao;


import com.github.fakemongo.Fongo;
import com.mongodb.client.MongoDatabase;

public class FongoLauncher{

private static MongoDatabase mongoDatabase;

public static synchronized MongoDatabase getMongoDatabase() {
if(mongoDatabase == null){
Fongo fongo = new Fongo("Fake Mongo Server");
mongoDatabase = fongo.getDatabase("meetupTestDB");
}
return mongoDatabase;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.github.swcraftlyon.meetup.infra.dao;

import com.github.swcraftlyon.meetup.infra.dao.mongo.MongoEventDao;
import org.bson.Document;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

public class MongoEventDaoTest {

public static final String DID_YOU_PERSIST = "Did You Persist ?";
public static final String YES_I_DID = "Yes i Did.";

private EventDAO eventDAO;

@Before
public void setUp() throws Exception {
eventDAO = new MongoEventDao(FongoLauncher.getMongoDatabase());
}

@Test
public void shouldPersistWhenCallingSaveMethod() throws Exception {

Document documentToSave = new Document();
documentToSave.append(DID_YOU_PERSIST, YES_I_DID);
eventDAO.save(documentToSave);

Document documentSaved = (Document) eventDAO.findByKey(DID_YOU_PERSIST,YES_I_DID);
Assert.assertEquals(documentSaved.get(DID_YOU_PERSIST, String.class), documentToSave.get(DID_YOU_PERSIST, String.class));
}

@Test
public void shouldFindAtLeastOneEventWhenWeSaveOne() throws Exception {
Document documentToSave = new Document();
documentToSave.append(DID_YOU_PERSIST, YES_I_DID);
eventDAO.save(documentToSave);

final List<Document> results = eventDAO.find();
Assert.assertNotNull(results);
Assert.assertTrue(results.size() > 0);
}

@After
public void tearDown() throws Exception {
FongoLauncher.getMongoDatabase().drop();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.github.swcraftlyon.meetup.infra.repositories;

import com.github.swcraftlyon.meetup.domain.Planner;
import com.github.swcraftlyon.meetup.domain.entities.Event;
import org.junit.Test;
import org.mockito.Mockito;


public class PlannerRepositoryTest {

@Test
public void shouldRecordAnEventWhenCallingRecordMethod() throws Exception {
Planner planner = Mockito.mock(Planner.class);
Event event = new Event("Mix-it Lyon");
planner.record(event);
Mockito.verify(planner).record(event);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

les verify sont à éviter.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Justement sur ce point je pense qu'on devrait faire un meetup.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure to understand @ldez point. Could you elaborate ?

Copy link
Copy Markdown
Author

@aminx4 aminx4 Apr 15, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, I wanted to know what is the best way to test the record method . because this one doesn't do a lot of things instead of filling a document and invoke the persist of the DAO class.

Usually I write an integration test in this case. thank's for your help.

}


}
32 changes: 32 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* This build file was auto generated by running the Gradle 'init' task
* by 'amine' at '13/04/16 19:13' with Gradle 2.5
*
* This generated file contains a commented-out sample Java project to get you started.
* For more details take a look at the Java Quickstart chapter in the Gradle
* user guide available at http://gradle.org/docs/2.5/userguide/tutorial_java_projects.html
*/

/*
// Apply the java plugin to add support for Java
apply plugin: 'java'

// In this section you declare where to find the dependencies of your project
repositories {
// Use 'jcenter' for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}

// In this section you declare the dependencies for your production and test code
dependencies {
// The production code uses the SLF4J logging API at compile time
compile 'org.slf4j:slf4j-api:1.7.12'

// Declare the dependency for your favourite test framework you want to use in your tests.
// TestNG is also supported by the Gradle Test task. Just change the
// testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
// 'test.useTestNG()' to your build script.
testCompile 'junit:junit:4.12'
}
*/