Skip to content

Commit

Permalink
Merge pull request #1206 from WeBankPartners/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
kanetz authored Jan 12, 2021
2 parents 580b6fd + 2316ab1 commit d491190
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 30 deletions.
19 changes: 16 additions & 3 deletions cmdb-core/src/main/java/com/webank/cmdb/dto/QueryRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class QueryRequest {
protected List<Filter> filters = new LinkedList<>();
protected String filterRs = FilterRelationship.And.getCode();

protected Sorting sorting = new Sorting();
protected List<Sorting> sortings = new LinkedList<>();
protected List<String> groupBys = new LinkedList<>();
protected List<String> refResources = new LinkedList<>();
Expand Down Expand Up @@ -75,18 +76,30 @@ public void setFilters(List<Filter> filters) {
this.filters = filters;
}

public boolean isSortingRequested() {
return this.sortings.isEmpty();
public Sorting getSorting() {
return sorting;
}

public void setSorting(Sorting sorting) {
this.sorting = sorting;
}

public List<Sorting> getSortings() {
return sortings;
if(!this.sortings.isEmpty()) return this.sortings;

if(!StringUtils.isBlank(this.sorting.getField())) return Collections.singletonList(this.sorting);

return Collections.emptyList();
}

public void setSortings(List<Sorting> sortings) {
this.sortings = sortings;
}

public boolean isSortingRequested() {
return !this.getSortings().isEmpty();
}

public boolean isPaging() {
return paging;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,5 @@
package com.webank.cmdb.controller.apiv2;

import static com.webank.cmdb.controller.QueryRequestUtils.defaultQueryObject;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.isEmptyOrNullString;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static com.webank.cmdb.util.SpecialSymbolUtils.getAfterSpecialSymbol;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.transaction.Transactional;

import org.junit.Ignore;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MvcResult;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableList;
Expand All @@ -37,6 +11,23 @@
import com.webank.cmdb.controller.QueryRequestUtils;
import com.webank.cmdb.dto.AutoFillItem;
import com.webank.cmdb.util.JsonUtil;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MvcResult;

import javax.persistence.EntityManager;
import javax.transaction.Transactional;
import java.util.*;

import static com.webank.cmdb.controller.QueryRequestUtils.defaultQueryObject;
import static com.webank.cmdb.util.SpecialSymbolUtils.getAfterSpecialSymbol;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;

public class ApiV2ControllerCiDataTest extends LegacyAbstractBaseControllerTest {

Expand Down Expand Up @@ -170,6 +161,36 @@ public void queryCiDataByPagingThenReturnData() throws Exception {
assertThat(total, greaterThanOrEqualTo(11));
}

@Test
public void queryCiDataWithSortingThenReturnData() throws Exception {
Map<String, Object> sortingMap = ImmutableMap.of("field", "name_en", "asc", true);
Map<String, Object> requestMap = ImmutableMap.of("sorting", sortingMap);
String reqJson = new ObjectMapper().writeValueAsString(requestMap);

mvc.perform(post("/api/v2/ci/{ciTypeId}/retrieve", ciTypeId).contentType(MediaType.APPLICATION_JSON)
.content(reqJson))
.andExpect(jsonPath("$.statusCode", is("OK")))
.andExpect(jsonPath("$.data.contents.length()", is(12)))
.andExpect(jsonPath("$.data.contents[0].data.name_en", is("Deposite system")))
.andExpect(jsonPath("$.data.contents[11].data.name_en", is("Subsystem9")))
;
}

@Test
public void queryCiDataWithMultipleSortingThenReturnData() throws Exception {
Map<String, Object> sortingMap = ImmutableMap.of("field", "name_en", "asc", true);
Map<String, Object> requestMap = ImmutableMap.of("sortings", Collections.singletonList(sortingMap));
String reqJson = new ObjectMapper().writeValueAsString(requestMap);

mvc.perform(post("/api/v2/ci/{ciTypeId}/retrieve", ciTypeId).contentType(MediaType.APPLICATION_JSON)
.content(reqJson))
.andExpect(jsonPath("$.statusCode", is("OK")))
.andExpect(jsonPath("$.data.contents.length()", is(12)))
.andExpect(jsonPath("$.data.contents[0].data.name_en", is("Deposite system")))
.andExpect(jsonPath("$.data.contents[11].data.name_en", is("Subsystem9")))
;
}

@Test
public void updateCiDataThenReturnData() throws Exception {
Map<?, ?> jsonMap = ImmutableMap.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;

import java.util.Arrays;
import java.util.Collections;
import java.util.Map;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
Expand All @@ -25,6 +28,8 @@
import com.webank.cmdb.controller.AbstractBaseControllerTest;
import com.webank.cmdb.util.JsonUtil;

import javax.transaction.Transactional;

@WithMockUser(username = "test", authorities = { ROLE_PREFIX + MENU_DESIGNING_CI_DATA_MANAGEMENT ,ROLE_PREFIX + MENU_APPLICATION_DEPLOYMENT_DESIGN })
public class UICiDataManagementControllerTest extends AbstractBaseControllerTest {

Expand Down Expand Up @@ -119,5 +124,66 @@ public void physicalDeploymentDesignQuery() throws Exception {
.contentType(MediaType.APPLICATION_JSON)
.content("{}"))
.andExpect(jsonPath("$.statusCode", is("OK")));
}
}

@Transactional
@Test
public void queryCiDataWithDefaultSortingThenReturnData() throws Exception {
int ciTypeId = givenAppliedCiType();
givenCiData(ciTypeId, ImmutableMap.of("code", "code1"));
givenCiData(ciTypeId, ImmutableMap.of("code", "code2"));

mvc.perform(post("/ui/v2/ci-types/{ci-type-id}/ci-data/query", ciTypeId).contentType(MediaType.APPLICATION_JSON)
.content("{}"))
.andExpect(jsonPath("$.statusCode", is("OK")))
.andExpect(jsonPath("$.data.contents.length()", is(2)))
.andExpect(jsonPath("$.data.contents[0].data.code", is("code2")))
.andExpect(jsonPath("$.data.contents[1].data.code", is("code1")))
;
}

@Transactional
@Test
public void queryCiDataWithSingleSortingThenReturnData() throws Exception {
int ciTypeId = givenAppliedCiType();
givenCiData(ciTypeId, ImmutableMap.of("code", "code1"));
givenCiData(ciTypeId, ImmutableMap.of("code", "code2"));

Map<String, Object> sortingMap = ImmutableMap.of("field", "code", "asc", true);
Map<String, Object> requestMap = ImmutableMap.of("sorting", sortingMap);
String reqJson = new ObjectMapper().writeValueAsString(requestMap);

mvc.perform(post("/ui/v2/ci-types/{ci-type-id}/ci-data/query", ciTypeId).contentType(MediaType.APPLICATION_JSON)
.content(reqJson))
.andExpect(jsonPath("$.statusCode", is("OK")))
.andExpect(jsonPath("$.data.contents.length()", is(2)))
.andExpect(jsonPath("$.data.contents[0].data.code", is("code1")))
.andExpect(jsonPath("$.data.contents[1].data.code", is("code2")))
;
}

@Transactional
@Test
public void queryCiDataWithMultipleSortingsThenReturnData() throws Exception {
int ciTypeId = givenAppliedCiType();
givenCiData(ciTypeId, ImmutableMap.of("code", "code1", "description", "description"));
givenCiData(ciTypeId, ImmutableMap.of("code", "code2", "description", "description"));

Map<String, Object> requestMap = ImmutableMap.of("sortings", Arrays.asList(
ImmutableMap.of("field", "description", "asc", false),
ImmutableMap.of("field", "code", "asc", true)
));
String reqJson = new ObjectMapper().writeValueAsString(requestMap);

mvc.perform(post("/ui/v2/ci-types/{ci-type-id}/ci-data/query", ciTypeId).contentType(MediaType.APPLICATION_JSON)
.content(reqJson))
.andExpect(jsonPath("$.statusCode", is("OK")))
.andExpect(jsonPath("$.data.contents.length()", is(2)))
.andExpect(jsonPath("$.data.contents[0].data.description", is("description")))
.andExpect(jsonPath("$.data.contents[0].data.code", is("code1")))
.andExpect(jsonPath("$.data.contents[1].data.description", is("description")))
.andExpect(jsonPath("$.data.contents[1].data.code", is("code2")))
;
}

}

0 comments on commit d491190

Please sign in to comment.