Skip to content
Pedro Henrique Ferreira edited this page Apr 2, 2021 · 12 revisions

The test method

We chose a method from the Hotel class to build the test cases. The Calculate Situation method evaluates the situation of the hotel based on the percentage of the occupied rooms, difference between check-ins and check-outs, and bookings.

Analysis Rooms situation Movement Incoming bookings
Overbooked occupied rooms > 0.8 check-in > check-out booking quantity > 0.4
Almost overbooked occupied rooms > 0.8 check-in > check-out booking quantity <= 0.4
High risk of overbooking occupied rooms > 0.8 check-in = check-out booking quantity > 0.4
Medium risk of overbooking occupied rooms > 0.8 check-in = check-out booking quantity <= 0.4
Slightly risk of overbooking occupied rooms > 0.8 check-in < check-out booking quantity > 0.4
Low risk overbooking occupied rooms > 0.8 check-in < check-out booking quantity <= 0.4
Low risk of loss 0.4 < occupied rooms <= 0.8 check-in > check-out booking quantity > 0.4
Slightly risk of loss 0.4 < occupied rooms <= 0.8 check-in > check-out booking quantity <= 0.4
Medium risk of loss 0.4 < occupied rooms <= 0.8 check-in = check-out booking quantity > 0.4
High risk of loss 0.4 < occupied rooms <= 0.8 check-in = check-out booking quantity <= 0.4
Almost loss 0.4 < occupied rooms <= 0.8 check-in < check-out booking quantity > 0.4
Loss 0.4 < occupied rooms <= 0.8 check-in < check-out booking quantity <= 0.4
Low risk of bankrupt occupied rooms <= 0.4 check-in > check-out booking quantity > 0.4
Slightly risk of bankrupt occupied rooms <= 0.4 check-in > check-out booking quantity <= 0.4
Medium risk of bankrupt occupied rooms <= 0.4 check-in = check-out booking quantity > 0.4
High risk of bankrupt occupied rooms <= 0.4 check-in = check-out booking quantity <= 0.4
Almost bankrupt occupied rooms <= 0.4 check-in < check-out booking quantity > 0.4
Bankrupt occupied rooms <= 0.4 check-in < check-out booking quantity <= 0.4

Defining the test cases

Enumerate conditions and command blocks

From the calculateSituation method, each command block and condition was enumerated:

    public static String calculateSituation(
            Double occupiedRoomsPercentage,
            Double checkInPercentage,
            Double checkOutPercentage,
            Double bookingQuantity){

        if(occupiedRoomsPercentage>0.80){                            //1
            if(checkInPercentage>checkOutPercentage){                //2
                if(bookingQuantity>0.40){                            //3
                    return "Overbooked";                             //4
                }else{
                    return "Almost overbooked";                      //5
                }

            }else if(checkInPercentage.equals(checkOutPercentage)){  //6
                if(bookingQuantity>0.40){                            //7
                    return "High risk of overbooking";               //8
                }else{
                    return "Medium risk of overbooking";             //9
                }
            }else{
                if(bookingQuantity>0.40){                            //10
                    return "Slightly risk of overbooking";           //11
                }else{
                    return "Low risk of overbooking";                //12
                }
            }

        }else if(occupiedRoomsPercentage>0.40){                      //13
            if(checkInPercentage>checkOutPercentage){                //14
                if(bookingQuantity>0.40){                            //15
                    return "Low risk of loss";                       //16
                }else{
                    return "Slightly risk of loss";                  //17
                }

            }else if(checkInPercentage.equals(checkOutPercentage)){  //18
                if(bookingQuantity>0.40){                            //19
                    return "Medium risk of loss";                    //20
                }else{
                    return "High risk of loss";                      //21
                }
            }else{
                if(bookingQuantity>0.40){                            //22
                    return "Almost loss";                            //23
                }else{
                    return "Loss";                                   //24
                }
            }
        }else{
            if(checkInPercentage>checkOutPercentage){                //25
                if(bookingQuantity>0.40){                            //26
                    return "Low risk of bankrupt";                   //27
                }else{
                    return "Slightly risk of bankrupt";              //28
                }

            }else if(checkInPercentage.equals(checkOutPercentage)){  //29
                if(bookingQuantity>0.40){                            //30
                    return "Medium risk of bankrupt";                //31
                }else{
                    return "High risk of bankrupt";                  //32
                }
            }else{
                if(bookingQuantity>0.40){                            //33
                    return "Almost bankrupt";                        //34
                }else{
                    return "Bankrupt";                               //35
                }
            }
        }                                                            //36
    }

