Skip to content

Commit

Permalink
task-unit-sample
Browse files Browse the repository at this point in the history
  • Loading branch information
yacekmm committed Dec 6, 2024
1 parent 36fe953 commit 32c9181
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class ConcertService {
private final ConcertRepo concertRepo;
private final PricingClient pricingClient;
private final EventPublisher eventPublisher;
private final TagService tagService;
private final CategoryService categoryService;
private final PromoterService promoterService;

Expand All @@ -34,7 +35,7 @@ public Either<ErrorResult, Concert> createConcert(String title, String dateTime,
return Either.left(notFound(not_found, "Promoter contract not found for %s", promoterIdString));
}
return concertFactory.createConcert(title, dateTime, promoterAgreement.promoterId())
.peek(concert -> concert.initNewConcert(categoryService))
.peek(concert -> concert.initNewConcert(tagService, categoryService))
.map(concertRepo::save)
.peek(concert -> eventPublisher.publish(concertCreated(concert, promoterAgreement.profitMarginPercentage())));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public PromoterId promoterId() {
}


public void initNewConcert(CategoryService categoryService) {
public void initNewConcert(TagService tagService, CategoryService categoryService) {
category = categoryService.categorize(title);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public class ConcertFixtures {
public PricingClient pricingClient;

//services
//TODO autowire TagService
@Autowired
public TagService tagService;
@Autowired
public CategoryService categoryService;

Expand Down Expand Up @@ -68,7 +69,7 @@ private static void initMocks(ConcertFixtures concertFixtures) {
}

private static void initServices(ConcertFixtures concertFixtures) {
//TODO init TagService
concertFixtures.tagService = new TagService();
concertFixtures.categoryService = new CategoryService();
}

Expand All @@ -78,7 +79,7 @@ private static void initSut(ConcertFixtures concertFixtures, SharedFixtures shar
concertFixtures.concertRepo,
concertFixtures.pricingClient,
sharedFixtures.fakeEventPublisher(),
//TODO inject TagService
concertFixtures.tagService,
concertFixtures.categoryService,
concertFixtures.promoterService
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,36 @@
package com.bottega.promoter.concert.domain


import com.bottega.promoter.fixtures.SpecificationBase
import com.bottega.sharedlib.config.TestClockConfig

import static com.bottega.sharedlib.config.TestClockConfig.TEST_TIME_PLUS_30_DAYS

class Concert_initConcert_unitSpec extends SpecificationBase {

def "initConcert - adds tags"() {
//TODO tests
given:
def newConcert = new Concert(new ConcertId(), Title.from(title).get(), ConcertDate.from(TEST_TIME_PLUS_30_DAYS.toString(), sharedFixtures.clock).get(), "vendor-id", new HashSet<>(), null)

when:
newConcert.initNewConcert(concertFixtures.tagService, concertFixtures.categoryService)

then:
with(newConcert.getTags().stream().map(Tag::getValue).toList()) {
size() == expectedTags.size()
containsAll(expectedTags)
}

where:
title | expectedTags
"no tags apply" | []
//TODO more cases
}

def "initConcert - assigns category"() {
given:
def newConcert = new Concert(new ConcertId(), Title.from(title).get(), ConcertDate.from(TEST_TIME_PLUS_30_DAYS.toString(), sharedFixtures.clock).get(), "vendor-id", new HashSet<>(), null)

when:
newConcert.initNewConcert(concertFixtures.categoryService)
newConcert.initNewConcert(concertFixtures.tagService, concertFixtures.categoryService)

then:
newConcert.category.value == expectedCategory
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,40 @@
package com.bottega.promoter.concert.domain;

import java.util.HashSet;
import java.util.*;
import java.util.stream.Stream;

import com.bottega.promoter.concert.fixtures.ConcertLogicTestBase;
import com.bottega.sharedlib.config.TestClockConfig;
import org.junit.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.*;
import static com.bottega.sharedlib.config.TestClockConfig.TEST_TIME_PLUS_30_DAYS;
import static java.util.stream.Collectors.toSet;
import static org.assertj.core.api.Assertions.assertThat;

class Concert_initConcert_unitTest extends ConcertLogicTestBase {

@Test
void initConcert_addsTags() {
//TODO tests
private static Stream<Arguments> provideStringsForTags() {
return Stream.of(
Arguments.of("no tags apply", Set.of())
//TODO more cases
);
}

@ParameterizedTest
@MethodSource("provideStringsForTags")
void initConcert_addsTags(String title, Set<String> expectedTags) {
//given
Concert newConcert = new Concert(new ConcertId(), Title.from(title).get(), ConcertDate.from(TEST_TIME_PLUS_30_DAYS.toString(), sharedFixtures.clock).get(), "vendor-id", new HashSet<>(), null);

//when
newConcert.initNewConcert(concertFixtures.tagService, concertFixtures.categoryService);

//then

assertThat(newConcert.getTags().stream().map(Tag::getValue).collect(toSet())).containsExactlyInAnyOrderElementsOf(expectedTags);
}


private static Stream<Arguments> provideStringsForCategories() {
return Stream.of(
Arguments.of("no category", "other"),
Expand All @@ -40,7 +52,7 @@ void initConcert_assignsCategory(String title, String expectedCategory) {
Concert newConcert = new Concert(new ConcertId(), Title.from(title).get(), ConcertDate.from(TEST_TIME_PLUS_30_DAYS.toString(), sharedFixtures.clock).get(), "vendor-id", new HashSet<>(), null);

//when
newConcert.initNewConcert(concertFixtures.categoryService);
newConcert.initNewConcert(concertFixtures.tagService, concertFixtures.categoryService);

//then
assertThat(newConcert.getCategory().getValue()).isEqualTo(expectedCategory);
Expand Down

0 comments on commit 32c9181

Please sign in to comment.