private NAThermProgram getExitingTestProgram(NAThermStateBody thermState) { if (thermState.getThermProgramBackup() != null) { for (NAThermProgram thermProgram : thermState.getThermProgramBackup()) { if (thermProgram.getName().equals(TEST_PROGRAM_NAME)) { return thermProgram; } } } return null; }
@Test public void testSchedule() { assumeNotNull(relayId); assumeNotNull(thermostatId); NAThermStateBody thermState = api.getthermstate(relayId, thermostatId).getBody(); if (thermState.getThermProgram().getName().equals(TEST_PROGRAM_NAME) || (thermState.getThermProgramOrder() != null && thermState.getThermProgramOrder().getName().equals(TEST_PROGRAM_NAME))) { throw new RuntimeException( "The test program should not be the one in use at the beginning of the test !"); } // Get program NAThermProgram program = thermState.getThermProgramOrder(); if (program == null) { program = thermState.getThermProgram(); } String previousProgramId = program.getProgramId(); // Do we need to create a test program ? // Note : if you want to test the program creation, you need to delete the test program manually // on Netatmo'site since it cannot be done by API. NAThermProgram testProgram = getExitingTestProgram(thermState); if (testProgram == null) { testProgram = program; testProgram.setProgramId(null); testProgram.setName(TEST_PROGRAM_NAME); NANewScheduleResponse newscheduleResponse = api.createnewschedule(relayId, thermostatId, testProgram); assertEquals(newscheduleResponse.getStatus(), "ok"); testProgram.setProgramId(newscheduleResponse.getBody().getScheduleId()); } // Switch to test schedule NAOkResponse naOkResponse = api.switchschedule(relayId, thermostatId, testProgram.getProgramId()); assertEquals(naOkResponse.getStatus(), "ok"); // Modify schedule Float nightTempBefore = null; for (NAZone zone : testProgram.getZones()) { if (zone.getType() == 1) { nightTempBefore = zone.getTemp(); zone.setTemp(nightTempBefore + Float.valueOf(0.5f)); break; } } if (nightTempBefore == null) { throw new RuntimeException( "The current program doesn't have a night zone. This is unexpected by this test !"); } naOkResponse = api.syncschedule(relayId, thermostatId, testProgram); assertEquals(naOkResponse.getStatus(), "ok"); // Verify the change of temperature thermState = api.getthermstate(relayId, thermostatId).getBody(); testProgram = thermState.getThermProgramOrder(); if (testProgram == null) { testProgram = thermState.getThermProgram(); } for (NAZone zone : testProgram.getZones()) { if (zone.getType() == 1) { assertEquals(nightTempBefore + Float.valueOf(0.5f), zone.getTemp(), 0.0001); zone.setTemp(nightTempBefore); break; } } // Rollback changes api.syncschedule(relayId, thermostatId, testProgram); api.switchschedule(relayId, thermostatId, previousProgramId); }