content: /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   *
   *    http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package org.apache.dolphinscheduler.api.controller;
  
  import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
  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.request.MockMvcRequestBuilders.put;
  import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
  import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  
  import org.apache.dolphinscheduler.api.enums.Status;
  import org.apache.dolphinscheduler.api.utils.Result;
  import org.apache.dolphinscheduler.common.utils.JSONUtils;
  import org.apache.dolphinscheduler.common.utils.PropertyUtils;
  
  import org.junit.jupiter.api.Assertions;
  import org.junit.jupiter.api.Test;
  import org.mockito.MockedStatic;
  import org.mockito.Mockito;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.http.MediaType;
  import org.springframework.test.web.servlet.MvcResult;
  import org.springframework.util.LinkedMultiValueMap;
  import org.springframework.util.MultiValueMap;
  
  public class TenantControllerTest extends AbstractControllerTest {
  
      private static final Logger logger = LoggerFactory.getLogger(TenantControllerTest.class);
  
      private MockedStatic<PropertyUtils> mockedStaticPropertyUtils;
  
      @Test
      public void testCreateTenant() throws Exception {
          mockedStaticPropertyUtils = Mockito.mockStatic(PropertyUtils.class);
  
          MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
          paramsMap.add("tenantCode", "hayden");
          paramsMap.add("queueId", "1");
          paramsMap.add("description", "tenant description");
  
          MvcResult mvcResult = mockMvc.perform(post("/tenants")
                  .header(SESSION_ID, sessionId)
                  .params(paramsMap))
                  .andExpect(content().contentType(MediaType.APPLICATION_JSON))
                  .andReturn();
  
          Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class);
          Assertions.assertEquals(Status.SUCCESS.getCode(), result.getCode().intValue());
          logger.info(mvcResult.getResponse().getContentAsString());
  
          mockedStaticPropertyUtils.close();
      }
  
      @Test
      public void testQueryTenantListPaging() throws Exception {
          MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
          paramsMap.add("pageNo", "1");
          paramsMap.add("searchVal", "tenant");
          paramsMap.add("pageSize", "30");
  
          MvcResult mvcResult = mockMvc.perform(get("/tenants/")
                  .header(SESSION_ID, sessionId)
                  .params(paramsMap))
                  .andExpect(status().isOk())
                  .andExpect(content().contentType(MediaType.APPLICATION_JSON))
                  .andReturn();
  
          Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class);
          Assertions.assertEquals(Status.SUCCESS.getCode(), result.getCode().intValue());
          logger.info(mvcResult.getResponse().getContentAsString());
      }
  
      @Test
      public void testUpdateTenant() throws Exception {
          MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
          paramsMap.add("id", "9");
          paramsMap.add("tenantCode", "cxc_te");
          paramsMap.add("queueId", "1");
          paramsMap.add("description", "tenant description");
  
          MvcResult mvcResult = mockMvc.perform(put("/tenants/{id}", 9)
                  .header(SESSION_ID, sessionId)
                  .params(paramsMap))
                  .andExpect(status().isOk())
                  .andExpect(content().contentType(MediaType.APPLICATION_JSON))
                  .andReturn();
  
          Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class);
          Assertions.assertEquals(Status.TENANT_NOT_EXIST.getCode(), result.getCode().intValue());
          logger.info(mvcResult.getResponse().getContentAsString());
  
      }
  
      @Test
      public void testVerifyTenantCode() throws Exception {
          MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
          paramsMap.add("tenantCode", "cxc_test");
  
          MvcResult mvcResult = mockMvc.perform(get("/tenants/verify-code")
                  .header(SESSION_ID, sessionId)
                  .params(paramsMap))
                  .andExpect(status().isOk())
                  .andExpect(content().contentType(MediaType.APPLICATION_JSON))
                  .andReturn();
  
          Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class);
          Assertions.assertEquals(Status.SUCCESS.getCode(), result.getCode().intValue());
          logger.info(mvcResult.getResponse().getContentAsString());
  
      }
  
      // @Test
      public void testVerifyTenantCodeExists() throws Exception {
          MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
          paramsMap.add("tenantCode", "hayden");
  
          MvcResult mvcResult = mockMvc.perform(get("/tenants/verify-code")
                  .header(SESSION_ID, sessionId)
                  .params(paramsMap))
                  .andExpect(status().isOk())
                  .andExpect(content().contentType(MediaType.APPLICATION_JSON))
                  .andReturn();
  
          Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class);
          Assertions.assertEquals(Status.OS_TENANT_CODE_EXIST.getCode(), result.getCode().intValue());
          logger.info(mvcResult.getResponse().getContentAsString());
  
      }
  
      @Test
      public void testQueryTenantList() throws Exception {
  
          MvcResult mvcResult = mockMvc.perform(get("/tenants/list")
                  .header(SESSION_ID, sessionId))
                  .andExpect(status().isOk())
                  .andExpect(content().contentType(MediaType.APPLICATION_JSON))
                  .andReturn();
  
          Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class);
          Assertions.assertEquals(Status.SUCCESS.getCode(), result.getCode().intValue());
          logger.info(mvcResult.getResponse().getContentAsString());
          logger.info(mvcResult.getResponse().getContentAsString());
      }
  
      @Test
      public void testDeleteTenantById() throws Exception {
          MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
          paramsMap.add("id", "64");
  
          MvcResult mvcResult = mockMvc.perform(delete("/tenants/{id}", 64)
                  .header(SESSION_ID, sessionId)
                  .params(paramsMap))
                  .andExpect(status().isOk())
                  .andExpect(content().contentType(MediaType.APPLICATION_JSON))
                  .andReturn();
          Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class);
          Assertions.assertEquals(Status.TENANT_NOT_EXIST.getCode(), result.getCode().intValue());
          logger.info(mvcResult.getResponse().getContentAsString());
      }
  }
  