-
Notifications
You must be signed in to change notification settings - Fork 7
A proposition for an hexagonal architecture implementation #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 |
|---|---|---|
| @@ -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); | ||
| } |
| 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) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. main inutile dans le contexte
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 :)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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!?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ligne vide
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. les méthodes de test doivent avoir la forme le
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
|---|---|---|
| @@ -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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. les
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Justement sur ce point je pense qu'on devrait faire un meetup.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure to understand @ldez point. Could you elaborate ?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| } | ||
|
|
||
|
|
||
| } | ||
| 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' | ||
| } | ||
| */ |
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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é