Commit 3ad3e188 authored by Colin DAMON's avatar Colin DAMON

First live content

First simple versions of the activity domain
Secondary ports exists but they need their adapters
parent 5ea7dcf8
......@@ -2,11 +2,13 @@
Création en live d'une application pour montrer l'apport de valeur pas différents niveaux de tests.
## Partie 1
- **Auteurs** : Hippolyte DURIX && Colin DAMON
- **Date** : 25/08/2020
- **Langage** : Java
- **Niveau** : Moyen
- **Replay** : //TODO
- **Niveau** : Débutant
- **Replay** : [La pyramide de tests de Kheops (partie 1) - Hippolyte et Colin](https://www.youtube.com/watch?v=rfRgJk251pw)
## JHipster
......@@ -54,7 +56,7 @@ The service worker initialization code is commented out by default. To enable it
```html
<script>
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('./service-worker.js').then(function () {
navigator.serviceWorker.register('./service-worker.js').then(function() {
console.log('Service Worker Registered');
});
}
......
package com.ippon.borestop.activity.domain;
public class Activities {
private final IdeasRepository ideas;
private final PartnersRepository partners;
public Activities(IdeasRepository ideas, PartnersRepository partners) {
this.ideas = ideas;
this.partners = partners;
}
public Activity next() {
Idea idea = ideas.next();
Partners matchingPartners = partners.find(idea.getCategory());
return new Activity(idea, matchingPartners);
}
}
package com.ippon.borestop.activity.domain;
import com.ippon.borestop.common.domain.error.Assert;
import java.util.Collection;
public class Activity {
private final Idea idea;
private final Partners partners;
public Activity(Idea idea, Partners partners) {
Assert.notNull("idea", idea);
Assert.notNull("partners", partners);
this.idea = idea;
this.partners = partners;
}
public String getLabel() {
return idea.getLabel();
}
public Collection<Partner> getPartners() {
return partners.getPartners();
}
}
package com.ippon.borestop.activity.domain;
public enum Category {
RELAXATION
}
package com.ippon.borestop.activity.domain;
import com.ippon.borestop.common.domain.error.Assert;
public class Idea {
private final String label;
private final Category category;
public Idea(String label, Category category) {
Assert.notBlank("label", label);
Assert.notNull("category", category);
this.label = label;
this.category = category;
}
public String getLabel() {
return label;
}
public Category getCategory() {
return category;
}
}
package com.ippon.borestop.activity.domain;
public interface IdeasRepository {
Idea next();
}
package com.ippon.borestop.activity.domain;
import com.ippon.borestop.common.domain.error.Assert;
public class Partner {
private final String name;
private final String website;
private Partner(PartnerBuilder builder) {
Assert.notBlank("name", builder.name);
Assert.notBlank("website", builder.website);
name = builder.name;
website = builder.website;
}
public static PartnerBuilder builder() {
return new PartnerBuilder();
}
public String getName() {
return name;
}
public String getWebsite() {
return website;
}
public static class PartnerBuilder {
private String name;
private String website;
public PartnerBuilder name(String name) {
this.name = name;
return this;
}
public PartnerBuilder website(String website) {
this.website = website;
return this;
}
public Partner build() {
return new Partner(this);
}
}
}
package com.ippon.borestop.activity.domain;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
public class Partners {
private final Collection<Partner> partners;
private Partners(PartnersBuilder builder) {
partners = Collections.unmodifiableCollection(builder.partners);
}
public static Partners empty() {
return builder().build();
}
public static PartnersBuilder builder() {
return new PartnersBuilder();
}
public Collection<Partner> getPartners() {
return partners;
}
public static class PartnersBuilder {
private final List<Partner> partners = new ArrayList<>();
public PartnersBuilder add(Partner partner) {
partners.add(partner);
return this;
}
public Partners build() {
return new Partners(this);
}
}
}
package com.ippon.borestop.activity.domain;
public interface PartnersRepository {
Partners find(Category category);
}
package com.ippon.borestop.activity.infrastructure.secondary;
import com.ippon.borestop.activity.domain.Idea;
import com.ippon.borestop.activity.domain.IdeasRepository;
import org.springframework.stereotype.Service;
@Service
class RestIdeasRepository implements IdeasRepository {
@Override
public Idea next() {
// TODO Auto-generated method stub
return null;
}
}
package com.ippon.borestop.activity.domain;
import static com.ippon.borestop.activity.domain.IdeasFixture.idea;
import static com.ippon.borestop.activity.domain.IdeasFixture.label;
import static com.ippon.borestop.activity.domain.PartnersFixture.firstPartner;
import static com.ippon.borestop.activity.domain.PartnersFixture.secondPartner;
import static com.ippon.borestop.activity.domain.PartnersFixture.thirdPartner;
import static com.ippon.borestop.activity.domain.PartnersFixture.threePartners;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.test.context.junit.jupiter.SpringExtension;
@ExtendWith(SpringExtension.class)
class ActivitiesUnitTest {
@Mock
private IdeasRepository ideas;
@Mock
private PartnersRepository partners;
@InjectMocks
private Activities activities;
@Test
void shouldBuildActivity() {
when(ideas.next()).thenReturn(idea());
when(partners.find(Category.RELAXATION)).thenReturn(threePartners());
Activity activity = activities.next();
assertThat(activity.getLabel()).isEqualTo(label());
assertThat(activity.getPartners())
.usingFieldByFieldElementComparator()
.containsExactly(firstPartner(), secondPartner(), thirdPartner());
}
}
package com.ippon.borestop.activity.domain;
import static com.ippon.borestop.activity.domain.IdeasFixture.*;
import static com.ippon.borestop.activity.domain.PartnersFixture.*;
import static org.assertj.core.api.Assertions.*;
import com.ippon.borestop.common.domain.error.MissingMandatoryValueException;
import org.junit.jupiter.api.Test;
class ActivityUnitTest {
@Test
void shouldNotBuildWithoutIdea() {
assertThatThrownBy(() -> new Activity(null, partners()))
.isExactlyInstanceOf(MissingMandatoryValueException.class)
.hasMessageContaining("idea");
}
@Test
void shouldNotBuildWithoutPartners() {
assertThatThrownBy(() -> new Activity(idea(), null))
.isExactlyInstanceOf(MissingMandatoryValueException.class)
.hasMessageContaining("partners");
}
@Test
void shouldBuildActivityWithoutMatchingPartners() {
Activity activity = new Activity(idea(), emptyPartners());
assertThat(activity.getLabel()).isEqualTo(label());
assertThat(activity.getPartners()).isEmpty();
}
@Test
void shouldBuildActivityWithThreePartnersInCategory() {
Activity activity = new Activity(idea(), threePartners());
assertThat(activity.getLabel()).isEqualTo(label());
assertThat(activity.getPartners())
.usingFieldByFieldElementComparator()
.containsExactly(firstPartner(), secondPartner(), thirdPartner());
}
private Partners partners() {
return threePartners();
}
private Partners emptyPartners() {
return Partners.empty();
}
}
package com.ippon.borestop.activity.domain;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import com.ippon.borestop.common.domain.error.MissingMandatoryValueException;
import org.junit.jupiter.api.Test;
class IdeaUnitTest {
@Test
void shouldNotBuildWithoutLabel() {
assertThatThrownBy(() -> new Idea(null, Category.RELAXATION))
.isExactlyInstanceOf(MissingMandatoryValueException.class)
.hasMessageContaining("label");
}
@Test
void shouldNotBuildWithBlankLabel() {
assertThatThrownBy(() -> new Idea(" ", Category.RELAXATION))
.isExactlyInstanceOf(MissingMandatoryValueException.class)
.hasMessageContaining("label");
}
@Test
void shouldNotBuildWithoutCategory() {
assertThatThrownBy(() -> new Idea("label", null))
.isExactlyInstanceOf(MissingMandatoryValueException.class)
.hasMessageContaining("category");
}
}
package com.ippon.borestop.activity.domain;
public final class IdeasFixture {
private IdeasFixture() {}
public static Idea idea() {
return new Idea(label(), Category.RELAXATION);
}
public static String label() {
return "This is my idea";
}
}
package com.ippon.borestop.activity.domain;
import static org.assertj.core.api.Assertions.*;
import com.ippon.borestop.activity.domain.Partner.PartnerBuilder;
import com.ippon.borestop.common.domain.error.MissingMandatoryValueException;
import org.junit.jupiter.api.Test;
class PartnerUnitTest {
@Test
void shouldNotBuildWithoutName() {
assertThatThrownBy(() -> fullBuilder().name(null).build())
.isExactlyInstanceOf(MissingMandatoryValueException.class)
.hasMessageContaining("name");
}
@Test
void shouldNotBuildWithBlankName() {
assertThatThrownBy(() -> fullBuilder().name(" ").build())
.isExactlyInstanceOf(MissingMandatoryValueException.class)
.hasMessageContaining("name");
}
@Test
void shouldNotBuildWithoutWebsite() {
assertThatThrownBy(() -> fullBuilder().website(null).build())
.isExactlyInstanceOf(MissingMandatoryValueException.class)
.hasMessageContaining("website");
}
@Test
void shouldNotBuildWithBlankWebsite() {
assertThatThrownBy(() -> fullBuilder().website(" ").build())
.isExactlyInstanceOf(MissingMandatoryValueException.class)
.hasMessageContaining("website");
}
private PartnerBuilder fullBuilder() {
return Partner.builder().name("name").website("http://name");
}
}
package com.ippon.borestop.activity.domain;
public final class PartnersFixture {
private PartnersFixture() {}
public static Partners threePartners() {
return Partners.builder().add(firstPartner()).add(secondPartner()).add(thirdPartner()).build();
}
public static Partner thirdPartner() {
return Partner.builder().name("third").website("http://third.com").build();
}
public static Partner secondPartner() {
return Partner.builder().name("second").website("http://second.com").build();
}
public static Partner firstPartner() {
return Partner.builder().name("name").website("http://name.com").build();
}
}
package com.ippon.borestop.activity.domain;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import org.junit.jupiter.api.Test;
class PartnersUnitTest {
@Test
void shouldNotBeAbleToUpdatePartners() {
assertThatThrownBy(() -> Partners.empty().getPartners().clear()).isExactlyInstanceOf(UnsupportedOperationException.class);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment