|
| 1 | +package com.cloudmedia.policy.api.policy; |
| 2 | + |
| 3 | +import com.cloudmedia.policy.persistence.entity.ContentPolicyEntity; |
| 4 | +import com.cloudmedia.policy.persistence.entity.ModerationState; |
| 5 | +import com.cloudmedia.policy.persistence.repository.ContentPolicyRepository; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | +import org.springframework.beans.factory.annotation.Autowired; |
| 8 | +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
| 9 | +import org.springframework.boot.test.context.SpringBootTest; |
| 10 | +import org.springframework.http.MediaType; |
| 11 | +import org.springframework.test.web.servlet.MockMvc; |
| 12 | +import org.springframework.transaction.annotation.Transactional; |
| 13 | + |
| 14 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; |
| 15 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
| 16 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 17 | + |
| 18 | +@SpringBootTest |
| 19 | +@AutoConfigureMockMvc |
| 20 | +@Transactional |
| 21 | +class ModerationControllerTest { |
| 22 | + |
| 23 | + @Autowired |
| 24 | + private MockMvc mockMvc; |
| 25 | + |
| 26 | + @Autowired |
| 27 | + private ContentPolicyRepository contentPolicyRepository; |
| 28 | + |
| 29 | + @Test |
| 30 | + void patchCreatesModerationStateWhenMissing() throws Exception { |
| 31 | + mockMvc.perform(patch("/v1/moderation/content/cnt_1").contentType(MediaType.APPLICATION_JSON) |
| 32 | + .header("X-Request-Id", "req_moderation_1").content(""" |
| 33 | + { |
| 34 | + "moderationState": "HIDDEN" |
| 35 | + } |
| 36 | + """)).andExpect(status().isOk()).andExpect(jsonPath("$.data.contentId").value("cnt_1")) |
| 37 | + .andExpect(jsonPath("$.data.moderationState").value("HIDDEN")) |
| 38 | + .andExpect(jsonPath("$.meta.requestId").value("req_moderation_1")); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + void patchUpdatesExistingModerationState() throws Exception { |
| 43 | + savePolicy("cnt_2", ModerationState.VISIBLE, "TR", "US"); |
| 44 | + |
| 45 | + mockMvc.perform(patch("/v1/moderation/content/cnt_2").contentType(MediaType.APPLICATION_JSON).content(""" |
| 46 | + { |
| 47 | + "moderationState": "REMOVED" |
| 48 | + } |
| 49 | + """)).andExpect(status().isOk()).andExpect(jsonPath("$.data.moderationState").value("REMOVED")) |
| 50 | + .andExpect(jsonPath("$.data.geoAllowList[0]").value("TR")) |
| 51 | + .andExpect(jsonPath("$.data.geoBlockList[0]").value("US")); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void patchRejectsInvalidModerationState() throws Exception { |
| 56 | + mockMvc.perform(patch("/v1/moderation/content/cnt_3").contentType(MediaType.APPLICATION_JSON).content(""" |
| 57 | + { |
| 58 | + "moderationState": "INVALID" |
| 59 | + } |
| 60 | + """)).andExpect(status().isBadRequest()).andExpect(jsonPath("$.error.code").value("VALIDATION_ERROR")); |
| 61 | + } |
| 62 | + |
| 63 | + private void savePolicy(String contentId, ModerationState moderationState, String allowList, String blockList) { |
| 64 | + ContentPolicyEntity entity = new ContentPolicyEntity(); |
| 65 | + entity.setContentId(contentId); |
| 66 | + entity.setGeoAllowList(allowList); |
| 67 | + entity.setGeoBlockList(blockList); |
| 68 | + entity.setModerationState(moderationState); |
| 69 | + contentPolicyRepository.saveAndFlush(entity); |
| 70 | + } |
| 71 | +} |
0 commit comments