Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejava
themeMidnight
titlesrc/main/java/com/example/demo/LinkService.java
linenumberstrue
collapsetrue
package com.example.demo;

import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;

import reactor.core.publisher.Mono;

@Service
public class LinkService {

    private final LinkRepository linkRepository;

    public LinkService(LinkRepository linkRepository) {
        this.linkRepository = linkRepository;
    }

    Mono<String> shortenLink(String link) {
        String randomKey = RandomStringUtils.randomAlphabetic(6);
        return linkRepository.save(new Link(link, randomKey))
                             .map(result -> result.getKey());
    }

    Mono<Link> getOriginalLink(String key) {
        return linkRepository.findByKey(key);
    }
}
Code Block
titleLinkControllerTest
linenumberstrue
collapsetrue
package com.example.demo;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;

import reactor.core.publisher.Mono;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;

@RunWith(SpringRunner.class)
@WebFluxTest(controllers = LinkController.class)
public class LinkControllerTest {

    @Autowired
    private WebTestClient webTestClient;

    @MockBean
    private LinkService linkService;

    @Test
    public void shortensLink() {
        when(linkService.shortenLink("https://spring.io")).thenReturn(Mono.just("aass2211"));
        webTestClient.post()
                     .uri("/link")
                     .contentType(MediaType.APPLICATION_JSON)
                     .syncBody("{\"link\":\"https://spring.io\"}")
                     .exchange()
                     .expectStatus()
                     .is2xxSuccessful()
                     .expectBody()
                     .jsonPath("$.shortenedLink")
                     .value(val -> assertThat(val).isEqualTo("aass2211"));
    }

    @Test
    public void redirectsToOriginalLink() {
        when(linkService.getOriginalLink("aaa21123"))
                .thenReturn(Mono.just(new Link("http://sprint.io", "aaa21123")));
        webTestClient.get()
                     .uri("/link/aaa21123")
                     .exchange()
                     .expectStatus()
                     .isPermanentRedirect()
                     .expectHeader()
                     .value("Location", location -> assertThat(location).isEqualTo("http://sprint.io"));
    }
}

Code Block
titleLinkServiceTest
linenumberstrue
collapsetrue
package com.example.demo;

import org.junit.Before;
import org.junit.Test;
import org.mockito.stubbing.Answer;

import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class LinkServiceTest {

    private LinkRepository linkRepository = mock(LinkRepository.class);
    private LinkService linkService = new LinkService(linkRepository);

    @Before
    public void setup() {
        when(linkRepository.save(any()))
                .thenAnswer((Answer<Mono<Link>>) invocationOnMock -> Mono.just((Link) invocationOnMock.getArguments()[0]));
    }

    @Test
    public void shortensLink() {
        StepVerifier.create(linkService.shortenLink("http://spring.io"))
                    .expectNextMatches(result -> result != null && result.length() > 0)
                    .expectComplete()
                    .verify();
    }

}