diff --git a/.env b/.env new file mode 100644 index 0000000..79fd424 --- /dev/null +++ b/.env @@ -0,0 +1,10 @@ +IP_HOST=10.43.100.19 +PORT_HOST=4444 +PORT_HOST_QA=24444 +USERNAME_DATABASE=root +PASSWORD_DATABASE=mysql_deployment +PASSWORD_DATABASE_QA=mysql_qa +URL_EUREKA=10.43.101.226:8761 +URL_EUREKA_QA=10.43.101.226:28761 +URL_DATABASE=10.43.100.12:13306 +URL_DATABASE_QA=10.43.100.12:23306 diff --git a/.env_template b/.env_template new file mode 100644 index 0000000..1a47883 --- /dev/null +++ b/.env_template @@ -0,0 +1,8 @@ +IP_HOST= +PORT_HOST= +PORT_HOST_QA= +USERNAME_DATABASE= +PASSWORD_DATABASE= +PASSWORD_DATABASE_QA= +URL_EUREKA= +URL_EUREKA_QA= diff --git a/compose.yml b/compose.yml index 02ea138..735a2c2 100644 --- a/compose.yml +++ b/compose.yml @@ -14,8 +14,8 @@ services: IP_HOST: ${IP_HOST} PORT_HOST: ${PORT_HOST} URL_DATABASE: ${URL_DATABASE} + restart: always - event-microservice-qa: build: . container_name: event-microservice-qa @@ -29,3 +29,5 @@ services: IP_HOST: ${IP_HOST} PORT_HOST: ${PORT_HOST_QA} URL_DATABASE: ${URL_DATABASE_QA} + restart: always + diff --git a/pom.xml b/pom.xml index fcce81c..f871f26 100644 --- a/pom.xml +++ b/pom.xml @@ -61,7 +61,31 @@ micrometer-registry-prometheus runtime + + org.springframework.boot + spring-boot-starter-security + + + com.auth0 + java-jwt + 3.18.2 + + + + + org.junit.platform + junit-platform-suite-engine + 1.10.0 + test + + + + org.junit.platform + junit-platform-suite-api + 1.10.0 + test + diff --git a/src/main/java/com/ekomodatech/festivanow/event/controller/EventController.java b/src/main/java/com/ekomodatech/festivanow/event/controller/EventController.java index 7a01901..8b5877c 100644 --- a/src/main/java/com/ekomodatech/festivanow/event/controller/EventController.java +++ b/src/main/java/com/ekomodatech/festivanow/event/controller/EventController.java @@ -18,6 +18,15 @@ import com.ekomodatech.festivanow.event.repository.EventRepository; import com.ekomodatech.festivanow.event.repository.LogisticRepository; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.userdetails.UserDetails; +import com.auth0.jwt.JWT; +import com.auth0.jwt.interfaces.Claim; +import com.auth0.jwt.interfaces.DecodedJWT; +import org.springframework.web.bind.annotation.RequestHeader; + + @RestController @RequestMapping("/event") public class EventController { @@ -33,34 +42,76 @@ public class EventController { private LogisticRepository logisticRepository; @GetMapping("/{id}") - @CrossOrigin(origins = "http://localhost:4200") - public ResponseEntity findEvent(@PathVariable Long id) { - try { - Event event = eventRepository.findById(id) - .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Event not found")); - return ResponseEntity.ok(event); - } catch (ResponseStatusException ex) { - throw ex; - } catch (Exception ex) { - throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Internal Server Error", ex); +public ResponseEntity findEvent(@PathVariable Long id, @RequestHeader(value = "Authorization", required = false) String authorizationHeader) { + try { + Event event = eventRepository.findById(id) + .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Event not found")); + + if (authorizationHeader != null) { + + String createdBy = getCurrentUsername(authorizationHeader); + + + if (!event.getVisibility() || createdBy.equals(event.getCreatedBy())) { + return ResponseEntity.ok(event); + } else { + + throw new ResponseStatusException(HttpStatus.FORBIDDEN, "Access denied"); + } + } else { + + if (!event.getVisibility()) { + return ResponseEntity.ok(event); + } else { + + throw new ResponseStatusException(HttpStatus.FORBIDDEN, "Access denied"); + } } + } catch (ResponseStatusException ex) { + throw ex; + } catch (Exception ex) { + throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Internal Server Error", ex); } +} + + + @GetMapping("/list") - @CrossOrigin(origins = "http://localhost:4200") - public ResponseEntity> listEvents() { + public ResponseEntity> listAllEvents(@RequestHeader(value = "Authorization", required = false) String authorizationHeader) { try { - List events = eventRepository.findAll(); + List events; + String createdBy = getCurrentUsername(authorizationHeader); + + if (createdBy == null || "Usuario Desconocido".equals(createdBy)) { + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Event not found"); + } else { + events = eventRepository.findByCreatedBy(createdBy); + } + return ResponseEntity.ok(events); } catch (Exception ex) { throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Internal Server Error", ex); } } + @GetMapping("/list/public") + public ResponseEntity> listPublicEvents() { + try { + List publicEvents = eventRepository.findByVisibilityFalse(); + return ResponseEntity.ok(publicEvents); + } catch (Exception ex) { + throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Internal Server Error", ex); + } + } + + + @PostMapping("/create") - @CrossOrigin(origins = "http://localhost:4200") - public ResponseEntity createEvent(@RequestBody Event newEvent) { + public ResponseEntity createEvent(@RequestBody Event newEvent, @RequestHeader("Authorization") String authorizationHeader) { try { + String createdBy = getCurrentUsername(authorizationHeader); + newEvent.setCreatedBy(createdBy); Event createdEvent = eventRepository.save(newEvent); return ResponseEntity.status(HttpStatus.CREATED).body(createdEvent); } catch (Exception ex) { @@ -68,8 +119,32 @@ public ResponseEntity createEvent(@RequestBody Event newEvent) { } } + public String getCurrentUsername(String authorizationHeader) { + + String jwtToken = authorizationHeader.replace("Bearer ", ""); + + + String username = getUsernameFromJWT(jwtToken); + + return username; + } + + private String getUsernameFromJWT(String jwtToken) { + try { + DecodedJWT decodedJWT = JWT.decode(jwtToken); + Claim preferredUsernameClaim = decodedJWT.getClaim("preferred_username"); + + if (!preferredUsernameClaim.isNull()) { + return preferredUsernameClaim.asString(); + } + } catch (Exception e) { + + } + + return "Usuario Desconocido"; + } + @PutMapping("/update/{id}") - @CrossOrigin(origins = "http://localhost:4200") public ResponseEntity updateEvent(@PathVariable Long id, @RequestBody Event updatedEvent) { try { Event event = eventRepository.findById(id) @@ -83,6 +158,8 @@ public ResponseEntity updateEvent(@PathVariable Long id, @RequestBody Ev event.setType(updatedEvent.getType()); event.setUrl(updatedEvent.getUrl()); event.setState(updatedEvent.getState()); + event.setDirection(updatedEvent.getDirection()); + event.setVisibility(updatedEvent.getVisibility()); // Guarda el evento actualizado en el repositorio eventRepository.save(event); @@ -97,7 +174,6 @@ public ResponseEntity updateEvent(@PathVariable Long id, @RequestBody Ev @DeleteMapping("/delete/{id}") - @CrossOrigin(origins = "http://localhost:4200") public ResponseEntity deleteEvent(@PathVariable Long id) { try { Event event = eventRepository.findById(id) @@ -110,7 +186,6 @@ public ResponseEntity deleteEvent(@PathVariable Long id) { } @GetMapping("/city/{id}") - @CrossOrigin(origins = "http://localhost:4200") public ResponseEntity findCity(@PathVariable Long id) { try { City city = cityRepository.findById(id) @@ -122,7 +197,6 @@ public ResponseEntity findCity(@PathVariable Long id) { } @GetMapping("/listCity") - @CrossOrigin(origins = "http://localhost:4200") public ResponseEntity> listCities() { try { List cities = cityRepository.findAll(); @@ -133,7 +207,6 @@ public ResponseEntity> listCities() { } @GetMapping("/logistic/{id}") - @CrossOrigin(origins = "http://localhost:4200") public ResponseEntity findLogistic(@PathVariable Long id) { try { Logistic logistic = logisticRepository.findById(id) @@ -145,7 +218,6 @@ public ResponseEntity findLogistic(@PathVariable Long id) { } @GetMapping("/listLogistic") - @CrossOrigin(origins = "http://localhost:4200") public ResponseEntity> listLogistics() { try { List logistics = logisticRepository.findAll(); @@ -159,4 +231,6 @@ public ResponseEntity> listLogistics() { String index() { return "index"; } + + } diff --git a/src/main/java/com/ekomodatech/festivanow/event/entity/Event.java b/src/main/java/com/ekomodatech/festivanow/event/entity/Event.java index b773184..146729b 100644 --- a/src/main/java/com/ekomodatech/festivanow/event/entity/Event.java +++ b/src/main/java/com/ekomodatech/festivanow/event/entity/Event.java @@ -1,40 +1,39 @@ package com.ekomodatech.festivanow.event.entity; -import java.sql.Date; - -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; -import jakarta.persistence.JoinColumn; -import jakarta.persistence.ManyToOne; -import jakarta.persistence.Table; +import jakarta.persistence.*; +import java.util.Date; @Entity @Table(name = "Event") public class Event { @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) + @GeneratedValue(strategy = GenerationType.IDENTITY) private long idEvent; private String name; - private Date date; + private String date; private long ability; private String description; private String type; private String url; private String state; + private String direction; + private boolean visibility; - @ManyToOne() + @ManyToOne() @JoinColumn(name = "id_City") - City city; - @ManyToOne() + private City city; + + @ManyToOne() @JoinColumn(name = "id_Logistic") - Logistic logistic; - + private Logistic logistic; + + + private String createdBy; public Event() { } - public Event(String name, Date date, long ability, String description, String type, String url, String state ) { + + public Event(String name, String date, long ability, String description, String type, String url, String state, String direction, boolean visibility, String createdBy) { this.name = name; this.date = date; this.ability = ability; @@ -42,65 +41,113 @@ public Event(String name, Date date, long ability, String description, String ty this.type = type; this.url = url; this.state = state; + this.direction = direction; + this.visibility = visibility; + this.createdBy = createdBy; } + public long getIdEvent() { return idEvent; } + public void setIdEvent(long idEvent) { this.idEvent = idEvent; } + public String getName() { return name; } + public void setName(String name) { this.name = name; } - public Date getDate() { + + public String getDate() { return date; } - public void setDate(Date date) { + + public void setDate(String date) { this.date = date; } + public long getAbility() { return ability; } + public void setAbility(long ability) { this.ability = ability; } + public String getDescription() { return description; } + public void setDescription(String description) { this.description = description; } + public String getType() { return type; } + public void setType(String type) { this.type = type; } + public City getCity() { return city; } + public void setCity(City city) { this.city = city; } + public Logistic getLogistic() { return logistic; } + public void setLogistic(Logistic logistic) { this.logistic = logistic; } - public String getUrl() { + + public String getUrl() { return url; } + public void setUrl(String url) { this.url = url; } + public String getState() { return state; } + public void setState(String state) { this.state = state; } + + public String getDirection() { + return direction; + } + + public void setDirection(String direction) { + this.direction = direction; + } + + public boolean getVisibility() { + return visibility; + } + + public void setVisibility(boolean visibility) { + this.visibility = visibility; + } + + public String getCreatedBy() { + return createdBy; + } + + public void setCreatedBy(String createdBy) { + this.createdBy = createdBy; + } } + diff --git a/src/main/java/com/ekomodatech/festivanow/event/repository/EventRepository.java b/src/main/java/com/ekomodatech/festivanow/event/repository/EventRepository.java index f906a25..c671c77 100644 --- a/src/main/java/com/ekomodatech/festivanow/event/repository/EventRepository.java +++ b/src/main/java/com/ekomodatech/festivanow/event/repository/EventRepository.java @@ -4,7 +4,12 @@ import org.springframework.stereotype.Repository; import com.ekomodatech.festivanow.event.entity.Event; + +import java.util.List; + @Repository public interface EventRepository extends JpaRepository { - + List findByVisibilityFalse(); + List findByCreatedByOrVisibilityFalse(String createdBy); + List findByCreatedBy(String createdBy); } diff --git a/src/test/java/com/ekomodatech/festivanow/event/EventMicroserviceApplicationTests.java b/src/test/java/com/ekomodatech/festivanow/event/EventMicroserviceApplicationTests.java index f59d401..a201940 100644 --- a/src/test/java/com/ekomodatech/festivanow/event/EventMicroserviceApplicationTests.java +++ b/src/test/java/com/ekomodatech/festivanow/event/EventMicroserviceApplicationTests.java @@ -1,13 +1,16 @@ package com.ekomodatech.festivanow.event; -import org.junit.jupiter.api.Test; +import org.junit.platform.suite.api.SelectPackages; +import org.junit.platform.suite.api.Suite; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest +@Suite +@SelectPackages("com.ekomodatech.festivanow.event") class EventMicroserviceApplicationTests { - @Test - void contextLoads() { - } +// @Test +// void contextLoads() { +// } } diff --git a/src/test/java/com/ekomodatech/festivanow/event/controller/AuthorityControllerTest.java b/src/test/java/com/ekomodatech/festivanow/event/controller/AuthorityControllerTest.java new file mode 100644 index 0000000..47a5d5d --- /dev/null +++ b/src/test/java/com/ekomodatech/festivanow/event/controller/AuthorityControllerTest.java @@ -0,0 +1,119 @@ +package com.ekomodatech.festivanow.event.controller; + +import com.ekomodatech.festivanow.event.entity.Authority; +import com.ekomodatech.festivanow.event.repository.AuthorityRepository; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.ui.ExtendedModelMap; +import org.springframework.ui.Model; + +import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class AuthorityControllerTest { + + @Mock + private AuthorityRepository authorityRepository; + + @InjectMocks + private AuthorityController authorityController; + + @Test + void testFindAuthority() { + // Create a sample authority + Authority authority = new Authority(); + long id = authority.getIdAuthority(); + authority.setName("Sample Authority"); + + // Mock the behavior of the authorityRepository + when(authorityRepository.findById(id)).thenReturn(Optional.of(authority)); + + // Call the method under test + ResponseEntity result = authorityController.findAuthority(id); + + // Assert that the result is not null and has the expected authority + assertNotNull(result); + assertEquals(authority, result.getBody()); + } + + @Test + void testListAuthorities() { + // Create sample authorities + Authority authority1 = new Authority(); + Authority authority2 = new Authority(); + List authorities = List.of(authority1, authority2); + + // Mock the behavior of the authorityRepository + when(authorityRepository.findAll()).thenReturn(authorities); + + // Call the method under test + ResponseEntity> result = authorityController.listAuthorities(); + + // Assert that the result is not null and contains the expected authorities + assertNotNull(result); + assertEquals(authorities, result.getBody()); + } + + @Test + void testCreateAuthority() { + // Create a sample authority + Authority authority = new Authority(); + authority.setName("Sample Authority"); + + // Mock the behavior of the authorityRepository + when(authorityRepository.save(authority)).thenReturn(authority); + + // Call the method under test + ResponseEntity result = authorityController.createAuthority(authority); + + // Assert that the result is not null and has the expected authority + assertNotNull(result); + assertEquals(authority, result.getBody()); + } + + @Test + void testDeleteAuthority() { + // Mock the behavior of the authorityRepository +// when(authorityRepository.deleteById(1L)).thenReturn(1L); + + // Call the method under test + ResponseEntity result = authorityController.deleteAuthority(1L); + + // Assert that the result is not null and has the expected status + assertNotNull(result); + assertEquals(HttpStatus.NO_CONTENT, result.getStatusCode()); + } + + @Test + void testUpdate() { + // Create a sample authority + Authority authority = new Authority(); +// long id = authority.getIdAuthority(); + authority.setName("Sample Authority"); + + // Mock the behavior of the authorityRepository + when(authorityRepository.findById(1L)).thenReturn(Optional.of(authority)); + + // Create a sample model + Model model = new ExtendedModelMap(); + + // Call the method under test + ResponseEntity result = authorityController.update(1L, model); + + // Assert that the result is not null and has the expected message + assertNotNull(result); + assertEquals("authority/update", result.getBody()); + } + + // You can add similar test methods for other controller endpoints + +} diff --git a/src/test/java/com/ekomodatech/festivanow/event/controller/CityControllerTest.java b/src/test/java/com/ekomodatech/festivanow/event/controller/CityControllerTest.java new file mode 100644 index 0000000..ffc787a --- /dev/null +++ b/src/test/java/com/ekomodatech/festivanow/event/controller/CityControllerTest.java @@ -0,0 +1,124 @@ +package com.ekomodatech.festivanow.event.controller; + +import com.ekomodatech.festivanow.event.entity.City; +import com.ekomodatech.festivanow.event.repository.CityRepository; +import com.ekomodatech.festivanow.event.repository.DepartmentRepository; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class CityControllerTest { + + @Mock + private CityRepository cityRepository; + + @Mock + private DepartmentRepository departmentRepository; + + @InjectMocks + private CityController cityController; + + @Test + void testFindCity() { + // Create a sample city + City city = new City(); + long id = city.getIdCity(); + city.setName("Sample City"); + + // Mock the behavior of the cityRepository + when(cityRepository.findById(id)).thenReturn(Optional.of(city)); + + // Call the method under test + ResponseEntity result = cityController.findCity(id); + + // Assert that the result is not null and has the expected city + assertNotNull(result); + assertEquals(HttpStatus.OK, result.getStatusCode()); + assertEquals(city, result.getBody()); + } + + @Test + void testListCities() { + // Create sample cities + City city1 = new City(); + City city2 = new City(); + List cities = List.of(city1, city2); + + // Mock the behavior of the cityRepository + when(cityRepository.findAll()).thenReturn(cities); + + // Call the method under test + ResponseEntity> result = cityController.listCities(); + + // Assert that the result is not null and contains the expected cities + assertNotNull(result); + assertEquals(HttpStatus.OK, result.getStatusCode()); + assertEquals(cities, result.getBody()); + } + + @Test + void testCreateCity() { + // Create a sample city + City city = new City(); + city.setName("Sample City"); + + // Mock the behavior of the cityRepository + when(cityRepository.save(any())).thenReturn(city); + + // Call the method under test + ResponseEntity result = cityController.createCity(city); + + // Assert that the result is not null and has the expected city + assertNotNull(result); + assertEquals(HttpStatus.CREATED, result.getStatusCode()); + assertEquals(city, result.getBody()); + } + + @Test + void testDeleteCity() { + City city = new City(); + long id = city.getIdCity(); + // Mock the behavior of the cityRepository +// when(cityRepository.findById(id)).thenReturn(Optional.of(city)); + + // Call the method under test + ResponseEntity result = cityController.deleteCity(id); + + // Assert that the result is not null and has the expected status + assertEquals(HttpStatus.NO_CONTENT, result.getStatusCode()); + } + + @Test + void testUpdateCity() { + // Create a sample city + City city = new City(); + long id = city.getIdCity(); + city.setName("Sample City"); + + // Mock the behavior of the cityRepository + when(cityRepository.findById(id)).thenReturn(Optional.of(city)); + when(cityRepository.save(city)).thenReturn(city); + + // Call the method under test + ResponseEntity result = cityController.updateCity(id, city); + + // Assert that the result is not null and has the expected city + assertNotNull(result); + assertEquals(HttpStatus.OK, result.getStatusCode()); + assertEquals(city, result.getBody()); + } + + // You can add similar test methods for other controller endpoints + +} diff --git a/src/test/java/com/ekomodatech/festivanow/event/controller/CountryControllerTest.java b/src/test/java/com/ekomodatech/festivanow/event/controller/CountryControllerTest.java new file mode 100644 index 0000000..bd27d3d --- /dev/null +++ b/src/test/java/com/ekomodatech/festivanow/event/controller/CountryControllerTest.java @@ -0,0 +1,120 @@ +package com.ekomodatech.festivanow.event.controller; + +import com.ekomodatech.festivanow.event.entity.Country; +import com.ekomodatech.festivanow.event.repository.CountryRepository; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + + +import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class CountryControllerTest { + + @Mock + private CountryRepository countryRepository; + + @InjectMocks + private CountryController countryController; + + @Test + void testFindCountry() { + // Create a sample country + Country country = new Country(); + long id = country.getIdCountry(); + country.setName("Sample Country"); + + // Mock the behavior of the countryRepository + when(countryRepository.findById(id)).thenReturn(Optional.of(country)); + + // Call the method under test + ResponseEntity result = countryController.findCountry(id); + + // Assert that the result is not null and has the expected country + assertNotNull(result); + assertEquals(HttpStatus.OK, result.getStatusCode()); + assertEquals(country, result.getBody()); + } + + @Test + void testListCountries() { + // Create sample countries + Country country1 = new Country(); + Country country2 = new Country(); + List countries = List.of(country1, country2); + + // Mock the behavior of the countryRepository + when(countryRepository.findAll()).thenReturn(countries); + + // Call the method under test + ResponseEntity> result = countryController.listCountries(); + + // Assert that the result is not null and contains the expected countries + assertNotNull(result); + assertEquals(HttpStatus.OK, result.getStatusCode()); + assertEquals(countries, result.getBody()); + } + + @Test + void testCreateCountry() { + // Create a sample country + Country country = new Country(); + country.setName("Sample Country"); + + // Mock the behavior of the countryRepository + when(countryRepository.save(any())).thenReturn(country); + + // Call the method under test + ResponseEntity result = countryController.createCountry(country); + + // Assert that the result is not null and has the expected country + assertNotNull(result); + assertEquals(HttpStatus.CREATED, result.getStatusCode()); + assertEquals(country, result.getBody()); + } + + @Test + void testDeleteCountry() { + // Mock the behavior of the countryRepository + + // Call the method under test + ResponseEntity result = countryController.deleteCountry(1L); + + // Assert that the result is not null and has the expected status + assertNotNull(result); + assertEquals(HttpStatus.NO_CONTENT, result.getStatusCode()); + } + + @Test + void testUpdateCountry() { + // Create a sample country + Country country = new Country(); + long id = country.getIdCountry(); + country.setName("Sample Country"); + + // Mock the behavior of the countryRepository + when(countryRepository.findById(id)).thenReturn(Optional.of(country)); + when(countryRepository.save(country)).thenReturn(country); + + // Call the method under test + ResponseEntity result = countryController.update(id, country); + + // Assert that the result is not null and has the expected message + assertNotNull(result); + assertEquals(HttpStatus.OK, result.getStatusCode()); + assertEquals("Country updated successfully", result.getBody()); + } + + // You can add similar test methods for other controller endpoints + +} diff --git a/src/test/java/com/ekomodatech/festivanow/event/controller/DepartmentControllerTest.java b/src/test/java/com/ekomodatech/festivanow/event/controller/DepartmentControllerTest.java new file mode 100644 index 0000000..042746b --- /dev/null +++ b/src/test/java/com/ekomodatech/festivanow/event/controller/DepartmentControllerTest.java @@ -0,0 +1,128 @@ +package com.ekomodatech.festivanow.event.controller; + +import com.ekomodatech.festivanow.event.entity.Department; +import com.ekomodatech.festivanow.event.repository.CountryRepository; +import com.ekomodatech.festivanow.event.repository.DepartmentRepository; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.ui.ExtendedModelMap; +import org.springframework.ui.Model; + +import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class DepartmentControllerTest { + + @Mock + private DepartmentRepository departmentRepository; + +// @Mock +// private CountryRepository countryRepository; + + @InjectMocks + private DepartmentController departmentController; + + @Test + void testFindDepartment() { + // Create a sample department + Department department = new Department(); + Long id = department.getIdDepartment(); + department.setName("Sample Department"); + + // Mock the behavior of the departmentRepository + when(departmentRepository.findById(id)).thenReturn(Optional.of(department)); + + // Call the method under test + ResponseEntity result = departmentController.findDepartment(id); + + // Assert that the result is not null and has the expected department + assertNotNull(result); + assertEquals(HttpStatus.OK, result.getStatusCode()); + assertEquals(department, result.getBody()); + } + + @Test + void testListDepartments() { + // Create sample departments + Department department1 = new Department(); + Department department2 = new Department(); + List departments = List.of(department1, department2); + + // Mock the behavior of the departmentRepository + when(departmentRepository.findAll()).thenReturn(departments); + + // Call the method under test + ResponseEntity> result = departmentController.listDepartments(); + + // Assert that the result is not null and contains the expected departments + assertNotNull(result); + assertEquals(HttpStatus.OK, result.getStatusCode()); + assertEquals(departments, result.getBody()); + } + + @Test + void testCreateDepartment() { + // Create a sample department + Department department = new Department(); + department.setName("Sample Department"); + + // Mock the behavior of the departmentRepository + when(departmentRepository.save(any())).thenReturn(department); + + // Call the method under test + ResponseEntity result = departmentController.createDepartment(department); + + // Assert that the result is not null and has the expected department + assertNotNull(result); + assertEquals(HttpStatus.CREATED, result.getStatusCode()); + assertEquals(department, result.getBody()); + } + + @Test + void testDeleteDepartment() { + // Mock the behavior of the departmentRepository + + // Call the method under test + ResponseEntity result = departmentController.deleteDepartment(1L); + + // Assert that the result is not null and has the expected status + assertNotNull(result); + assertEquals(HttpStatus.NO_CONTENT, result.getStatusCode()); + } + + @Test + void testUpdateDepartment() { + // Create a sample department + Department department = new Department(); + Long id = department.getIdDepartment(); + department.setName("Sample Department"); + + // Mock the behavior of the departmentRepository + when(departmentRepository.findById(id)).thenReturn(Optional.of(department)); + + // Create a sample model + Model model = new ExtendedModelMap(); + model.addAttribute("department", department); + + // Call the method under test + ResponseEntity result = departmentController.update(id, model); + + // Assert that the result is not null and has the expected message + assertNotNull(result); + assertEquals(HttpStatus.OK, result.getStatusCode()); + assertEquals("update", result.getBody()); + } + + // You can add similar test methods for other controller endpoints + +} diff --git a/src/test/java/com/ekomodatech/festivanow/event/controller/EventControllerTest.java b/src/test/java/com/ekomodatech/festivanow/event/controller/EventControllerTest.java new file mode 100644 index 0000000..bee4daf --- /dev/null +++ b/src/test/java/com/ekomodatech/festivanow/event/controller/EventControllerTest.java @@ -0,0 +1,206 @@ +package com.ekomodatech.festivanow.event.controller; + +import com.ekomodatech.festivanow.event.entity.City; +import com.ekomodatech.festivanow.event.entity.Event; +import com.ekomodatech.festivanow.event.entity.Logistic; +import com.ekomodatech.festivanow.event.repository.CityRepository; +import com.ekomodatech.festivanow.event.repository.EventRepository; +import com.ekomodatech.festivanow.event.repository.LogisticRepository; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; + +import org.mockito.MockitoAnnotations; +import org.mockito.Spy; +import org.springframework.http.HttpStatus; + +import java.util.List; +import java.util.Optional; + +import org.springframework.http.ResponseEntity; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.Mockito.*; +import static org.mockito.Mockito.verify; + + +public class EventControllerTest { + + @Mock + private EventRepository eventRepository; + + @Mock + private CityRepository cityRepository; + + @Mock + private LogisticRepository logisticRepository; + + @Spy + @InjectMocks + private EventController eventController; + + @BeforeEach + public void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + public void testFindEvent() { + Long eventId = 1L; + Event event = new Event(); + event.setIdEvent(eventId); + + when(eventRepository.findById(eventId)).thenReturn(Optional.of(event)); + + ResponseEntity responseEntity = eventController.findEvent(eventId, null); + + verify(eventRepository).findById(eventId); + + assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); + assertNotNull(responseEntity.getBody()); + assertEquals(eventId, responseEntity.getBody().getIdEvent()); + } + + @Test + public void testFindEventWithAuthorization() { + Long eventId = 1L; + String createdBy = "testUser"; + Event event = new Event(); + event.setVisibility(true); + event.setIdEvent(eventId); + event.setCreatedBy(createdBy); + + doReturn(createdBy).when(eventController).getCurrentUsername(anyString()); + when(eventRepository.findById(eventId)).thenReturn(Optional.of(event)); + + ResponseEntity responseEntity = eventController.findEvent(eventId, "Bearer "); + + + assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); + } + + @Test + public void testListAllEvents() { + String createdBy = "testUser"; + List events = List.of(new Event(), new Event()); + doReturn(createdBy).when(eventController).getCurrentUsername(anyString()); + when(eventController.getCurrentUsername("Bearer ")).thenReturn(createdBy); + when(eventRepository.findByCreatedBy(anyString())).thenReturn(events); + ResponseEntity >responseEntity = eventController.listAllEvents("Bearer "); + + assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); + assertEquals(events, responseEntity.getBody()); + } + + @Test + public void testListPublicEvents() { + List publicEvents = List.of(new Event(), new Event()); + + when(eventRepository.findByVisibilityFalse()).thenReturn(publicEvents); + + ResponseEntity> responseEntity = eventController.listPublicEvents(); + + verify(eventRepository).findByVisibilityFalse(); + + assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); + assertEquals(publicEvents, responseEntity.getBody()); + } + + @Test + public void testCreateEvent() { + String createdBy = "testUser"; + Event event = new Event(); + event.setCreatedBy(createdBy); + + when(eventRepository.save(event)).thenReturn(event); + + ResponseEntity responseEntity = eventController.createEvent(event, "Bearer "); + + verify(eventRepository).save(event); + + assertEquals(HttpStatus.CREATED, responseEntity.getStatusCode()); + assertEquals(event, responseEntity.getBody()); + } + + @Test + public void testUpdateEvent() { + Long eventId = 1L; + Event existingEvent = new Event(); + existingEvent.setIdEvent(eventId); + existingEvent.setName("Existing Event"); + + Event updatedEvent = new Event(); + updatedEvent.setName("Updated Event"); + + when(eventRepository.findById(eventId)).thenReturn(Optional.of(existingEvent)); + when(eventRepository.save(existingEvent)).thenReturn(existingEvent); + + ResponseEntity responseEntity = eventController.updateEvent(eventId, updatedEvent); + + verify(eventRepository).findById(eventId); + + assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); + assertEquals("Event updated successfully", responseEntity.getBody()); + assertEquals(updatedEvent.getName(), existingEvent.getName()); + } + + @Test + public void testDeleteEvent() { + Long eventId = 1L; + Event event = new Event(); + + when(eventRepository.findById(eventId)).thenReturn(Optional.of(event)); + doNothing().when(eventRepository).deleteById(eventId); + + ResponseEntity responseEntity = eventController.deleteEvent(eventId); + + verify(eventRepository).findById(eventId); + verify(eventRepository).deleteById(eventId); + + assertEquals(HttpStatus.NO_CONTENT, responseEntity.getStatusCode()); + } + + // Add similar test methods for other controller actions + + @Test + void testFindCity() { + City city = new City(); + Long id = city.getIdCity(); + when(cityRepository.findById(id)).thenReturn(Optional.of(city)); + City result = eventController.findCity(id).getBody(); + Assertions.assertNotNull(result); + Assertions.assertEquals(city, result); + } + + @Test + void testListCities() { + City[] cities = {new City(), new City()}; + when(cityRepository.findAll()).thenReturn(List.of(cities)); + List result = eventController.listCities().getBody(); + Assertions.assertNotNull(result); + Assertions.assertEquals(cities.length, result.size()); + } + + + @Test + void testFindLogistic() { + Logistic logistic = new Logistic(); + Long id = logistic.getIdLogistic(); + when(logisticRepository.findById(id)).thenReturn(Optional.of(logistic)); + Logistic result = eventController.findLogistic(id).getBody(); + Assertions.assertNotNull(result); + Assertions.assertEquals(logistic, result); + } + + @Test + void testListLogistics() { + Logistic[] logistics = {new Logistic(), new Logistic()}; + when(logisticRepository.findAll()).thenReturn(List.of(logistics)); + List result = eventController.listLogistics().getBody(); + Assertions.assertNotNull(result); + Assertions.assertEquals(logistics.length, result.size()); + } +} diff --git a/src/test/java/com/ekomodatech/festivanow/event/controller/LogisticControllerTest.java b/src/test/java/com/ekomodatech/festivanow/event/controller/LogisticControllerTest.java new file mode 100644 index 0000000..4e576b4 --- /dev/null +++ b/src/test/java/com/ekomodatech/festivanow/event/controller/LogisticControllerTest.java @@ -0,0 +1,120 @@ +package com.ekomodatech.festivanow.event.controller; + +import com.ekomodatech.festivanow.event.entity.Logistic; +import com.ekomodatech.festivanow.event.repository.LogisticRepository; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.ui.ExtendedModelMap; +import org.springframework.ui.Model; + +import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class LogisticControllerTest { + + @Mock + private LogisticRepository logisticRepository; + + @InjectMocks + private LogisticController logisticController; + + @Test + void testFindLogistic() { + // Create a sample logistic + Logistic logistic = new Logistic(); + long id = logistic.getIdLogistic(); + logistic.setDescription("Sample Logistic"); + + // Mock the behavior of the logisticRepository + when(logisticRepository.findById(id)).thenReturn(Optional.of(logistic)); + + // Call the method under test + Logistic result = logisticController.findLogistic(id); + + // Assert that the result is not null and has the expected logistic + assertNotNull(result); + assertEquals(logistic, result); + } + + @Test + void testListLogistics() { + // Create sample logistics + Logistic logistic1 = new Logistic(); + Logistic logistic2 = new Logistic(); + List logistics = List.of(logistic1, logistic2); + + // Mock the behavior of the logisticRepository + when(logisticRepository.findAll()).thenReturn(logistics); + + // Call the method under test + List result = logisticController.listLogistics(); + + // Assert that the result is not null and contains the expected logistics + assertNotNull(result); + assertEquals(logistics, result); + } + + @Test + void testCreateLogistic() { + // Create a sample logistic + Logistic logistic = new Logistic(); + + logistic.setDescription("Sample Logistic"); + + // Mock the behavior of the logisticRepository + when(logisticRepository.save(logistic)).thenReturn(logistic); + + // Call the method under test + Logistic result = logisticController.createLogistic(logistic); + + // Assert that the result is not null and has the expected logistic + assertNotNull(result); + assertEquals(logistic, result); + } + + @Test + void testDeleteLogistic() { + // Mock the behavior of the logisticRepository +// when(logisticRepository.deleteById(id)).thenReturn(id); + + // Call the method under test + ResponseEntity result = logisticController.deleteLogistic(1L); + + // Assert that the result is not null and has the expected status + assertNotNull(result); + assertEquals(HttpStatus.NO_CONTENT, result.getStatusCode()); + } + + @Test + void testUpdate() { + // Create a sample logistic + Logistic logistic = new Logistic(); + long id = logistic.getIdLogistic(); + logistic.setDescription("Sample Logistic"); + + // Mock the behavior of the logisticRepository + when(logisticRepository.findById(id)).thenReturn(Optional.of(logistic)); + + // Create a sample model + Model model = new ExtendedModelMap(); + + // Call the method under test + String result = logisticController.update(id, model); + + // Assert that the result is not null and has the expected message + assertNotNull(result); + assertEquals("update", result); + } + + // You can add similar test methods for other controller endpoints + +} diff --git a/src/test/java/com/ekomodatech/festivanow/event/controller/PermissionControllerTest.java b/src/test/java/com/ekomodatech/festivanow/event/controller/PermissionControllerTest.java new file mode 100644 index 0000000..9ad7213 --- /dev/null +++ b/src/test/java/com/ekomodatech/festivanow/event/controller/PermissionControllerTest.java @@ -0,0 +1,127 @@ +package com.ekomodatech.festivanow.event.controller; + +import com.ekomodatech.festivanow.event.entity.Permission; +import com.ekomodatech.festivanow.event.repository.AuthorityRepository; +import com.ekomodatech.festivanow.event.repository.EventRepository; +import com.ekomodatech.festivanow.event.repository.PermissionRepository; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.ui.ExtendedModelMap; +import org.springframework.ui.Model; + +import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class PermissionControllerTest { + + @Mock + private PermissionRepository permissionRepository; + +// @Mock +// private AuthorityRepository authorityRepository; +// +// @Mock +// private EventRepository eventRepository; + + @InjectMocks + private PermissionController permissionController; + + @Test + void testFindPermission() { + // Create a sample permission + Permission permission = new Permission(); + long id = permission.getIdPermission(); + permission.setDescription("Sample Permission"); + + // Mock the behavior of the permissionRepository + when(permissionRepository.findById(id)).thenReturn(Optional.of(permission)); + + // Call the method under test + Permission result = permissionController.findPermission(id); + + // Assert that the result is not null and has the expected permission + assertNotNull(result); + assertEquals(permission, result); + } + + @Test + void testListPermissions() { + // Create sample permissions + Permission permission1 = new Permission(); + Permission permission2 = new Permission(); + List permissions = List.of(permission1, permission2); + + // Mock the behavior of the permissionRepository + when(permissionRepository.findAll()).thenReturn(permissions); + + // Call the method under test + List result = permissionController.listPermissions(); + + // Assert that the result is not null and contains the expected permissions + assertNotNull(result); + assertEquals(permissions, result); + } + + @Test + void testCreatePermission() { + // Create a sample permission + Permission permission = new Permission(); + permission.setDescription("Sample Permission"); + + // Mock the behavior of the permissionRepository + when(permissionRepository.save(permission)).thenReturn(permission); + + // Call the method under test + Permission result = permissionController.createPermission(permission); + + // Assert that the result is not null and has the expected permission + assertNotNull(result); + assertEquals(permission, result); + } + + @Test + void testDeletePermission() { + // Mock the behavior of the permissionRepository +// when(permissionRepository.deleteById(1L)).thenReturn(1L); + + // Call the method under test + ResponseEntity result = permissionController.deletePermission(1L); + + // Assert that the result is not null and has the expected status + assertNotNull(result); + assertEquals(HttpStatus.NO_CONTENT, result.getStatusCode()); + } + + @Test + void testUpdate() { + // Create a sample permission + Permission permission = new Permission(); + long id = permission.getIdPermission(); + permission.setDescription("Sample Permission"); + + // Mock the behavior of the permissionRepository + when(permissionRepository.findById(id)).thenReturn(Optional.of(permission)); + + // Create a sample model + Model model = new ExtendedModelMap(); + + // Call the method under test + String result = permissionController.update(id, model); + + // Assert that the result is not null and has the expected message + assertNotNull(result); + assertEquals("update", result); + } + + // You can add similar test methods for other controller endpoints + +} diff --git a/src/test/java/com/ekomodatech/festivanow/event/controller/PolicyControllerTest.java b/src/test/java/com/ekomodatech/festivanow/event/controller/PolicyControllerTest.java new file mode 100644 index 0000000..17f2fa6 --- /dev/null +++ b/src/test/java/com/ekomodatech/festivanow/event/controller/PolicyControllerTest.java @@ -0,0 +1,119 @@ +package com.ekomodatech.festivanow.event.controller; + +import com.ekomodatech.festivanow.event.entity.Policy; +import com.ekomodatech.festivanow.event.repository.PolicyRepository; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.ui.ExtendedModelMap; +import org.springframework.ui.Model; + +import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class PolicyControllerTest { + + @Mock + private PolicyRepository policyRepository; + + @InjectMocks + private PolicyController policyController; + + @Test + void testFindPolicy() { + // Create a sample policy + Policy policy = new Policy(); + long id = policy.getIdPolicy(); + policy.setName("Sample Policy"); + + // Mock the behavior of the policyRepository + when(policyRepository.findById(id)).thenReturn(Optional.of(policy)); + + // Call the method under test + Policy result = policyController.findPolicy(id); + + // Assert that the result is not null and has the expected policy + assertNotNull(result); + assertEquals(policy, result); + } + + @Test + void testListPolicies() { + // Create sample policies + Policy policy1 = new Policy(); + Policy policy2 = new Policy(); + List policies = List.of(policy1, policy2); + + // Mock the behavior of the policyRepository + when(policyRepository.findAll()).thenReturn(policies); + + // Call the method under test + List result = policyController.listPolicies(); + + // Assert that the result is not null and contains the expected policies + assertNotNull(result); + assertEquals(policies, result); + } + + @Test + void testCreatePolicy() { + // Create a sample policy + Policy policy = new Policy(); + policy.setName("Sample Policy"); + + // Mock the behavior of the policyRepository + when(policyRepository.save(policy)).thenReturn(policy); + + // Call the method under test + Policy result = policyController.createPolicy(policy); + + // Assert that the result is not null and has the expected policy + assertNotNull(result); + assertEquals(policy, result); + } + + @Test + void testDeletePolicy() { + // Mock the behavior of the policyRepository +// when(policyRepository.deleteById(1L)).thenReturn(1L); + + // Call the method under test + ResponseEntity result = policyController.deletePolicy(1L); + + // Assert that the result is not null and has the expected status + assertNotNull(result); + assertEquals(HttpStatus.NO_CONTENT, result.getStatusCode()); + } + + @Test + void testUpdate() { + // Create a sample policy + Policy policy = new Policy(); + long id = policy.getIdPolicy(); + policy.setName("Sample Policy"); + + // Mock the behavior of the policyRepository + when(policyRepository.findById(id)).thenReturn(Optional.of(policy)); + + // Create a sample model + Model model = new ExtendedModelMap(); + + // Call the method under test + String result = policyController.update(id, model); + + // Assert that the result is not null and has the expected message + assertNotNull(result); + assertEquals("update", result); + } + + // You can add similar test methods for other controller endpoints + +} diff --git a/src/test/java/com/ekomodatech/festivanow/event/services/AuthorityServiceImplTest.java b/src/test/java/com/ekomodatech/festivanow/event/services/AuthorityServiceImplTest.java new file mode 100644 index 0000000..0912b1b --- /dev/null +++ b/src/test/java/com/ekomodatech/festivanow/event/services/AuthorityServiceImplTest.java @@ -0,0 +1,98 @@ +package com.ekomodatech.festivanow.event.services; + +import com.ekomodatech.festivanow.event.entity.Authority; +import com.ekomodatech.festivanow.event.repository.AuthorityRepository; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; + +import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class AuthorityServiceImplTest { + + @Mock + private AuthorityRepository authorityRepository; + + @InjectMocks + private AuthorityServiceImpl authorityService; + + @BeforeEach + public void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + public void testSaveAuthority() { + // Create a sample authority + Authority authority = new Authority(); +// long id = authority.getIdAuthority(); + authority.setName("Sample Authority"); + + // Mock the behavior of the authorityRepository + Mockito.when(authorityRepository.save(authority)).thenReturn(authority); + + // Call the method under test + Authority savedAuthority = authorityService.save(authority); + + // Assert that the result is not null and has the expected authority + assertNotNull(savedAuthority); + assertEquals(authority, savedAuthority); + } + + @Test + public void testFindByIdAuthority() { + // Create a sample authority + Authority authority = new Authority(); + long id = authority.getIdAuthority(); + authority.setName("Sample Authority"); + + // Mock the behavior of the authorityRepository + Mockito.when(authorityRepository.findById(id)).thenReturn(Optional.of(authority)); + + // Call the method under test + Authority foundAuthority = authorityService.findByIdAuthority(id); + + // Assert that the result is not null and has the expected authority + assertNotNull(foundAuthority); + assertEquals(authority, foundAuthority); + } + + @Test + public void testFindAllAuthorities() { + // Create sample authorities + Authority authority1 = new Authority(); + Authority authority2 = new Authority(); + List authorities = List.of(authority1, authority2); + + // Mock the behavior of the authorityRepository + Mockito.when(authorityRepository.findAll()).thenReturn(authorities); + + // Call the method under test + List foundAuthorities = authorityService.findAllAuthorities(); + + // Assert that the result is not null and contains the expected authorities + assertNotNull(foundAuthorities); + assertEquals(authorities, foundAuthorities); + } + + @Test + public void testDeleteAuthority() { + // Mock the behavior of the authorityRepository + Mockito.doNothing().when(authorityRepository).deleteById(1L); + + // Call the method under test + authorityService.deleteAuthority(1L); + + // Assert that the delete operation was invoked + Mockito.verify(authorityRepository, Mockito.times(1)).deleteById(1L); + } + + // You can add similar test methods for other service methods +} diff --git a/src/test/java/com/ekomodatech/festivanow/event/services/CityServiceImplTest.java b/src/test/java/com/ekomodatech/festivanow/event/services/CityServiceImplTest.java new file mode 100644 index 0000000..25c5e83 --- /dev/null +++ b/src/test/java/com/ekomodatech/festivanow/event/services/CityServiceImplTest.java @@ -0,0 +1,98 @@ +package com.ekomodatech.festivanow.event.services; + +import com.ekomodatech.festivanow.event.entity.City; +import com.ekomodatech.festivanow.event.repository.CityRepository; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; + +import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class CityServiceImplTest { + + @Mock + private CityRepository cityRepository; + + @InjectMocks + private CityServiceImpl cityService; + + @BeforeEach + public void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + public void testSaveCity() { + // Create a sample city + City city = new City(); +// long id = city.getIdCity(); + city.setName("Sample City"); + + // Mock the behavior of the cityRepository + Mockito.when(cityRepository.save(city)).thenReturn(city); + + // Call the method under test + City savedCity = cityService.save(city); + + // Assert that the result is not null and has the expected city + assertNotNull(savedCity); + assertEquals(city, savedCity); + } + + @Test + public void testFindByIdCity() { + // Create a sample city + City city = new City(); + long id = city.getIdCity(); + city.setName("Sample City"); + + // Mock the behavior of the cityRepository + Mockito.when(cityRepository.findById(id)).thenReturn(Optional.of(city)); + + // Call the method under test + City foundCity = cityService.findByIdCity(id); + + // Assert that the result is not null and has the expected city + assertNotNull(foundCity); + assertEquals(city, foundCity); + } + + @Test + public void testFindAllCity() { + // Create sample cities + City city1 = new City(); + City city2 = new City(); + List cities = List.of(city1, city2); + + // Mock the behavior of the cityRepository + Mockito.when(cityRepository.findAll()).thenReturn(cities); + + // Call the method under test + List foundCities = cityService.findAllCity(); + + // Assert that the result is not null and contains the expected cities + assertNotNull(foundCities); + assertEquals(cities, foundCities); + } + + @Test + public void testDeleteCity() { + // Mock the behavior of the cityRepository + Mockito.doNothing().when(cityRepository).deleteById(1L); + + // Call the method under test + cityService.deleteCity(1L); + + // Assert that the delete operation was invoked + Mockito.verify(cityRepository, Mockito.times(1)).deleteById(1L); + } + + // You can add similar test methods for other service methods +} diff --git a/src/test/java/com/ekomodatech/festivanow/event/services/CountryServiceImplTest.java b/src/test/java/com/ekomodatech/festivanow/event/services/CountryServiceImplTest.java new file mode 100644 index 0000000..17f16ee --- /dev/null +++ b/src/test/java/com/ekomodatech/festivanow/event/services/CountryServiceImplTest.java @@ -0,0 +1,99 @@ +package com.ekomodatech.festivanow.event.services; + +import com.ekomodatech.festivanow.event.entity.Country; +import com.ekomodatech.festivanow.event.repository.CountryRepository; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; + +import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class CountryServiceImplTest { + + @Mock + private CountryRepository countryRepository; + + @InjectMocks + private CountryServiceImpl countryService; + + + @BeforeEach + public void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + public void testSaveCountry() { + // Create a sample country + Country country = new Country(); +// long id = country.getIdCountry(); + country.setName("Sample Country"); + + // Mock the behavior of the countryRepository + Mockito.when(countryRepository.save(country)).thenReturn(country); + + // Call the method under test + Country savedCountry = countryService.save(country); + + // Assert that the result is not null and has the expected country + assertNotNull(savedCountry); + assertEquals(country, savedCountry); + } + + @Test + public void testFindByIdCountry() { + // Create a sample country + Country country = new Country(); + long id = country.getIdCountry(); + country.setName("Sample Country"); + + // Mock the behavior of the countryRepository + Mockito.when(countryRepository.findById(id)).thenReturn(Optional.of(country)); + + // Call the method under test + Country foundCountry = countryService.findByIdCountry(id); + + // Assert that the result is not null and has the expected country + assertNotNull(foundCountry); + assertEquals(country, foundCountry); + } + + @Test + public void testFindAllCountry() { + // Create sample countries + Country country1 = new Country(); + Country country2 = new Country(); + List countries = List.of(country1, country2); + + // Mock the behavior of the countryRepository + Mockito.when(countryRepository.findAll()).thenReturn(countries); + + // Call the method under test + List foundCountries = countryService.findAllCountry(); + + // Assert that the result is not null and contains the expected countries + assertNotNull(foundCountries); + assertEquals(countries, foundCountries); + } + + @Test + public void testDeleteCountry() { + // Mock the behavior of the countryRepository + Mockito.doNothing().when(countryRepository).deleteById(1L); + + // Call the method under test + countryService.deleteCountry(1L); + + // Assert that the delete operation was invoked + Mockito.verify(countryRepository, Mockito.times(1)).deleteById(1L); + } + + // You can add similar test methods for other service methods +} diff --git a/src/test/java/com/ekomodatech/festivanow/event/services/DepartmentServiceImplTest.java b/src/test/java/com/ekomodatech/festivanow/event/services/DepartmentServiceImplTest.java new file mode 100644 index 0000000..846f3ca --- /dev/null +++ b/src/test/java/com/ekomodatech/festivanow/event/services/DepartmentServiceImplTest.java @@ -0,0 +1,98 @@ +package com.ekomodatech.festivanow.event.services; + +import com.ekomodatech.festivanow.event.entity.Department; +import com.ekomodatech.festivanow.event.repository.DepartmentRepository; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; + +import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class DepartmentServiceImplTest { + + @Mock + private DepartmentRepository departmentRepository; + + @InjectMocks + private DepartmentServiceImpl departmentService; + + @BeforeEach + public void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + public void testSaveDepartment() { + // Create a sample department + Department department = new Department(); +// long id = department.getIdDepartment(); + department.setName("Sample Department"); + + // Mock the behavior of the departmentRepository + Mockito.when(departmentRepository.save(department)).thenReturn(department); + + // Call the method under test + Department savedDepartment = departmentService.save(department); + + // Assert that the result is not null and has the expected department + assertNotNull(savedDepartment); + assertEquals(department, savedDepartment); + } + + @Test + public void testFindByIdDepartment() { + // Create a sample department + Department department = new Department(); + Long id = department.getIdDepartment(); + department.setName("Sample Department"); + + // Mock the behavior of the departmentRepository + Mockito.when(departmentRepository.findById(id)).thenReturn(Optional.of(department)); + + // Call the method under test + Department foundDepartment = departmentService.findByIdDepartment(id); + + // Assert that the result is not null and has the expected department + assertNotNull(foundDepartment); + assertEquals(department, foundDepartment); + } + + @Test + public void testFindAllDepartment() { + // Create sample departments + Department department1 = new Department(); + Department department2 = new Department(); + List departments = List.of(department1, department2); + + // Mock the behavior of the departmentRepository + Mockito.when(departmentRepository.findAll()).thenReturn(departments); + + // Call the method under test + List foundDepartments = departmentService.findAllDepartment(); + + // Assert that the result is not null and contains the expected departments + assertNotNull(foundDepartments); + assertEquals(departments, foundDepartments); + } + + @Test + public void testDeleteDepartment() { + // Mock the behavior of the departmentRepository + Mockito.doNothing().when(departmentRepository).deleteById(1L); + + // Call the method under test + departmentService.deleteDepartment(1L); + + // Assert that the delete operation was invoked + Mockito.verify(departmentRepository, Mockito.times(1)).deleteById(1L); + } + + // You can add similar test methods for other service methods +} diff --git a/src/test/java/com/ekomodatech/festivanow/event/services/EventServiceImplTest.java b/src/test/java/com/ekomodatech/festivanow/event/services/EventServiceImplTest.java new file mode 100644 index 0000000..a108a02 --- /dev/null +++ b/src/test/java/com/ekomodatech/festivanow/event/services/EventServiceImplTest.java @@ -0,0 +1,98 @@ +package com.ekomodatech.festivanow.event.services; + +import com.ekomodatech.festivanow.event.entity.Event; +import com.ekomodatech.festivanow.event.repository.EventRepository; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; + +import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class EventServiceImplTest { + + @Mock + private EventRepository eventRepository; + + @InjectMocks + private EventServiceImpl eventService; + + @BeforeEach + public void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + public void testSaveEvent() { + // Create a sample event + Event event = new Event(); +// long id = event.getIdEvent(); + event.setName("Sample Event"); + + // Mock the behavior of the eventRepository + Mockito.when(eventRepository.save(event)).thenReturn(event); + + // Call the method under test + Event savedEvent = eventService.save(event); + + // Assert that the result is not null and has the expected event + assertNotNull(savedEvent); + assertEquals(event, savedEvent); + } + + @Test + public void testFindByIdEvent() { + // Create a sample event + Event event = new Event(); + long id = event.getIdEvent(); + event.setName("Sample Event"); + + // Mock the behavior of the eventRepository + Mockito.when(eventRepository.findById(id)).thenReturn(Optional.of(event)); + + // Call the method under test + Event foundEvent = eventService.findByIdEvent(id); + + // Assert that the result is not null and has the expected event + assertNotNull(foundEvent); + assertEquals(event, foundEvent); + } + + @Test + public void testFindAllEvent() { + // Create sample events + Event event1 = new Event(); + Event event2 = new Event(); + List events = List.of(event1, event2); + + // Mock the behavior of the eventRepository + Mockito.when(eventRepository.findAll()).thenReturn(events); + + // Call the method under test + List foundEvents = eventService.findAllEvent(); + + // Assert that the result is not null and contains the expected events + assertNotNull(foundEvents); + assertEquals(events, foundEvents); + } + + @Test + public void testDeleteEvent() { + // Mock the behavior of the eventRepository + Mockito.doNothing().when(eventRepository).deleteById(1L); + + // Call the method under test + eventService.deleteEvent(1L); + + // Assert that the delete operation was invoked + Mockito.verify(eventRepository, Mockito.times(1)).deleteById(1L); + } + + // You can add similar test methods for other service methods +} diff --git a/src/test/java/com/ekomodatech/festivanow/event/services/LogisticServiceImplTest.java b/src/test/java/com/ekomodatech/festivanow/event/services/LogisticServiceImplTest.java new file mode 100644 index 0000000..f3ccadd --- /dev/null +++ b/src/test/java/com/ekomodatech/festivanow/event/services/LogisticServiceImplTest.java @@ -0,0 +1,98 @@ +package com.ekomodatech.festivanow.event.services; + +import com.ekomodatech.festivanow.event.entity.Logistic; +import com.ekomodatech.festivanow.event.repository.LogisticRepository; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; + +import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class LogisticServiceImplTest { + + @Mock + private LogisticRepository logisticRepository; + + @InjectMocks + private LogisticServiceImpl logisticService; + + @BeforeEach + public void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + public void testSaveLogistic() { + // Create a sample logistic + Logistic logistic = new Logistic(); +// long id = logistic.getIdLogistic(); + logistic.setDescription("Sample Logistic"); + + // Mock the behavior of the logisticRepository + Mockito.when(logisticRepository.save(logistic)).thenReturn(logistic); + + // Call the method under test + Logistic savedLogistic = logisticService.save(logistic); + + // Assert that the result is not null and has the expected logistic + assertNotNull(savedLogistic); + assertEquals(logistic, savedLogistic); + } + + @Test + public void testFindByIdLogistic() { + // Create a sample logistic + Logistic logistic = new Logistic(); + long id = logistic.getIdLogistic(); + logistic.setDescription("Sample Logistic"); + + // Mock the behavior of the logisticRepository + Mockito.when(logisticRepository.findById(id)).thenReturn(Optional.of(logistic)); + + // Call the method under test + Logistic foundLogistic = logisticService.findByIdLogistic(id); + + // Assert that the result is not null and has the expected logistic + assertNotNull(foundLogistic); + assertEquals(logistic, foundLogistic); + } + + @Test + public void testFindAllLogistics() { + // Create sample logistics + Logistic logistic1 = new Logistic(); + Logistic logistic2 = new Logistic(); + List logistics = List.of(logistic1, logistic2); + + // Mock the behavior of the logisticRepository + Mockito.when(logisticRepository.findAll()).thenReturn(logistics); + + // Call the method under test + List foundLogistics = logisticService.findAllLogistics(); + + // Assert that the result is not null and contains the expected logistics + assertNotNull(foundLogistics); + assertEquals(logistics, foundLogistics); + } + + @Test + public void testDeleteLogistic() { + // Mock the behavior of the logisticRepository + Mockito.doNothing().when(logisticRepository).deleteById(1L); + + // Call the method under test + logisticService.deleteLogistic(1L); + + // Assert that the delete operation was invoked + Mockito.verify(logisticRepository, Mockito.times(1)).deleteById(1L); + } + + // You can add similar test methods for other service methods +} diff --git a/src/test/java/com/ekomodatech/festivanow/event/services/PermissionServiceImplTest.java b/src/test/java/com/ekomodatech/festivanow/event/services/PermissionServiceImplTest.java new file mode 100644 index 0000000..2608301 --- /dev/null +++ b/src/test/java/com/ekomodatech/festivanow/event/services/PermissionServiceImplTest.java @@ -0,0 +1,98 @@ +package com.ekomodatech.festivanow.event.services; + +import com.ekomodatech.festivanow.event.entity.Permission; +import com.ekomodatech.festivanow.event.repository.PermissionRepository; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; + +import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class PermissionServiceImplTest { + + @Mock + private PermissionRepository permissionRepository; + + @InjectMocks + private PermissionServiceImpl permissionService; + + @BeforeEach + public void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + public void testSavePermission() { + // Create a sample permission + Permission permission = new Permission(); +// long id = permission.getIdPermission(); + permission.setDescription("Sample Permission"); + + // Mock the behavior of the permissionRepository + Mockito.when(permissionRepository.save(permission)).thenReturn(permission); + + // Call the method under test + Permission savedPermission = permissionService.save(permission); + + // Assert that the result is not null and has the expected permission + assertNotNull(savedPermission); + assertEquals(permission, savedPermission); + } + + @Test + public void testFindByIdPermission() { + // Create a sample permission + Permission permission = new Permission(); + long id = permission.getIdPermission(); + permission.setDescription("Sample Permission"); + + // Mock the behavior of the permissionRepository + Mockito.when(permissionRepository.findById(id)).thenReturn(Optional.of(permission)); + + // Call the method under test + Permission foundPermission = permissionService.findByIdPermission(id); + + // Assert that the result is not null and has the expected permission + assertNotNull(foundPermission); + assertEquals(permission, foundPermission); + } + + @Test + public void testFindAllPermissions() { + // Create sample permissions + Permission permission1 = new Permission(); + Permission permission2 = new Permission(); + List permissions = List.of(permission1, permission2); + + // Mock the behavior of the permissionRepository + Mockito.when(permissionRepository.findAll()).thenReturn(permissions); + + // Call the method under test + List foundPermissions = permissionService.findAllPermissions(); + + // Assert that the result is not null and contains the expected permissions + assertNotNull(foundPermissions); + assertEquals(permissions, foundPermissions); + } + + @Test + public void testDeletePermission() { + // Mock the behavior of the permissionRepository + Mockito.doNothing().when(permissionRepository).deleteById(1L); + + // Call the method under test + permissionService.deletePermission(1L); + + // Assert that the delete operation was invoked + Mockito.verify(permissionRepository, Mockito.times(1)).deleteById(1L); + } + + // You can add similar test methods for other service methods +} diff --git a/src/test/java/com/ekomodatech/festivanow/event/services/PolicyServiceImplTest.java b/src/test/java/com/ekomodatech/festivanow/event/services/PolicyServiceImplTest.java new file mode 100644 index 0000000..c8d9f0a --- /dev/null +++ b/src/test/java/com/ekomodatech/festivanow/event/services/PolicyServiceImplTest.java @@ -0,0 +1,98 @@ +package com.ekomodatech.festivanow.event.services; + +import com.ekomodatech.festivanow.event.entity.Policy; +import com.ekomodatech.festivanow.event.repository.PolicyRepository; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; + +import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class PolicyServiceImplTest { + + @Mock + private PolicyRepository policyRepository; + + @InjectMocks + private PolicyServiceImpl policyService; + + @BeforeEach + public void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + public void testSavePolicy() { + // Create a sample policy + Policy policy = new Policy(); +// long id = policy.getIdPolicy(); + policy.setName("Sample Policy"); + + // Mock the behavior of the policyRepository + Mockito.when(policyRepository.save(policy)).thenReturn(policy); + + // Call the method under test + Policy savedPolicy = policyService.save(policy); + + // Assert that the result is not null and has the expected policy + assertNotNull(savedPolicy); + assertEquals(policy, savedPolicy); + } + + @Test + public void testFindByIdPolicy() { + // Create a sample policy + Policy policy = new Policy(); + long id = policy.getIdPolicy(); + policy.setName("Sample Policy"); + + // Mock the behavior of the policyRepository + Mockito.when(policyRepository.findById(id)).thenReturn(Optional.of(policy)); + + // Call the method under test + Policy foundPolicy = policyService.findByIdPolicy(id); + + // Assert that the result is not null and has the expected policy + assertNotNull(foundPolicy); + assertEquals(policy, foundPolicy); + } + + @Test + public void testFindAllPolicies() { + // Create sample policies + Policy policy1 = new Policy(); + Policy policy2 = new Policy(); + List policies = List.of(policy1, policy2); + + // Mock the behavior of the policyRepository + Mockito.when(policyRepository.findAll()).thenReturn(policies); + + // Call the method under test + List foundPolicies = policyService.findAllPolicies(); + + // Assert that the result is not null and contains the expected policies + assertNotNull(foundPolicies); + assertEquals(policies, foundPolicies); + } + + @Test + public void testDeletePolicy() { + // Mock the behavior of the policyRepository + Mockito.doNothing().when(policyRepository).deleteById(1L); + + // Call the method under test + policyService.deletePolicy(1L); + + // Assert that the delete operation was invoked + Mockito.verify(policyRepository, Mockito.times(1)).deleteById(1L); + } + + // You can add similar test methods for other service methods +} diff --git a/target/classes/com/ekomodatech/festivanow/event/controller/ValidationException.class b/target/classes/com/ekomodatech/festivanow/event/controller/ValidationException.class index 46f8d06..5c5fd37 100644 Binary files a/target/classes/com/ekomodatech/festivanow/event/controller/ValidationException.class and b/target/classes/com/ekomodatech/festivanow/event/controller/ValidationException.class differ