src/test/java/edu/kea/kab/integrationtests/SaveInputTestAndRetrieveInputTest.java
@Test
    void canSaveInputToDatabaseAndRetrieveInput() throws Exception {

        // count is equal to 0 because it's a mock database and therefore the database is empty on startup.
        assertThat(consumptionRepository.count()).isEqualTo(0);

        // Run the controller method "/input"
        // .param() is the object parameter
        mvc.perform(post("/input").with(csrf().asHeader())
                .contentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE)
                .param("videoHours", "2")
                .param("musicHours", "3")
                .param("mobileHours", "5")
                .accept(MediaType.APPLICATION_JSON))
                .andDo(print())
                .andExpect(status().isFound());

        // count is equal to 1 because it's a mock database and therefore the database is empty on startup.
        assertThat(consumptionRepository.count()).isEqualTo(1);

        Iterable<Consumption> consumptions = consumptionRepository.findAll();

        // iterate through the Consumption table
        assertThat(consumptions.iterator().hasNext()).isTrue();

        // save the next object.
        Consumption savedConsumptions = consumptions.iterator().next();
        assertThat(savedConsumptions).isNotNull();

        // compare the object to the original saved object
        assertThat(savedConsumptions.getVideoHours()).isEqualTo(2.0);
        assertThat(savedConsumptions.getMusicHours()).isEqualTo(3.0);
        assertThat(savedConsumptions.getMobileHours()).isEqualTo(5.0);

        Calendar calendar = new GregorianCalendar();
        calendar.setTime(new Date());
        int week = calendar.get(Calendar.WEEK_OF_YEAR);
        assertThat(savedConsumptions.getWeek()).isEqualTo(week);
        int year = calendar.get(Calendar.YEAR);
        assertThat(savedConsumptions.getYear()).isEqualTo(year);

    }
// Test if a newly created Consumption object is saved to the consumption table
Test if a newly created Consumption object is saved to the consumption table
Test if a newly created Consumption object is saved to the consumption table
@Test
    void canSaveInputToDatabaseAndRetrieveInput() throws Exception {
        assertThat(consumptionRepository.count()).isEqualTo(0);
        mvc.perform(post("/input").with(csrf().asHeader())
                .contentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE)
                .param("videoHours", "2")
                .param("musicHours", "3")
                .param("mobileHours", "5")
                .accept(MediaType.APPLICATION_JSON))
                .andDo(print())
                .andExpect(status().isFound());
        assertThat(consumptionRepository.count()).isEqualTo(1);
        Iterable<Consumption> consumptions = consumptionRepository.findAll();
        assertThat(consumptions.iterator().hasNext()).isTrue();
        Consumption savedConsumptions = consumptions.iterator().next();
        assertThat(savedConsumptions).isNotNull();
        assertThat(savedConsumptions.getVideoHours()).isEqualTo(2.0);
        assertThat(savedConsumptions.getMusicHours()).isEqualTo(3.0);
        assertThat(savedConsumptions.getMobileHours()).isEqualTo(5.0);
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(new Date());
        int week = calendar.get(Calendar.WEEK_OF_YEAR);
        assertThat(savedConsumptions.getWeek()).isEqualTo(week);
        int year = calendar.get(Calendar.YEAR);
        assertThat(savedConsumptions.getYear()).isEqualTo(year);
    }
@ Test void canSaveInputToDatabaseAndRetrieveInput ( ) throws Exception { assertThat ( consumptionRepository . count ( ) ) . isEqualTo ( 0 ) ; mvc . perform ( post ( "/input" ) . with ( csrf ( ) . asHeader ( ) ) . contentType ( MediaType . APPLICATION_FORM_URLENCODED_VALUE ) . param ( "videoHours" , "2" ) . param ( "musicHours" , "3" ) . param ( "mobileHours" , "5" ) . accept ( MediaType . APPLICATION_JSON ) ) . andDo ( print ( ) ) . andExpect ( status ( ) . isFound ( ) ) ; assertThat ( consumptionRepository . count ( ) ) . isEqualTo ( 1 ) ; Iterable < Consumption > consumptions = consumptionRepository . findAll ( ) ; assertThat ( consumptions . iterator ( ) . hasNext ( ) ) . isTrue ( ) ; Consumption savedConsumptions = consumptions . iterator ( ) . next ( ) ; assertThat ( savedConsumptions ) . isNotNull ( ) ; assertThat ( savedConsumptions . getVideoHours ( ) ) . isEqualTo ( 2.0 ) ; assertThat ( savedConsumptions . getMusicHours ( ) ) . isEqualTo ( 3.0 ) ; assertThat ( savedConsumptions . getMobileHours ( ) ) . isEqualTo ( 5.0 ) ; Calendar calendar = new GregorianCalendar ( ) ; calendar . setTime ( new Date ( ) ) ; int week = calendar . get ( Calendar . WEEK_OF_YEAR ) ; assertThat ( savedConsumptions . getWeek ( ) ) . isEqualTo ( week ) ; int year = calendar . get ( Calendar . YEAR ) ; assertThat ( savedConsumptions . getYear ( ) ) . isEqualTo ( year ) ; }
Test if a newly created Consumption object is saved to the consumption table
Test if a newly created Consumption object is saved to the consumption table
// count is equal to 0 because it's a mock database and therefore the database is empty on startup. // Run the controller method "/input" // .param() is the object parameter // count is equal to 1 because it's a mock database and therefore the database is empty on startup. // iterate through the Consumption table // save the next object. // compare the object to the original saved object