Cyclomatic Complexity Graph

From the enumerations, the cyclomatic complexity graph follows:

Determine Test Cases

From the analysis of the Cyclomatic Complexity, the test cases are determined:

Occupied rooms Check in Check out Bookings quantity Result
1 0.81 1.0 0.0 0.41 Overbooked
2 0.81 1.0 0.0 0.40 Almost overbooked
3 0.81 0.0 0.0 0.41 High risk of overbooking
4 0.81 0.0 0.0 0.40 Medium risk of overbooking
5 0.81 0.0 1.0 0.41 Slightly risk of overbooking
6 0.81 0.0 1.0 0.40 Low risk of overbooking
7 0.80 1.0 0.0 0.41 Low risk of loss
8 0.80 1.0 0.0 0.40 Slightly risk of loss
9 0.80 0.0 0.0 0.41 Medium risk of loss
10 0.80 0.0 0.0 0.40 High risk of loss
11 0.80 0.0 1.0 0.41 Almos Loss
12 0.80 0.0 1.0 0.40 Loss
13 0.39 1.0 0.0 0.41 Low risk of bankrupt
14 0.39 1.0 0.0 0.40 Slightly risk of bankrupt
15 0.39 0.0 0.0 0.41 Medium risk of bankrupt
16 0.39 0.0 0.0 0.40 High risk of bankrupt
17 0.39 0.0 1.0 0.41 Almost bankrupt
18 0.39 0.0 1.0 0.40 Bankrupt

Implementing test cases

JUnit Tests

Code

package model;

import org.junit.Test;

import static org.junit.Assert.*;

public class HotelTest {

    @Test
    public void testSituationOverbooked() {
        Double occupiedRooms = 0.81;
        Double checkIn = 1D;
        Double checkOut = 0D;
        Double booking = 0.41;

        String result = Hotel.calculateSituation(occupiedRooms, checkIn, checkOut, booking);
        assertEquals("Overbooked", result);
    }

    @Test
    public void testSituationAlmostOverbooked() {
        Double occupiedRooms = 0.81;
        Double checkIn = 1D;
        Double checkOut = 0D;
        Double booking = 0.40;

        String result = Hotel.calculateSituation(occupiedRooms, checkIn, checkOut, booking);
        assertEquals("Almost overbooked", result);
    }

    @Test
    public void testSituationHighRiskOfOverbooking() {
        Double occupiedRooms = 0.81;
        Double checkIn = 0D;
        Double checkOut = 0D;
        Double booking = 0.41;

        String result = Hotel.calculateSituation(occupiedRooms, checkIn, checkOut, booking);
        assertEquals("High risk of overbooking", result);
    }

    @Test
    public void testSituationMediumRiskOfOverbooking() {
        Double occupiedRooms = 0.81;
        Double checkIn = 0D;
        Double checkOut = 0D;
        Double booking = 0.40;

        String result = Hotel.calculateSituation(occupiedRooms, checkIn, checkOut, booking);
        assertEquals("Medium risk of overbooking", result);
    }

    @Test
    public void testSituationSlightlyRiskOfOverbooking() {
        Double occupiedRooms = 0.81;
        Double checkIn = 0D;
        Double checkOut = 1D;
        Double booking = 0.41;

        String result = Hotel.calculateSituation(occupiedRooms, checkIn, checkOut, booking);
        assertEquals("Slightly risk of overbooking", result);
    }

    @Test
    public void testSituationLowRiskOfOverbooking() {
        Double occupiedRooms = 0.81;
        Double checkIn = 0D;
        Double checkOut = 1D;
        Double booking = 0.40;

        String result = Hotel.calculateSituation(occupiedRooms, checkIn, checkOut, booking);
        assertEquals("Low risk of overbooking", result);
    }

    @Test
    public void testSituationLowRiskOfLoss() {
        Double occupiedRooms = 0.80;
        Double checkIn = 1D;
        Double checkOut = 0D;
        Double booking = 0.41;

        String result = Hotel.calculateSituation(occupiedRooms, checkIn, checkOut, booking);
        assertEquals("Low risk of loss", result);
    }

