diff --git a/src/main/java/io/khasang/pm/config/AppConfig.java b/src/main/java/io/khasang/pm/config/AppConfig.java index 2228d30..9afbb17 100644 --- a/src/main/java/io/khasang/pm/config/AppConfig.java +++ b/src/main/java/io/khasang/pm/config/AppConfig.java @@ -1,17 +1,20 @@ package io.khasang.pm.config; import io.khasang.pm.dao.CatDao; +import io.khasang.pm.dao.DocumentDao; import io.khasang.pm.dao.EmployeeDao; import io.khasang.pm.dao.ProjectDao; -import io.khasang.pm.dao.DocumentDao; +import io.khasang.pm.dao.RoleDao; import io.khasang.pm.dao.impl.CatDaoImpl; +import io.khasang.pm.dao.impl.DocumentDaoImpl; import io.khasang.pm.dao.impl.EmployeeDaoImpl; import io.khasang.pm.dao.impl.ProjectDaoImpl; -import io.khasang.pm.dao.impl.DocumentDaoImpl; +import io.khasang.pm.dao.impl.RoleDaoImpl; import io.khasang.pm.entity.Cat; import io.khasang.pm.entity.Employee; import io.khasang.pm.entity.Project; import io.khasang.pm.entity.Document; +import io.khasang.pm.entity.Role; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy; @@ -34,9 +37,14 @@ public DocumentDao documentDao(){ return new DocumentDaoImpl(Document.class); } + @Bean + public RoleDao roleDao() { + return new RoleDaoImpl(Role.class); + } + @Bean public EmployeeDao employeeDao(){ return new EmployeeDaoImpl(Employee.class); } -} +} \ No newline at end of file diff --git a/src/main/java/io/khasang/pm/config/jpa/HibernateConfig.java b/src/main/java/io/khasang/pm/config/jpa/HibernateConfig.java index 367615f..1172e05 100644 --- a/src/main/java/io/khasang/pm/config/jpa/HibernateConfig.java +++ b/src/main/java/io/khasang/pm/config/jpa/HibernateConfig.java @@ -62,4 +62,4 @@ public void setDataSource(DataSource dataSource) { public void setEnvironment(Environment environment) { this.environment = environment; } -} +} \ No newline at end of file diff --git a/src/main/java/io/khasang/pm/config/security/SecurityConfig.java b/src/main/java/io/khasang/pm/config/security/SecurityConfig.java index bc40efc..662e6a9 100644 --- a/src/main/java/io/khasang/pm/config/security/SecurityConfig.java +++ b/src/main/java/io/khasang/pm/config/security/SecurityConfig.java @@ -37,4 +37,4 @@ private PasswordEncoder passwordEncoder() { public void setUserDetailsService(UserDetailsService userDetailsService) { this.userDetailsService = userDetailsService; } -} +} \ No newline at end of file diff --git a/src/main/java/io/khasang/pm/config/security/SecurityInit.java b/src/main/java/io/khasang/pm/config/security/SecurityInit.java index ec1f649..cb68568 100644 --- a/src/main/java/io/khasang/pm/config/security/SecurityInit.java +++ b/src/main/java/io/khasang/pm/config/security/SecurityInit.java @@ -3,4 +3,5 @@ import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer; public class SecurityInit extends AbstractSecurityWebApplicationInitializer { -} + +} \ No newline at end of file diff --git a/src/main/java/io/khasang/pm/controller/AppController.java b/src/main/java/io/khasang/pm/controller/AppController.java index 863ccfc..3af351d 100644 --- a/src/main/java/io/khasang/pm/controller/AppController.java +++ b/src/main/java/io/khasang/pm/controller/AppController.java @@ -30,11 +30,16 @@ public String getProjectPage() { return "project"; } - @RequestMapping("/") + @RequestMapping("/getCat") public String getHelloPage() { return "cat"; } + @RequestMapping("/") + public String getRolePage() { + return "role"; + } + @RequestMapping("/doc") public String workWithDoc() { return "doc"; diff --git a/src/main/java/io/khasang/pm/controller/CatController.java b/src/main/java/io/khasang/pm/controller/CatController.java index c3d8403..25b5c02 100644 --- a/src/main/java/io/khasang/pm/controller/CatController.java +++ b/src/main/java/io/khasang/pm/controller/CatController.java @@ -36,4 +36,4 @@ public List getAll(){ public void setCatService(CatService catService) { this.catService = catService; } -} +} \ No newline at end of file diff --git a/src/main/java/io/khasang/pm/controller/Rabbit.java b/src/main/java/io/khasang/pm/controller/Rabbit.java index 8c5a68c..711bb4e 100644 --- a/src/main/java/io/khasang/pm/controller/Rabbit.java +++ b/src/main/java/io/khasang/pm/controller/Rabbit.java @@ -43,4 +43,4 @@ public void cleanUp() { public void init() { System.err.println("Rabbit is coming..."); } -} +} \ No newline at end of file diff --git a/src/main/java/io/khasang/pm/controller/RoleController.java b/src/main/java/io/khasang/pm/controller/RoleController.java new file mode 100644 index 0000000..5ffcd40 --- /dev/null +++ b/src/main/java/io/khasang/pm/controller/RoleController.java @@ -0,0 +1,39 @@ +package io.khasang.pm.controller; + +import io.khasang.pm.entity.Role; +import io.khasang.pm.service.RoleService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@Controller +@RequestMapping("/role") +// localhost:8080/role +public class RoleController { + private RoleService roleService; + + @RequestMapping(value = "/add", method = RequestMethod.POST, produces = "application/json;charset=utf-8") + @ResponseBody + public Role addRole(@RequestBody Role role){ + return roleService.add(role); + } + + @RequestMapping(value = "/get/{id}", method = RequestMethod.GET) + @ResponseBody + public Role getById(@PathVariable("id") long id) { + return roleService.getById(id); + } + + @RequestMapping(value = "/all", method = RequestMethod.GET) + @ResponseBody + public List getAll(){ + return roleService.getAll(); + } + + @Autowired + public void setRoleService(RoleService roleService){ + this.roleService = roleService; + } +} \ No newline at end of file diff --git a/src/main/java/io/khasang/pm/dao/RoleDao.java b/src/main/java/io/khasang/pm/dao/RoleDao.java new file mode 100644 index 0000000..23e427d --- /dev/null +++ b/src/main/java/io/khasang/pm/dao/RoleDao.java @@ -0,0 +1,6 @@ +package io.khasang.pm.dao; + +import io.khasang.pm.entity.Role; + +public interface RoleDao extends BasicDao { +} diff --git a/src/main/java/io/khasang/pm/dao/impl/RoleDaoImpl.java b/src/main/java/io/khasang/pm/dao/impl/RoleDaoImpl.java new file mode 100644 index 0000000..79c1d23 --- /dev/null +++ b/src/main/java/io/khasang/pm/dao/impl/RoleDaoImpl.java @@ -0,0 +1,10 @@ +package io.khasang.pm.dao.impl; + +import io.khasang.pm.dao.RoleDao; +import io.khasang.pm.entity.Role; + +public class RoleDaoImpl extends BasicDaoImpl implements RoleDao { + public RoleDaoImpl(Class entityClass) { + super(entityClass); + } +} diff --git a/src/main/java/io/khasang/pm/entity/Login.java b/src/main/java/io/khasang/pm/entity/Login.java new file mode 100644 index 0000000..8e9a7f9 --- /dev/null +++ b/src/main/java/io/khasang/pm/entity/Login.java @@ -0,0 +1,37 @@ +package io.khasang.pm.entity; + +import javax.persistence.*; + +@Entity +@Table(name = "login") +public class Login { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + private String name; + private String description; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } +} diff --git a/src/main/java/io/khasang/pm/entity/Role.java b/src/main/java/io/khasang/pm/entity/Role.java new file mode 100644 index 0000000..4fa4993 --- /dev/null +++ b/src/main/java/io/khasang/pm/entity/Role.java @@ -0,0 +1,51 @@ +package io.khasang.pm.entity; + +import javax.persistence.*; +import java.util.ArrayList; +import java.util.List; + +@Entity +@Table(name = "roles") +public class Role { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private long id; + + private String name; + private String description; + + public List getLogin() { + return login; + } + + public void setLogin(List login) { + this.login = login; + } + + @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER) + private List login = new ArrayList<>(); + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } +} \ No newline at end of file diff --git a/src/main/java/io/khasang/pm/service/RoleService.java b/src/main/java/io/khasang/pm/service/RoleService.java new file mode 100644 index 0000000..b69c813 --- /dev/null +++ b/src/main/java/io/khasang/pm/service/RoleService.java @@ -0,0 +1,13 @@ +package io.khasang.pm.service; + +import io.khasang.pm.entity.Role; + +import java.util.List; + +public interface RoleService { + Role add(Role role); + + Role getById(long id); + + List getAll(); +} \ No newline at end of file diff --git a/src/main/java/io/khasang/pm/service/impl/RoleServiceImpl.java b/src/main/java/io/khasang/pm/service/impl/RoleServiceImpl.java new file mode 100644 index 0000000..dcfb189 --- /dev/null +++ b/src/main/java/io/khasang/pm/service/impl/RoleServiceImpl.java @@ -0,0 +1,34 @@ +package io.khasang.pm.service.impl; + +import io.khasang.pm.dao.RoleDao; +import io.khasang.pm.entity.Role; +import io.khasang.pm.service.RoleService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service("roleService") +public class RoleServiceImpl implements RoleService { + private RoleDao roleDao; + + @Override + public Role add(Role role) { + return roleDao.add(role); + } + + @Override + public Role getById(long id) { + return roleDao.getById(id); + } + + @Override + public List getAll() { + return roleDao.getAll(); + } + + @Autowired + public void setRoleDao (RoleDao roleDao){ + this.roleDao = roleDao; + } +} \ No newline at end of file diff --git a/src/main/resources/hibernate.properties b/src/main/resources/hibernate.properties index ba58962..5e878f8 100644 --- a/src/main/resources/hibernate.properties +++ b/src/main/resources/hibernate.properties @@ -1,4 +1,4 @@ hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect hibernate.show_sql=true hibernate.format_sql=true -hibernate.hbm2ddl.auto=update \ No newline at end of file +hibernate.hbm2ddl.auto=create \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/views/role.jsp b/src/main/webapp/WEB-INF/views/role.jsp new file mode 100644 index 0000000..bdf536e --- /dev/null +++ b/src/main/webapp/WEB-INF/views/role.jsp @@ -0,0 +1,113 @@ +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + + + Title + + + + +

