Developer 역할 Spring 골격 smoke #4

Open
forge-bot wants to merge 9 commits from forge/role-developer-live-v3-001-attempt-1-run-2d01a4fe6309 into main
9 changed files with 298 additions and 0 deletions

View file

@ -0,0 +1,3 @@
# role-developer-live-v3-001-attempt-1-run-2d01a4fe6309
Forge 이슈 작업 브랜치 `forge/role-developer-live-v3-001-attempt-1-run-2d01a4fe6309`.

40
pom.xml Normal file
View file

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.5</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>runtime-role-matrix-live-202607141522-v3</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>Runtime Role Matrix Live</name>
<description>Developer role Spring Boot skeleton application</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View file

@ -0,0 +1,12 @@
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DeveloperApplication {
public static void main(String[] args) {
SpringApplication.run(DeveloperApplication.class, args);
}
}

View file

@ -0,0 +1,37 @@
package com.example.demo.controller;
import com.example.demo.service.DeveloperService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/api/roles")
public class DeveloperController {
private final DeveloperService developerService;
public DeveloperController(DeveloperService developerService) {
this.developerService = developerService;
}
@GetMapping
public ResponseEntity<List<String>> getAllRoles() {
return ResponseEntity.ok(developerService.getAllRoles());
}
@GetMapping("/{role}")
public ResponseEntity<Map<String, String>> getRolePermission(@PathVariable String role) {
String permission = developerService.getRolePermission(role);
return ResponseEntity.ok(Map.of("role", role, "permission", permission));
}
@GetMapping("/{role}/check")
public ResponseEntity<Map<String, Boolean>> checkPermission(
@PathVariable String role,
@RequestParam String action) {
boolean allowed = developerService.hasPermission(role, action);
return ResponseEntity.ok(Map.of("action", action, "allowed", allowed));
}
}

View file

@ -0,0 +1,37 @@
package com.example.demo.service;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
@Service
public class DeveloperService {
private final Map<String, String> roleMatrix = new HashMap<>();
public DeveloperService() {
roleMatrix.put("developer", "read-write");
roleMatrix.put("viewer", "read-only");
roleMatrix.put("admin", "full-access");
}
public String getRolePermission(String role) {
return roleMatrix.getOrDefault(role.toLowerCase(), "no-access");
}
public List<String> getAllRoles() {
return List.copyOf(roleMatrix.keySet());
}
public boolean hasPermission(String role, String requiredPermission) {
String permission = getRolePermission(role);
if ("full-access".equals(permission)) {
return true;
}
if ("read-write".equals(permission)) {
return !"delete".equals(requiredPermission);
}
return "read".equals(requiredPermission);
}
}

View file

@ -0,0 +1,2 @@
spring.application.name=runtime-role-matrix-live-202607141522-v3
server.port=8080

View file

@ -0,0 +1,14 @@
package com.example.demo;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestPropertySource(properties = {"spring.main.allow-bean-definition-overriding=true"})
class DeveloperApplicationTests {
@Test
void contextLoads() {
}
}

View file

@ -0,0 +1,65 @@
package com.example.demo.controller;
import com.example.demo.service.DeveloperService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;
import java.util.List;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@WebMvcTest(DeveloperController.class)
class DeveloperControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private DeveloperService developerService;
@Test
void getAllRoles_returnsRoleList() throws Exception {
when(developerService.getAllRoles()).thenReturn(List.of("developer", "viewer", "admin"));
mockMvc.perform(get("/api/roles"))
.andExpect(status().isOk())
.andExpect(jsonPath("$").isArray())
.andExpect(jsonPath("$.length()").value(3))
.andExpect(jsonPath("$[0]").value("developer"));
}
@Test
void getRolePermission_returnsPermission() throws Exception {
when(developerService.getRolePermission("developer")).thenReturn("read-write");
mockMvc.perform(get("/api/roles/developer"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.role").value("developer"))
.andExpect(jsonPath("$.permission").value("read-write"));
}
@Test
void checkPermission_allowed_returnsTrue() throws Exception {
when(developerService.hasPermission("developer", "read")).thenReturn(true);
mockMvc.perform(get("/api/roles/developer/check").param("action", "read"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.action").value("read"))
.andExpect(jsonPath("$.allowed").value(true));
}
@Test
void checkPermission_denied_returnsFalse() throws Exception {
when(developerService.hasPermission("developer", "delete")).thenReturn(false);
mockMvc.perform(get("/api/roles/developer/check").param("action", "delete"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.action").value("delete"))
.andExpect(jsonPath("$.allowed").value(false));
}
}

View file

@ -0,0 +1,88 @@
package com.example.demo.service;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.DisplayName;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
class DeveloperServiceTest {
private DeveloperService developerService;
@BeforeEach
void setUp() {
developerService = new DeveloperService();
}
@Test
@DisplayName("Developer role should have read-write permission")
void getRolePermission_developer_returnsReadWrite() {
String permission = developerService.getRolePermission("developer");
assertEquals("read-write", permission);
}
@Test
@DisplayName("Viewer role should have read-only permission")
void getRolePermission_viewer_returnsReadOnly() {
String permission = developerService.getRolePermission("viewer");
assertEquals("read-only", permission);
}
@Test
@DisplayName("Admin role should have full-access permission")
void getRolePermission_admin_returnsFullAccess() {
String permission = developerService.getRolePermission("admin");
assertEquals("full-access", permission);
}
@Test
@DisplayName("Unknown role should return no-access")
void getRolePermission_unknown_returnsNoAccess() {
String permission = developerService.getRolePermission("unknown");
assertEquals("no-access", permission);
}
@Test
@DisplayName("Role lookup should be case-insensitive")
void getRolePermission_caseInsensitive() {
assertEquals("read-write", developerService.getRolePermission("DEVELOPER"));
assertEquals("read-write", developerService.getRolePermission("Developer"));
}
@Test
@DisplayName("getAllRoles should return all defined roles")
void getAllRoles_returnsAllRoles() {
List<String> roles = developerService.getAllRoles();
assertEquals(3, roles.size());
assertTrue(roles.contains("developer"));
assertTrue(roles.contains("viewer"));
assertTrue(roles.contains("admin"));
}
@Test
@DisplayName("Admin should have all permissions")
void hasPermission_admin_hasAllPermissions() {
assertTrue(developerService.hasPermission("admin", "read"));
assertTrue(developerService.hasPermission("admin", "write"));
assertTrue(developerService.hasPermission("admin", "delete"));
}
@Test
@DisplayName("Developer should have read and write but not delete")
void hasPermission_developer_readWriteNotDelete() {
assertTrue(developerService.hasPermission("developer", "read"));
assertTrue(developerService.hasPermission("developer", "write"));
assertFalse(developerService.hasPermission("developer", "delete"));
}
@Test
@DisplayName("Viewer should only have read permission")
void hasPermission_viewer_onlyRead() {
assertTrue(developerService.hasPermission("viewer", "read"));
assertFalse(developerService.hasPermission("viewer", "write"));
assertFalse(developerService.hasPermission("viewer", "delete"));
}
}