    @Test
    public void testSituationSlightlyRiskOfLoss() {
        Double occupiedRooms = 0.80;
        Double checkIn = 1D;
        Double checkOut = 0D;
        Double booking = 0.40;

        String result = Hotel.calculateSituation(occupiedRooms, checkIn, checkOut, booking);
        assertEquals("Slightly risk of loss", result);
    }

    @Test
    public void testSituationMediumRiskOfLoss() {
        Double occupiedRooms = 0.80;
        Double checkIn = 0D;
        Double checkOut = 0D;
        Double booking = 0.41;

        String result = Hotel.calculateSituation(occupiedRooms, checkIn, checkOut, booking);
        assertEquals("Medium risk of loss", result);
    }

    @Test
    public void testSituationHighRiskOfLoss() {
        Double occupiedRooms = 0.80;
        Double checkIn = 0D;
        Double checkOut = 0D;
        Double booking = 0.40;

        String result = Hotel.calculateSituation(occupiedRooms, checkIn, checkOut, booking);
        assertEquals("High risk of loss", result);
    }

    @Test
    public void testSituationAlmostLoss() {
        Double occupiedRooms = 0.80;
        Double checkIn = 0D;
        Double checkOut = 1D;
        Double booking = 0.41;

        String result = Hotel.calculateSituation(occupiedRooms, checkIn, checkOut, booking);
        assertEquals("Almost loss", result);
    }

    @Test
    public void testSituationLoss() {
        Double occupiedRooms = 0.80;
        Double checkIn = 0D;
        Double checkOut = 1D;
        Double booking = 0.40;

        String result = Hotel.calculateSituation(occupiedRooms, checkIn, checkOut, booking);
        assertEquals("Loss", result);
    }

    @Test
    public void testSituationLowRiskOfBankrupt() {
        Double occupiedRooms = 0.39;
        Double checkIn = 1D;
        Double checkOut = 0D;
        Double booking = 0.41;

        String result = Hotel.calculateSituation(occupiedRooms, checkIn, checkOut, booking);
        assertEquals("Low risk of bankrupt", result);
    }

    @Test
    public void testSituationSlightlyRiskOfBankrupt() {
        Double occupiedRooms = 0.39;
        Double checkIn = 1D;
        Double checkOut = 0D;
        Double booking = 0.40;

        String result = Hotel.calculateSituation(occupiedRooms, checkIn, checkOut, booking);
        assertEquals("Slightly risk of bankrupt", result);
    }

    @Test
    public void testSituationMediumRiskOfBankrupt() {
        Double occupiedRooms = 0.39;
        Double checkIn = 0D;
        Double checkOut = 0D;
        Double booking = 0.41;

        String result = Hotel.calculateSituation(occupiedRooms, checkIn, checkOut, booking);
        assertEquals("Medium risk of bankrupt", result);
    }

    @Test
    public void testSituationHighRiskOfBankrupt() {
        Double occupiedRooms = 0.39;
        Double checkIn = 0D;
        Double checkOut = 0D;
        Double booking = 0.40;

        String result = Hotel.calculateSituation(occupiedRooms, checkIn, checkOut, booking);
        assertEquals("High risk of bankrupt", result);
    }

    @Test
    public void testSituationAlmostBankrupt() {
        Double occupiedRooms = 0.39;
        Double checkIn = 0D;
        Double checkOut = 1D;
        Double booking = 0.41;

        String result = Hotel.calculateSituation(occupiedRooms, checkIn, checkOut, booking);
        assertEquals("Almost bankrupt", result);
    }

    @Test
    public void testSituationBankrupt() {
        Double occupiedRooms = 0.39;
        Double checkIn = 0D;
        Double checkOut = 1D;
        Double booking = 0.40;

        String result = Hotel.calculateSituation(occupiedRooms, checkIn, checkOut, booking);
        assertEquals("Bankrupt", result);
    }

}

Running

Cypress Tests

Code

