File size: 8,005 Bytes
86a3cc6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package com.dalab.autolabel.controller;

import com.dalab.autolabel.client.rest.dto.*;
import com.dalab.autolabel.exception.AutoLabelingException;
import com.dalab.autolabel.service.ILabelingJobService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.BeforeEach;
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.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebMvcTest(LabelingJobController.class)
class LabelingJobControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private ILabelingJobService labelingJobService;

    @Autowired
    private ObjectMapper objectMapper;

    private LabelingJobRequest labelingJobRequest;
    private LabelingJobResponse labelingJobResponse;
    private LabelingJobStatusResponse labelingJobStatusResponse;
    private LabelingFeedbackRequest labelingFeedbackRequest;
    private LabelingFeedbackResponse labelingFeedbackResponse;

    @BeforeEach
    void setUp() {
        String jobId = UUID.randomUUID().toString();
        labelingJobRequest = LabelingJobRequest.builder().jobName("Test Job").build();
        labelingJobResponse = LabelingJobResponse.builder()
                .jobId(jobId)
                .status("SUBMITTED")
                .submittedAt(LocalDateTime.now())
                .message("Job submitted")
                .build();
        labelingJobStatusResponse = LabelingJobStatusResponse.builder()
                .jobId(jobId).jobName("Test Job").status("RUNNING").build();

        labelingFeedbackRequest = LabelingFeedbackRequest.builder()
                .assetId("asset-123")
                .labelingJobId(jobId)
                .feedbackItems(List.of(LabelingFeedbackRequest.FeedbackItem.builder()
                        .suggestedLabel("PII")
                        .type(LabelingFeedbackRequest.FeedbackType.CONFIRMED)
                        .build()))
                .build();
        labelingFeedbackResponse = LabelingFeedbackResponse.builder()
                .feedbackId(UUID.randomUUID().toString())
                .assetId("asset-123")
                .status("RECEIVED")
                .build();
    }

    @Test
    @WithMockUser(authorities = "ROLE_DATA_STEWARD")
    void submitLabelingJob_ValidRequest_ShouldSucceed() throws Exception {
        when(labelingJobService.submitLabelingJob(any(LabelingJobRequest.class))).thenReturn(labelingJobResponse);

        mockMvc.perform(post("/api/v1/labeling/jobs")
                        .with(csrf())
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(objectMapper.writeValueAsString(labelingJobRequest)))
                .andExpect(status().isAccepted())
                .andExpect(jsonPath("$.jobId").value(labelingJobResponse.getJobId()));
    }
    
    @Test
    @WithMockUser(authorities = "ROLE_USER") // Insufficient role
    void submitLabelingJob_UserRole_ShouldBeForbidden() throws Exception {
        mockMvc.perform(post("/api/v1/labeling/jobs")
                        .with(csrf())
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(objectMapper.writeValueAsString(labelingJobRequest)))
                .andExpect(status().isForbidden());
    }

    @Test
    @WithMockUser(authorities = "ROLE_DATA_STEWARD")
    void getJobStatus_ExistingJob_ShouldReturnStatus() throws Exception {
        when(labelingJobService.getJobStatus(eq(labelingJobStatusResponse.getJobId()))).thenReturn(labelingJobStatusResponse);

        mockMvc.perform(get("/api/v1/labeling/jobs/{jobId}", labelingJobStatusResponse.getJobId()))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.jobId").value(labelingJobStatusResponse.getJobId()))
                .andExpect(jsonPath("$.status").value("RUNNING"));
    }

    @Test
    @WithMockUser(authorities = "ROLE_DATA_STEWARD")
    void getJobStatus_NonExistingJob_ShouldReturnNotFound() throws Exception {
        String nonExistentJobId = UUID.randomUUID().toString();
        when(labelingJobService.getJobStatus(eq(nonExistentJobId))).thenReturn(null);

        mockMvc.perform(get("/api/v1/labeling/jobs/{jobId}", nonExistentJobId))
                .andExpect(status().isNotFound());
    }

    @Test
    @WithMockUser(authorities = "ROLE_USER")
    void listJobs_ShouldReturnJobList() throws Exception {
        LabelingJobListResponse listResponse = LabelingJobListResponse.builder()
                .jobs(Collections.singletonList(labelingJobStatusResponse))
                .pageNumber(0).pageSize(20).totalElements(1L).totalPages(1).last(true)
                .build();
        when(labelingJobService.listJobs(any(Pageable.class))).thenReturn(listResponse);

        mockMvc.perform(get("/api/v1/labeling/jobs").param("page", "0").param("size", "20"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.jobs[0].jobId").value(labelingJobStatusResponse.getJobId()));
    }

    @Test
    @WithMockUser(authorities = "ROLE_DATA_STEWARD")
    void submitLabelingFeedback_ValidRequest_ShouldSucceed() throws Exception {
        when(labelingJobService.processLabelingFeedback(any(LabelingFeedbackRequest.class))).thenReturn(labelingFeedbackResponse);

        mockMvc.perform(post("/api/v1/labeling/feedback")
                        .with(csrf())
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(objectMapper.writeValueAsString(labelingFeedbackRequest)))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.feedbackId").value(labelingFeedbackResponse.getFeedbackId()));
    }
    
    @Test
    @WithMockUser(authorities = "ROLE_USER") // Insufficient role
    void submitLabelingFeedback_UserRole_ShouldBeForbidden() throws Exception {
         mockMvc.perform(post("/api/v1/labeling/feedback")
                        .with(csrf())
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(objectMapper.writeValueAsString(labelingFeedbackRequest)))
                .andExpect(status().isForbidden());
    }

    @Test
    @WithMockUser(authorities = "ROLE_DATA_STEWARD")
    void submitLabelingJob_ServiceThrowsAutoLabelingException_ShouldReturnBadRequest() throws Exception {
        when(labelingJobService.submitLabelingJob(any(LabelingJobRequest.class)))
            .thenThrow(new AutoLabelingException("Test ML config error"));

        mockMvc.perform(post("/api/v1/labeling/jobs")
                        .with(csrf())
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(objectMapper.writeValueAsString(labelingJobRequest)))
                .andExpect(status().isBadRequest())
                .andExpect(jsonPath("$.message").value("Test ML config error"));
    }
}