diff --git a/src/main/java/io/khasang/pm/config/AppConfig.java b/src/main/java/io/khasang/pm/config/AppConfig.java index b10c81b..69e234d 100644 --- a/src/main/java/io/khasang/pm/config/AppConfig.java +++ b/src/main/java/io/khasang/pm/config/AppConfig.java @@ -29,7 +29,8 @@ public CatDao catDao() { } @Bean - public ProjectDao projectDao() {return new ProjectDaoImpl(Project.class); + public ProjectDao projectDao() { + return new ProjectDaoImpl(Project.class); } @Bean @@ -37,6 +38,11 @@ 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); @@ -46,4 +52,4 @@ public EmployeeDao employeeDao(){ public ChildDocumentDao childDocumentDao() { return new ChildDocumentDaoImpl(ChildDocument.class); } -} +} \ 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 e8fc5de..bc837ae 100644 --- a/src/main/java/io/khasang/pm/controller/AppController.java +++ b/src/main/java/io/khasang/pm/controller/AppController.java @@ -35,6 +35,11 @@ public String getHelloPage() { return "cat"; } + @RequestMapping("/role") + public String getRole() { + return "role"; + } + @RequestMapping("/childDocument") public String getСhildDocumentPage() { return "childdocument"; 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..b2d9b8a --- /dev/null +++ b/src/main/java/io/khasang/pm/controller/RoleController.java @@ -0,0 +1,38 @@ +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") +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..fc1d73e --- /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); + } +} \ No newline at end of file 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..e70ae97 --- /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; + } +} \ No newline at end of file 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..eed5aa8 --- /dev/null +++ b/src/main/java/io/khasang/pm/service/RoleService.java @@ -0,0 +1,11 @@ +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/webapp/WEB-INF/views/role.jsp b/src/main/webapp/WEB-INF/views/role.jsp new file mode 100644 index 0000000..340e95e --- /dev/null +++ b/src/main/webapp/WEB-INF/views/role.jsp @@ -0,0 +1,109 @@ +<%@ 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..fa3a23c --- /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; + } +} \ No newline at end of file