describe('Admin', () => {
  beforeEach(() => {
    cy.visit('localhost:8080')
    cy.location('pathname').should('include', '/auth/login');

    cy.get('input[name="login"]').type('admin');
    cy.get('input[name="password"]').type('admin');

    cy.get('input[type="submit"]').contains('Login').click();
    
    cy.location('pathname').should('include', '/dashboard');
  })
  
  it('should succeed on create user', () => {
    cy.visit('localhost:8080/dashboard')

    cy.location('pathname').should('include', '/dashboard');

    cy.get('a').contains('Staff').click();

    cy.contains('Staff').should('be.visible');

    cy.get('button').contains('New Staff').click();

    cy.get('input[name="name"]').type('staff');
    cy.get('select[name="access_level"]').select('STAFF');
    cy.get('input[name="login"]').type('staff');
    cy.get('input[name="password"]').type('staff');

    cy.get('input[type="submit"]').contains('Submit').click();

    cy.visit('localhost:8080/staff');
    cy.location('pathname').should('include', '/staff');
    cy.get('table').contains('td', 'staff').should('be.visible');

  });

  it('should succeed on create room type', () => {
    cy.visit('localhost:8080/dashboard')

    cy.location('pathname').should('include', '/dashboard');

    cy.get('a').contains('Room Types').click();

    cy.contains('Room Types').should('be.visible');

    cy.get('button').contains('New Room Type').click();

    cy.get('input[name="name"]').type('mock room');
    cy.get('textarea').type('A beatiful mock room');
    cy.get('input[name="daily_price"]').type('999.99');

    cy.get('input[type="submit"]').contains('Submit').click();

    cy.visit('localhost:8080/roomTypes');
    cy.location('pathname').should('include', '/roomTypes');
    cy.get('table').contains('td', 'mock room').should('be.visible');

  });

  it('should succeed on create room', () => {
    cy.visit('localhost:8080/dashboard')

    cy.location('pathname').should('include', '/dashboard');

    cy.get('a').contains('Rooms').click();

    cy.contains('Rooms').should('be.visible');

    cy.get('button').contains('New Room').click();

    cy.get('input[name="number"]').type('999');
    cy.get('select[name="id_room_type"] option').eq(1).then(
    element => 
        cy.get('select[name=id_room_type]').select(element.val())
    );

    cy.get('input[type="submit"]').contains('Submit').click();

    cy.visit('localhost:8080/rooms');
    cy.location('pathname').should('include', '/rooms');
    cy.get('table').contains('td', '999').scrollIntoView().should('be.visible');
  });

  it('should succeed on create guest', () => {
    cy.visit('localhost:8080/dashboard')

    cy.location('pathname').should('include', '/dashboard');

    cy.get('a').contains('Guests').click();

    cy.contains('Guests').should('be.visible');

    cy.get('button').contains('New Guest').click();

    cy.get('input[name="name"]').type('John Deere');
    cy.get('input[name="document"]').type('156415616915');
    cy.get('input[name="birth_date"]').type('1956-09-17');
    cy.get('input[name="email"]').type('john@deere.com');
    cy.get('input[name="phone_number"]').type('61956977412');

    cy.get('input[type="submit"]').contains('Submit').click();

    cy.visit('localhost:8080/guests');
    cy.location('pathname').should('include', '/guests');
    cy.get('table').contains('td', 'John Deere').scrollIntoView().should('be.visible');

  });

  it('should succeed on create booking', () => {
    cy.visit('localhost:8080/dashboard')

    cy.location('pathname').should('include', '/dashboard');

    cy.get('a').contains('Bookings').click();

    cy.contains('Bookings').should('be.visible');

    cy.get('button').contains('New Booking').click();

    cy.get('input[name="arrival"]').type('2022-04-01');
    cy.get('input[name="departure"]').type('2022-04-30');
    cy.get('select[name="room_type"] option').eq(1).then(
        element => cy.get('select[name=room_type]').select(element.val())
    );
    cy.get('select[name="id_room"] option').eq(1).then(
        element => cy.get('select[name=id_room]').select(element.val())
    );
    cy.get('select[name="id_guest"] option').eq(1).then(
        element => cy.get('select[name=id_guest]').select(element.val())
    );

    cy.get('input[type="submit"]').contains('Submit').click();

    cy.location('pathname').should('include', '/bookings');
    cy.get('div').contains('button', 'Checkin').should('be.visible');

  });

});

export {};

Running