diff --git a/pom.xml b/pom.xml index b19fc7c..b64f5ee 100644 --- a/pom.xml +++ b/pom.xml @@ -185,6 +185,28 @@ + + org.jacoco + jacoco-maven-plugin + 0.8.5 + + + + prepare-agent + + + + + report + test + + report + + + + + + com.google.cloud.tools jib-maven-plugin diff --git a/src/main/java/com/example/slabiak/appointmentscheduler/model/AdjusterAfterEnd.java b/src/main/java/com/example/slabiak/appointmentscheduler/model/AdjusterAfterEnd.java new file mode 100644 index 0000000..98746ba --- /dev/null +++ b/src/main/java/com/example/slabiak/appointmentscheduler/model/AdjusterAfterEnd.java @@ -0,0 +1,13 @@ +package com.example.slabiak.appointmentscheduler.model; + +class AdjusterAfterEnd implements TimePeriodAdjuster { + + + @Override + public TimePeroid adjust(TimePeroid original, TimePeroid breakPeriod) { + if (breakPeriod.getEnd().isAfter(original.getEnd())) { + original.setEnd(breakPeriod.getEnd()); + } + return original; + } +} \ No newline at end of file diff --git a/src/main/java/com/example/slabiak/appointmentscheduler/model/AdjusterBeforeStart.java b/src/main/java/com/example/slabiak/appointmentscheduler/model/AdjusterBeforeStart.java new file mode 100644 index 0000000..af73bd4 --- /dev/null +++ b/src/main/java/com/example/slabiak/appointmentscheduler/model/AdjusterBeforeStart.java @@ -0,0 +1,18 @@ +package com.example.slabiak.appointmentscheduler.model; + + +import java.time.LocalDateTime; + +class AdjusterBeforeStart implements TimePeriodAdjuster { + + + @Override + public TimePeroid adjust(TimePeroid original, TimePeroid breakPeriod) { + if (breakPeriod.getStart().isBefore(original.getStart())) { + original.setStart((breakPeriod.getStart())); + } + return original; + } +} + + diff --git a/src/main/java/com/example/slabiak/appointmentscheduler/model/DayPlan.java b/src/main/java/com/example/slabiak/appointmentscheduler/model/DayPlan.java index 0be2dda..3ce8d73 100644 --- a/src/main/java/com/example/slabiak/appointmentscheduler/model/DayPlan.java +++ b/src/main/java/com/example/slabiak/appointmentscheduler/model/DayPlan.java @@ -1,6 +1,7 @@ package com.example.slabiak.appointmentscheduler.model; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -13,7 +14,8 @@ public DayPlan() { breaks = new ArrayList(); } - public List timePeroidsWithBreaksExcluded() { + +/* public List timePeroidsWithBreaksExcluded() { ArrayList timePeroidsWithBreaksExcluded = new ArrayList<>(); timePeroidsWithBreaksExcluded.add(getWorkingHours()); List breaks = getBreaks(); @@ -46,8 +48,57 @@ public List timePeroidsWithBreaksExcluded() { return timePeroidsWithBreaksExcluded; + }*/ + +/* + public List timePeroidsWithBreaksExcluded() { + ArrayList timePeroidsWithBreaksExcluded = new ArrayList<>(); + timePeroidsWithBreaksExcluded.add(getWorkingHours()); + List breaks = getBreaks(); + + if (!breaks.isEmpty()) { + processBreaks(timePeroidsWithBreaksExcluded, breaks); + } + + return timePeroidsWithBreaksExcluded; + } + + private void processBreaks(List timePeroidsWithBreaksExcluded, List breaks) { + ArrayList toAdd = new ArrayList<>(); + for (TimePeroid break1 : breaks) { + adjustBreakStartAndEnd(break1); + updateTimePeroids(timePeroidsWithBreaksExcluded, break1, toAdd); + } + timePeroidsWithBreaksExcluded.addAll(toAdd); + Collections.sort(timePeroidsWithBreaksExcluded); + } + + private void adjustBreakStartAndEnd(TimePeroid break1) { + if (break1.getStart().isBefore(workingHours.getStart())) { + break1.setStart(workingHours.getStart()); + } + if (break1.getEnd().isAfter(workingHours.getEnd())) { + break1.setEnd(workingHours.getEnd()); + } } + private void updateTimePeroids(List timePeroidsWithBreaksExcluded, TimePeroid break1, List toAdd) { + for (TimePeroid peroid : timePeroidsWithBreaksExcluded) { + if (break1.getStart().equals(peroid.getStart()) && break1.getEnd().isAfter(peroid.getStart()) && break1.getEnd().isBefore(peroid.getEnd())) { + peroid.setStart(break1.getEnd()); + } + if (break1.getStart().isAfter(peroid.getStart()) && break1.getStart().isBefore(peroid.getEnd()) && break1.getEnd().equals(peroid.getEnd())) { + peroid.setEnd(break1.getStart()); + } + if (break1.getStart().isAfter(peroid.getStart()) && break1.getEnd().isBefore(peroid.getEnd())) { + toAdd.add(new TimePeroid(peroid.getStart(), break1.getStart())); + peroid.setStart(break1.getEnd()); + } + } + } +*/ + + public TimePeroid getWorkingHours() { return workingHours; } @@ -72,4 +123,29 @@ public void addBreak(TimePeroid breakToAdd) { breaks.add(breakToAdd); } + public List timePeroidsWithBreaksExcluded() { + List timePeriodsWithBreaksExcluded = new ArrayList<>(); + timePeriodsWithBreaksExcluded.add(getWorkingHours()); + List breaks = getBreaks(); + + if (!breaks.isEmpty()) { + List adjusters = Arrays.asList( + new AdjusterBeforeStart(), + new AdjusterAfterEnd() + // Add more adjusters as needed + ); + + for (TimePeroid breakPeriod : breaks) { + for (TimePeroid timePeriod : timePeriodsWithBreaksExcluded) { + for (TimePeriodAdjuster adjuster : adjusters) { + adjuster.adjust(timePeriod, breakPeriod); + } + } + } + + Collections.sort(timePeriodsWithBreaksExcluded); + } + + return timePeriodsWithBreaksExcluded; + } } diff --git a/src/main/java/com/example/slabiak/appointmentscheduler/model/TimePeriodAdjuster.java b/src/main/java/com/example/slabiak/appointmentscheduler/model/TimePeriodAdjuster.java new file mode 100644 index 0000000..120e059 --- /dev/null +++ b/src/main/java/com/example/slabiak/appointmentscheduler/model/TimePeriodAdjuster.java @@ -0,0 +1,5 @@ +package com.example.slabiak.appointmentscheduler.model; + +public interface TimePeriodAdjuster { + TimePeroid adjust(TimePeroid original, TimePeroid breakPeriod); +} diff --git a/src/main/java/com/example/slabiak/appointmentscheduler/service/AppointmentService.java b/src/main/java/com/example/slabiak/appointmentscheduler/service/AppointmentService.java index c90defa..1e51c72 100644 --- a/src/main/java/com/example/slabiak/appointmentscheduler/service/AppointmentService.java +++ b/src/main/java/com/example/slabiak/appointmentscheduler/service/AppointmentService.java @@ -34,7 +34,7 @@ public interface AppointmentService { List getAppointmentsByProviderAtDay(int providerId, LocalDate day); - List getAppointmentsByCustomerAtDay(int providerId, LocalDate day); + List getAppointmentsByCustomerAtDayAndTimePeriods(int providerId, LocalDate day); List getConfirmedAppointmentsByCustomerId(int customerId); diff --git a/src/main/java/com/example/slabiak/appointmentscheduler/service/RetailCustomerService.java b/src/main/java/com/example/slabiak/appointmentscheduler/service/RetailCustomerService.java new file mode 100644 index 0000000..02fedd1 --- /dev/null +++ b/src/main/java/com/example/slabiak/appointmentscheduler/service/RetailCustomerService.java @@ -0,0 +1,23 @@ +package com.example.slabiak.appointmentscheduler.service; + +import com.example.slabiak.appointmentscheduler.entity.user.Role; +import com.example.slabiak.appointmentscheduler.entity.user.customer.RetailCustomer; +import com.example.slabiak.appointmentscheduler.model.UserForm; +import org.springframework.security.access.prepost.PreAuthorize; + +import java.util.Collection; +import java.util.List; + +public interface RetailCustomerService { + public void saveNewRetailCustomer(UserForm userForm); + + public void updateRetailCustomerProfile(UserForm updateData); + + public List getAllRetailCustomers(); + + public RetailCustomer getRetailCustomerById(int retailCustomerId); + + public Collection getRolesForRetailCustomer(); + + +} diff --git a/src/main/java/com/example/slabiak/appointmentscheduler/service/UserService.java b/src/main/java/com/example/slabiak/appointmentscheduler/service/UserService.java index 471966c..e97f455 100644 --- a/src/main/java/com/example/slabiak/appointmentscheduler/service/UserService.java +++ b/src/main/java/com/example/slabiak/appointmentscheduler/service/UserService.java @@ -49,6 +49,8 @@ public interface UserService { void updateProviderProfile(UserForm updateData); + Collection getRoleForCorporateCustomers(); + Collection getRolesForProvider(); /* @@ -67,7 +69,6 @@ public interface UserService { void updateRetailCustomerProfile(UserForm updateData); - Collection getRolesForRetailCustomer(); /* * CorporateCustomer @@ -80,7 +81,6 @@ public interface UserService { void updateCorporateCustomerProfile(UserForm updateData); - Collection getRoleForCorporateCustomers(); } diff --git a/src/main/java/com/example/slabiak/appointmentscheduler/service/impl/AppointmentServiceImpl.java b/src/main/java/com/example/slabiak/appointmentscheduler/service/impl/AppointmentServiceImpl.java index 98826d4..d54fcf7 100644 --- a/src/main/java/com/example/slabiak/appointmentscheduler/service/impl/AppointmentServiceImpl.java +++ b/src/main/java/com/example/slabiak/appointmentscheduler/service/impl/AppointmentServiceImpl.java @@ -88,7 +88,13 @@ public List getAppointmentsByProviderAtDay(int providerId, LocalDat return appointmentRepository.findByProviderIdWithStartInPeroid(providerId, day.atStartOfDay(), day.atStartOfDay().plusDays(1)); } - @Override + + public List getAppointmentsByCustomerAtDayAndTimePeriods(int providerId, LocalDate day) { + return appointmentRepository.findByCustomerIdWithStartInPeroid(providerId, day.atStartOfDay(), day.atStartOfDay().plusDays(1)); + } + + + public List getAppointmentsByCustomerAtDay(int providerId, LocalDate day) { return appointmentRepository.findByCustomerIdWithStartInPeroid(providerId, day.atStartOfDay(), day.atStartOfDay().plusDays(1)); } @@ -127,6 +133,8 @@ public void createNewAppointment(int workId, int providerId, int customerId, Loc } } + // Before + @Override public void addMessageToAppointmentChat(int appointmentId, int authorId, ChatMessage chatMessage) { diff --git a/src/main/java/com/example/slabiak/appointmentscheduler/service/impl/EmailServiceImpl.java b/src/main/java/com/example/slabiak/appointmentscheduler/service/impl/EmailServiceImpl.java index 7569092..ebfd6e9 100644 --- a/src/main/java/com/example/slabiak/appointmentscheduler/service/impl/EmailServiceImpl.java +++ b/src/main/java/com/example/slabiak/appointmentscheduler/service/impl/EmailServiceImpl.java @@ -39,7 +39,8 @@ public EmailServiceImpl(JavaMailSender javaMailSender, SpringTemplateEngine temp this.baseUrl = baseUrl; } - @Async + //Before Extract Method + /*@Async @Override public void sendEmail(String to, String subject, String templateName, Context templateContext, File attachment) { try { @@ -65,8 +66,42 @@ public void sendEmail(String to, String subject, String templateName, Context te log.error("Error while adding attachment to email, error is {}", e.getLocalizedMessage()); } + }*/ + + //After Extract Method + @Async + @Override + public void sendEmail(String to, String subject, String templateName, Context templateContext, File attachment) { + try { + MimeMessage message = createMimeMessageWithAttachment(to, subject, templateName, templateContext, attachment); + javaMailSender.send(message); + } catch (MessagingException e) { + log.error("Error while adding attachment to email, error is {}", e.getLocalizedMessage()); + } } + private MimeMessage createMimeMessageWithAttachment(String to, String subject, String templateName, Context templateContext, File attachment) throws MessagingException { + MimeMessage message = javaMailSender.createMimeMessage(); + MimeMessageHelper helper = new MimeMessageHelper(message, + MimeMessageHelper.MULTIPART_MODE_MIXED_RELATED, + StandardCharsets.UTF_8.name()); + + String html = templateEngine.process("email/" + templateName, templateContext); + + helper.setTo(to); + helper.setFrom("appointmentscheduler@gmail.com"); + helper.setSubject(subject); + helper.setText(html, true); + + if (attachment != null) { + helper.addAttachment("invoice", attachment); + } + + return message; + } + + + @Async @Override public void sendAppointmentFinishedNotification(Appointment appointment) { diff --git a/src/main/java/com/example/slabiak/appointmentscheduler/service/impl/RetailCustomerService.java b/src/main/java/com/example/slabiak/appointmentscheduler/service/impl/RetailCustomerService.java new file mode 100644 index 0000000..7b64440 --- /dev/null +++ b/src/main/java/com/example/slabiak/appointmentscheduler/service/impl/RetailCustomerService.java @@ -0,0 +1,69 @@ +// RetailCustomerService.java + +package com.example.slabiak.appointmentscheduler.service.impl; + +import com.example.slabiak.appointmentscheduler.dao.RoleRepository; +import com.example.slabiak.appointmentscheduler.dao.user.customer.RetailCustomerRepository; +import com.example.slabiak.appointmentscheduler.entity.user.Role; // Add this import +import com.example.slabiak.appointmentscheduler.entity.user.customer.RetailCustomer; +import com.example.slabiak.appointmentscheduler.model.UserForm; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.security.core.userdetails.UsernameNotFoundException; +import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.stereotype.Service; + +import java.util.Collection; +import java.util.HashSet; +import java.util.List; + +@Service +public class RetailCustomerService implements com.example.slabiak.appointmentscheduler.service.RetailCustomerService { + + private final RetailCustomerRepository retailCustomerRepository; + private final PasswordEncoder passwordEncoder; + + private RoleRepository roleRepository ; + + public RetailCustomerService(RetailCustomerRepository retailCustomerRepository,PasswordEncoder passwordEncoder,RoleRepository roleRepository) { + this.retailCustomerRepository = retailCustomerRepository; + this.passwordEncoder = passwordEncoder; + this.roleRepository = roleRepository; + + } + + @Override + @PreAuthorize("#updateData.id == principal.id or hasRole('ADMIN')") + public void updateRetailCustomerProfile(UserForm updateData) { + RetailCustomer retailCustomer = retailCustomerRepository.getOne(updateData.getId()); + retailCustomer.update(updateData); + retailCustomerRepository.save(retailCustomer); + } + + @Override + public void saveNewRetailCustomer(UserForm userForm) { + RetailCustomer retailCustomer = new RetailCustomer(userForm, passwordEncoder.encode(userForm.getPassword()), getRolesForRetailCustomer()); + retailCustomerRepository.save(retailCustomer); + } + + @Override + public List getAllRetailCustomers() { + return retailCustomerRepository.findAll(); + } + + + @Override + @PreAuthorize("#retailCustomerId == principal.id or hasRole('ADMIN')") + public RetailCustomer getRetailCustomerById(int retailCustomerId) { + return retailCustomerRepository.findById(retailCustomerId) + .orElseThrow(() -> new UsernameNotFoundException("User not found!")); + } + + + public Collection getRolesForRetailCustomer() { + HashSet roles = new HashSet(); + roles.add(roleRepository.findByName("ROLE_CUSTOMER_RETAIL")); + roles.add(roleRepository.findByName("ROLE_CUSTOMER")); + return roles; + } + +} diff --git a/src/main/java/com/example/slabiak/appointmentscheduler/service/impl/UserServiceImpl.java b/src/main/java/com/example/slabiak/appointmentscheduler/service/impl/UserServiceImpl.java index f7e40fb..424a812 100644 --- a/src/main/java/com/example/slabiak/appointmentscheduler/service/impl/UserServiceImpl.java +++ b/src/main/java/com/example/slabiak/appointmentscheduler/service/impl/UserServiceImpl.java @@ -16,6 +16,7 @@ import com.example.slabiak.appointmentscheduler.entity.user.provider.Provider; import com.example.slabiak.appointmentscheduler.model.ChangePasswordForm; import com.example.slabiak.appointmentscheduler.model.UserForm; +import com.example.slabiak.appointmentscheduler.service.RetailCustomerService; import com.example.slabiak.appointmentscheduler.service.UserService; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.core.userdetails.UsernameNotFoundException; @@ -36,8 +37,9 @@ public class UserServiceImpl implements UserService { private final UserRepository userRepository; private final RoleRepository roleRepository; private final PasswordEncoder passwordEncoder; + private final RetailCustomerService retailCustomerService; - public UserServiceImpl(ProviderRepository providerRepository, CustomerRepository customerRepository, CorporateCustomerRepository corporateCustomerRepository, RetailCustomerRepository retailCustomerRepository, UserRepository userRepository, RoleRepository roleRepository, PasswordEncoder passwordEncoder) { + public UserServiceImpl(ProviderRepository providerRepository, CustomerRepository customerRepository, CorporateCustomerRepository corporateCustomerRepository, RetailCustomerRepository retailCustomerRepository, UserRepository userRepository, RoleRepository roleRepository, PasswordEncoder passwordEncoder, RetailCustomerService retailCustomerService) { this.providerRepository = providerRepository; this.customerRepository = customerRepository; this.corporateCustomerRepository = corporateCustomerRepository; @@ -45,6 +47,7 @@ public UserServiceImpl(ProviderRepository providerRepository, CustomerRepository this.userRepository = userRepository; this.roleRepository = roleRepository; this.passwordEncoder = passwordEncoder; + this.retailCustomerService = retailCustomerService; } @Override @@ -74,8 +77,7 @@ public Provider getProviderById(int providerId) { @Override @PreAuthorize("#retailCustomerId == principal.id or hasRole('ADMIN')") public RetailCustomer getRetailCustomerById(int retailCustomerId) { - return retailCustomerRepository.findById(retailCustomerId) - .orElseThrow(() -> new UsernameNotFoundException("User not found!")); + return retailCustomerService.getRetailCustomerById(retailCustomerId); } @@ -99,7 +101,7 @@ public List getAllCustomers() { @Override public List getAllRetailCustomers() { - return retailCustomerRepository.findAll(); + return retailCustomerService.getAllRetailCustomers(); } @@ -159,12 +161,9 @@ public void updateProviderProfile(UserForm updateData) { @Override @PreAuthorize("#updateData.id == principal.id or hasRole('ADMIN')") public void updateRetailCustomerProfile(UserForm updateData) { - RetailCustomer retailCustomer = retailCustomerRepository.getOne(updateData.getId()); - retailCustomer.update(updateData); - retailCustomerRepository.save(retailCustomer); + retailCustomerService.updateRetailCustomerProfile(updateData); } - @Override @PreAuthorize("#updateData.id == principal.id or hasRole('ADMIN')") public void updateCorporateCustomerProfile(UserForm updateData) { @@ -176,8 +175,7 @@ public void updateCorporateCustomerProfile(UserForm updateData) { @Override public void saveNewRetailCustomer(UserForm userForm) { - RetailCustomer retailCustomer = new RetailCustomer(userForm, passwordEncoder.encode(userForm.getPassword()), getRolesForRetailCustomer()); - retailCustomerRepository.save(retailCustomer); + retailCustomerService.saveNewRetailCustomer(userForm); } @Override @@ -193,13 +191,13 @@ public void saveNewProvider(UserForm userForm) { providerRepository.save(provider); } - @Override - public Collection getRolesForRetailCustomer() { - HashSet roles = new HashSet(); - roles.add(roleRepository.findByName("ROLE_CUSTOMER_RETAIL")); - roles.add(roleRepository.findByName("ROLE_CUSTOMER")); - return roles; - } + //for Move method +// public Collection getRolesForRetailCustomer() { +// HashSet roles = new HashSet(); +// roles.add(roleRepository.findByName("ROLE_CUSTOMER_RETAIL")); +// roles.add(roleRepository.findByName("ROLE_CUSTOMER")); +// return roles; +// } @Override diff --git a/src/test/java/com/example/slabiak/appointmentscheduler/controller/ExchangeControllerTest.java b/src/test/java/com/example/slabiak/appointmentscheduler/controller/ExchangeControllerTest.java new file mode 100644 index 0000000..ca5bca6 --- /dev/null +++ b/src/test/java/com/example/slabiak/appointmentscheduler/controller/ExchangeControllerTest.java @@ -0,0 +1,136 @@ +package com.example.slabiak.appointmentscheduler.controller; + +import com.example.slabiak.appointmentscheduler.controller.ExchangeController; +import com.example.slabiak.appointmentscheduler.entity.Appointment; +import com.example.slabiak.appointmentscheduler.security.CustomUserDetails; +import com.example.slabiak.appointmentscheduler.service.AppointmentService; +import com.example.slabiak.appointmentscheduler.service.ExchangeService; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestParam; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.*; + +public class ExchangeControllerTest { + + private ExchangeController exchangeController; + + @Mock + private ExchangeService exchangeService; + + @Mock + private AppointmentService appointmentService; + + @Mock + private CustomUserDetails currentUser; + + @Mock + private Model model; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + exchangeController = new ExchangeController(exchangeService, appointmentService); + } + + @Test + public void testShowEligibleAppointmentsToExchange() { + // Arrange + int oldAppointmentId = 1; + List eligibleAppointments = new ArrayList<>(); + when(exchangeService.getEligibleAppointmentsForExchange(oldAppointmentId)).thenReturn(eligibleAppointments); + + // Act + String viewName = exchangeController.showEligibleAppointmentsToExchange(oldAppointmentId, model); + + // Assert + assertEquals("exchange/listProposals", viewName); + verify(model).addAttribute("appointmentId", oldAppointmentId); + verify(model).addAttribute("numberOfEligibleAppointments", eligibleAppointments.size()); + verify(model).addAttribute("eligibleAppointments", eligibleAppointments); + } + + @Test + public void testShowExchangeSummaryScreen_ExchangePossible() { + // Arrange + int oldAppointmentId = 1; + int newAppointmentId = 2; + when(exchangeService.checkIfExchangeIsPossible(oldAppointmentId, newAppointmentId, currentUser.getId())).thenReturn(true); + + // Act + String viewName = exchangeController.showExchangeSummaryScreen(oldAppointmentId, newAppointmentId, model, currentUser); + + // Assert + verify(model).addAttribute("oldAppointment", appointmentService.getAppointmentByIdWithAuthorization(oldAppointmentId)); + verify(model).addAttribute("newAppointment", appointmentService.getAppointmentById(newAppointmentId)); + assertEquals("exchange/exchangeSummary", viewName); + } + + @Test + public void testShowExchangeSummaryScreen_ExchangeNotPossible() { + // Arrange + int oldAppointmentId = 1; + int newAppointmentId = 2; + when(exchangeService.checkIfExchangeIsPossible(oldAppointmentId, newAppointmentId, currentUser.getId())).thenReturn(false); + + // Act + String viewName = exchangeController.showExchangeSummaryScreen(oldAppointmentId, newAppointmentId, model, currentUser); + + // Assert + verify(model, never()).addAttribute("oldAppointment", appointmentService.getAppointmentByIdWithAuthorization(oldAppointmentId)); + verify(model, never()).addAttribute("newAppointment", appointmentService.getAppointmentById(newAppointmentId)); + assertEquals("redirect:/appointments/all", viewName); + } + + + + @Test + public void testProcessExchangeRequest_Failure() { + // Arrange + int oldAppointmentId = 1; + int newAppointmentId = 2; + when(exchangeService.requestExchange(oldAppointmentId, newAppointmentId, currentUser.getId())).thenReturn(false); + + // Act + String viewName = exchangeController.processExchangeRequest(oldAppointmentId, newAppointmentId, model, currentUser); + + // Assert + verify(model).addAttribute("message", "Error! Exchange not sent!"); + assertEquals("exchange/requestConfirmation", viewName); + } + + @Test + public void testProcessExchangeAcceptation() { + // Arrange + int exchangeId = 1; + + // Act + String viewName = exchangeController.processExchangeAcceptation(exchangeId, model, currentUser); + + // Assert + verify(exchangeService).acceptExchange(exchangeId, currentUser.getId()); + assertEquals("redirect:/appointments/all", viewName); + } + + @Test + public void testProcessExchangeRejection() { + // Arrange + int exchangeId = 1; + + // Act + String viewName = exchangeController.processExchangeRejection(exchangeId, model, currentUser); + + // Assert + verify(exchangeService).rejectExchange(exchangeId, currentUser.getId()); + assertEquals("redirect:/appointments/all", viewName); + } +} + diff --git a/src/test/java/com/example/slabiak/appointmentscheduler/controller/WorkControllerTest.java b/src/test/java/com/example/slabiak/appointmentscheduler/controller/WorkControllerTest.java new file mode 100644 index 0000000..6b7cfcc --- /dev/null +++ b/src/test/java/com/example/slabiak/appointmentscheduler/controller/WorkControllerTest.java @@ -0,0 +1,102 @@ +package com.example.slabiak.appointmentscheduler.controller; + +import com.example.slabiak.appointmentscheduler.entity.Work; +import com.example.slabiak.appointmentscheduler.service.WorkService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.*; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.info.BuildProperties; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.ui.Model; +import org.springframework.ui.ModelMap; +import org.springframework.web.servlet.ModelAndView; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.*; + +@ExtendWith(MockitoExtension.class) +class WorkControllerTest { + @InjectMocks + private WorkController workController; + + @Mock + private WorkService workService; + + @MockBean + private BuildProperties buildProperties; + + + + @BeforeEach + public void setUp() { + MockitoAnnotations.initMocks(this); + } + + @Test + void testShowAllWorks() { + // Create a list of works to be returned by the service + List works = new ArrayList<>(); + works.add(new Work()); + + // Create the WorkService mock + WorkService workService = Mockito.mock(WorkService.class); + + // Stub the service to return the list of works + Mockito.when(workService.getAllWorks()).thenReturn(works); + + // Create the model + Model model = mock(Model.class); + + Mockito.lenient().when(model.addAttribute(Mockito.eq("works"), Mockito.anyList())).thenReturn(model); + + // Create the controller + WorkController workController = new WorkController(workService); + + // Perform the test + String viewName = workController.showAllWorks(model); + + // Assert the view name and model attributes as needed + + assertEquals("works/list", viewName); + } + + + @Test + public void testShowFormForUpdateWork() { + // Arrange + Model model = mock(Model.class); + int workId = 1; + Work work1 = new Work(); + work1.setId(1);// Create a Work object + + // Act + String viewName = workController.showFormForUpdateWork(workId, model); + + // Assert + // Verify that the expected Work object is added to the model + assertEquals("works/createOrUpdateWorkForm", viewName); // Replace with the correct view name + } + + @Test + public void testShowFormForAddWork() { + // Arrange + Model model = mock(Model.class); + + // Act + String viewName = workController.showFormForAddWork(model); + + // Assert + ArgumentCaptor workCaptor = ArgumentCaptor.forClass(Work.class); + verify(model).addAttribute(eq("work"), workCaptor.capture()); + assertEquals("works/createOrUpdateWorkForm", viewName); // Replace with the correct view name + } + +}