Role Menu

+ + + + + + + + + + + + + + + + + + + + + + +
Request typeURLValue
Get All roles - GET/role/all + +
Get role by id - GET/role/get/{id} + id: + +
add new role - POST/role/add + name: + description: + +
+ +
+
+ RESPONSE> +
+
+
+ + \ No newline at end of file diff --git a/src/test/java/io/khasang/pm/controller/RoleControllerIntegrationTest.java b/src/test/java/io/khasang/pm/controller/RoleControllerIntegrationTest.java new file mode 100644 index 0000000..df2c919 --- /dev/null +++ b/src/test/java/io/khasang/pm/controller/RoleControllerIntegrationTest.java @@ -0,0 +1,83 @@ +package io.khasang.pm.controller; + +import io.khasang.pm.entity.Login; +import io.khasang.pm.entity.Role; +import org.junit.Test; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.client.RestTemplate; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +public class RoleControllerIntegrationTest { + private static final String ROOT = "http://localhost:8080/role"; + private static final String ADD = "/add"; + private static final String GET = "/get"; + private static final String ALL = "/all"; + + @Test + public void checkAddRole(){ + Role testRole = createRole(); + RestTemplate template = new RestTemplate(); + ResponseEntity responseEntity = template.exchange( + ROOT + GET + "/{id}", + HttpMethod.GET, + null, + Role.class, + testRole.getId() + ); + + assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); + Role receivedRole = responseEntity.getBody(); + assertNotNull(receivedRole); + } + + private Role createRole() { + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON_UTF8); + + Role role = preFillRole(); + HttpEntity entity = new HttpEntity<>(role, headers); + RestTemplate template = new RestTemplate(); + Role createdRole = template.exchange( + ROOT + ADD, + HttpMethod.POST, + entity, + Role.class + ).getBody(); + //Assert - класс с статич.методами для сравнения ожидаемого значения с актуальным + assertNotNull(createdRole); + assertEquals("testRole", createdRole.getName()); + + return createdRole; + } + + private Role preFillRole() { + Role role = new Role(); + role.setName("testRole"); + role.setDescription("test role only for integration test"); + + Login loginOne = new Login(); + loginOne.setName("loginOne"); + loginOne.setDescription("This is login number one"); + + Login loginTwo = new Login(); + loginTwo.setName("loginTwo"); + loginTwo.setDescription("This is login number two"); + + List logins = new ArrayList<>(); + logins.add(loginOne); + logins.add(loginTwo); + + role.setLogin(logins); + return role; + } +}