Implementing the Fibonacci service

Most of you might be familiar with Fibonacci's numbers. Here's a brief explanation anyway for those who don't know what they are.

Fibonacci's sequence is an integer sequence resulting from the recurrence f(n) = f(n-1) - f(n - 2). The sequence starts with being f(0) = 0 and f(1) = 1. All other numbers are generated applying the recurrence as many times as needed until a value substitution can be performed using either 0 or 1 known values.

That is: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,...

More info about Fibonacci's sequence can be found here: http://www.wolframalpha.com/input/?i=fibonacci+sequence

As an extra functionality, we want to limit how long the value computation takes, so we impose a constraint on the input; our service will only compute Fibonacci's numbers from 0 to 30 (both numbers included).

This is a possible implementation of a class computing Fibonacci's numbers:

@Service("fibonacci") 
public class FibonacciService { 
    public static final int LIMIT = 30; 
 
    public int getNthNumber(int n) { 
        if (isOutOfLimits(n) { 
        throw new IllegalArgumentException( 
        "Requested number must be a positive " + 
           number no bigger than " + LIMIT); 
        if (n == 0) return 0; 
        if (n == 1 || n == 2) return 1; 
        int first, second = 1, result = 1; 
        do { 
            first = second; 
            second = result; 
            result = first + second; 
            --n; 
        } while (n > 2); 
        return result; 
    } 
 
    private boolean isOutOfLimits(int number) { 
        return number > LIMIT || number < 0; 
    } 
} 

For the sake of brevity, the TDD Red-Green-Refactor process is not explicitly explained in the demonstration, but has been present through development. Only the final implementation with the final tests is presented:

public class FibonacciServiceTest { 
    private FibonacciService tested; 
    private final String expectedExceptionMessage = 
"Requested number " + "must be a positive number no bigger than " + FibonacciService.LIMIT; @Rule public ExpectedException exception = ExpectedException.none(); @Before public void beforeTest() { tested = new FibonacciService(); } @Test public void test0() { int actual = tested.getNthNumber(0); assertEquals(0, actual); } @Test public void test1() { int actual = tested.getNthNumber(1); assertEquals(1, actual); } @Test public void test7() { int actual = tested.getNthNumber(7); assertEquals(13, actual); } @Test public void testNegative() { exception.expect(IllegalArgumentException.class); exception.expectMessage(is(expectedExceptionMessage)); tested.getNthNumber(-1); } @Test public void testOutOfBounce() { exception.expect(IllegalArgumentException.class); exception.expectMessage(is(expectedExceptionMessage)); tested.getNthNumber(31); } }

Also, we can now turn on the fibonacci feature in the application.yml file, perform some queries with the browser, and check how is it going:

features: 
    fibonacci: 
        restEnabled: true 

Execute Gradle's run command:

    $>gradle run

Now we can fully test our REST API using the browser, with a number between 0 and 30:

Then, we test it with a number bigger than 30, and lastly by introducing characters instead of numbers:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.165.62