Java Code Examples for org.springframework.test.web.servlet.request.MockMvcRequestBuilders#post()

The following examples show how to use org.springframework.test.web.servlet.request.MockMvcRequestBuilders#post() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RecentlyUsedSearchTest.java    From rebuild with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testAdd() throws Exception {
	MockHttpServletRequestBuilder builder = MockMvcRequestBuilders
			.post("/commons/search/recently-add?id=" + UserService.ADMIN_USER);
	MvcResponse resp = perform(builder, UserService.ADMIN_USER);
	System.out.println(resp);
	Assert.assertTrue(resp.isSuccess());
	
	builder = MockMvcRequestBuilders
			.post("/commons/search/recently-add?id=" + UserService.SYSTEM_USER);
	perform(builder, UserService.ADMIN_USER);
}
 
Example 2
Source File: GeneralOperatingControllTest.java    From rebuild with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void test9Delete() throws Exception {
	MockHttpServletRequestBuilder builder = MockMvcRequestBuilders
			.post("/app/entity/record-delete?id=" + lastSaveId);
	MvcResponse resp = perform(builder, UserService.ADMIN_USER);
	System.out.println(resp);
	Assert.assertTrue(resp.isSuccess());
}
 
Example 3
Source File: GeneralOperatingControllTest.java    From rebuild with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void test7FetchRecordMeta() throws Exception {
	MockHttpServletRequestBuilder builder = MockMvcRequestBuilders
			.post("/app/entity/record-meta?id=" + lastSaveId);
	MvcResponse resp = perform(builder, UserService.ADMIN_USER);
	System.out.println(resp);
	Assert.assertTrue(resp.isSuccess());
}
 
Example 4
Source File: RelatedListControllTest.java    From rebuild with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testrRelatedCounts() throws Exception {
	MockHttpServletRequestBuilder builder = MockMvcRequestBuilders
			.post("/app/entity/related-counts?masterId=" + DepartmentService.ROOT_DEPT + "&relateds=User");
	MvcResponse resp = perform(builder, UserService.ADMIN_USER);
	System.out.println(resp);
	Assert.assertTrue(resp.isSuccess());
}
 
Example 5
Source File: DashboardControllTest.java    From rebuild with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testDashGets() throws Exception {
	MockHttpServletRequestBuilder builder = MockMvcRequestBuilders
			.post("/dashboard/dash-gets");
	MvcResponse resp = perform(builder, UserService.ADMIN_USER);
	System.out.println(resp);
	Assert.assertTrue(resp.isSuccess());
}
 
Example 6
Source File: NotificationControllTest.java    From rebuild with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testCheckMessage() throws Exception {
	MockHttpServletRequestBuilder builder = MockMvcRequestBuilders
			.post("/notification/check-message");
	MvcResponse resp = perform(builder, UserService.ADMIN_USER);
	System.out.println(resp);
	Assert.assertTrue(resp.isSuccess());
}
 
Example 7
Source File: GeneralOperatingControllTest.java    From rebuild with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void test4Share() throws Exception {
	MockHttpServletRequestBuilder builder = MockMvcRequestBuilders
			.post("/app/entity/record-share?id=" + lastSaveId + "&to=" + UserService.SYSTEM_USER);
	MvcResponse resp = perform(builder, UserService.ADMIN_USER);
	System.out.println(resp);
	Assert.assertTrue(resp.isSuccess());
}
 
Example 8
Source File: GeneralOperatingControllTest.java    From rebuild with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void test3Assgin() throws Exception {
	MockHttpServletRequestBuilder builder = MockMvcRequestBuilders
			.post("/app/entity/record-assign?id=" + lastSaveId + "&to=" + UserService.SYSTEM_USER);
	MvcResponse resp = perform(builder, UserService.ADMIN_USER);
	System.out.println(resp);
	Assert.assertTrue(resp.isSuccess());
}
 
Example 9
Source File: UserControllerUnitTest.java    From java-starthere with MIT License 5 votes vote down vote up
@Test
public void postUserRoleByIds() throws Exception
{
    String apiUrl = "/users/user/{userid}/role/{roleid}";

    RequestBuilder rb = MockMvcRequestBuilders.post(apiUrl, 3, 2);

    mockMvc.perform(rb)
           .andExpect(status().is2xxSuccessful())
           .andDo(MockMvcResultHandlers.print());
}
 
Example 10
Source File: GatewayFlowRuleControllerTest.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
@Test
public void testAddFlowRule() throws Exception {
    String path = "/gateway/flow/new.json";

    AddFlowRuleReqVo reqVo = new AddFlowRuleReqVo();
    reqVo.setApp(TEST_APP);
    reqVo.setIp(TEST_IP);
    reqVo.setPort(TEST_PORT);

    reqVo.setResourceMode(RESOURCE_MODE_ROUTE_ID);
    reqVo.setResource("httpbin_route");

    reqVo.setGrade(FLOW_GRADE_QPS);
    reqVo.setCount(5D);
    reqVo.setInterval(30L);
    reqVo.setIntervalUnit(GatewayFlowRuleEntity.INTERVAL_UNIT_SECOND);
    reqVo.setControlBehavior(CONTROL_BEHAVIOR_DEFAULT);
    reqVo.setBurst(0);
    reqVo.setMaxQueueingTimeoutMs(0);

    given(sentinelApiClient.modifyGatewayFlowRules(eq(TEST_APP), eq(TEST_IP), eq(TEST_PORT), any())).willReturn(true);

    MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post(path);
    requestBuilder.content(JSON.toJSONString(reqVo)).contentType(MediaType.APPLICATION_JSON);

    // Do controller logic
    MvcResult mvcResult = mockMvc.perform(requestBuilder)
            .andExpect(MockMvcResultMatchers.status().isOk())
            .andDo(MockMvcResultHandlers.print()).andReturn();

    // Verify the modifyGatewayFlowRules method has been called
    verify(sentinelApiClient).modifyGatewayFlowRules(eq(TEST_APP), eq(TEST_IP), eq(TEST_PORT), any());

    Result<GatewayFlowRuleEntity> result = JSONObject.parseObject(mvcResult.getResponse().getContentAsString(), new TypeReference<Result<GatewayFlowRuleEntity>>() {});
    assertTrue(result.isSuccess());

    // Verify the result
    GatewayFlowRuleEntity entity = result.getData();
    assertNotNull(entity);
    assertEquals(TEST_APP, entity.getApp());
    assertEquals(TEST_IP, entity.getIp());
    assertEquals(TEST_PORT, entity.getPort());
    assertEquals(RESOURCE_MODE_ROUTE_ID, entity.getResourceMode().intValue());
    assertEquals("httpbin_route", entity.getResource());
    assertNotNull(entity.getId());
    assertNotNull(entity.getGmtCreate());
    assertNotNull(entity.getGmtModified());

    // Verify the entity which is add in memory repository
    List<GatewayFlowRuleEntity> entitiesInMem = repository.findAllByApp(TEST_APP);
    assertEquals(1, entitiesInMem.size());
    assertEquals(entity, entitiesInMem.get(0));
}
 
Example 11
Source File: SigninControllTest.java    From rebuild with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void testLogin() throws Exception {
	MockHttpServletRequestBuilder builder = MockMvcRequestBuilders
			.post("/user/user-login?user=admin&passwd=111111");
	System.out.println(perform(builder, null));
}
 
Example 12
Source File: GatewayFlowRuleControllerTest.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
@Test
public void testDeleteFlowRule() throws Exception {
    String path = "/gateway/flow/delete.json";

    // Add one entity into memory repository for delete
    GatewayFlowRuleEntity addEntity = new GatewayFlowRuleEntity();
    addEntity.setId(1L);
    addEntity.setApp(TEST_APP);
    addEntity.setIp(TEST_IP);
    addEntity.setPort(TEST_PORT);
    addEntity.setResource("httpbin_route");
    addEntity.setResourceMode(RESOURCE_MODE_ROUTE_ID);
    addEntity.setGrade(FLOW_GRADE_QPS);
    addEntity.setCount(5D);
    addEntity.setInterval(30L);
    addEntity.setIntervalUnit(GatewayFlowRuleEntity.INTERVAL_UNIT_SECOND);
    addEntity.setControlBehavior(CONTROL_BEHAVIOR_DEFAULT);
    addEntity.setBurst(0);
    addEntity.setMaxQueueingTimeoutMs(0);
    Date date = new Date();
    date = DateUtils.addSeconds(date, -1);
    addEntity.setGmtCreate(date);
    addEntity.setGmtModified(date);

    GatewayParamFlowItemEntity addItemEntity = new GatewayParamFlowItemEntity();
    addEntity.setParamItem(addItemEntity);
    addItemEntity.setParseStrategy(PARAM_PARSE_STRATEGY_CLIENT_IP);

    repository.save(addEntity);

    given(sentinelApiClient.modifyGatewayFlowRules(eq(TEST_APP), eq(TEST_IP), eq(TEST_PORT), any())).willReturn(true);

    MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post(path);
    requestBuilder.param("id", String.valueOf(addEntity.getId()));

    // Do controller logic
    MvcResult mvcResult = mockMvc.perform(requestBuilder)
            .andExpect(MockMvcResultMatchers.status().isOk()).andDo(MockMvcResultHandlers.print()).andReturn();

    // Verify the modifyGatewayFlowRules method has been called
    verify(sentinelApiClient).modifyGatewayFlowRules(eq(TEST_APP), eq(TEST_IP), eq(TEST_PORT), any());

    // Verify the result
    Result<Long> result = JSONObject.parseObject(mvcResult.getResponse().getContentAsString(), new TypeReference<Result<Long>>() {});
    assertTrue(result.isSuccess());

    assertEquals(addEntity.getId(), result.getData());

    // Now no entities in memory
    List<GatewayFlowRuleEntity> entitiesInMem = repository.findAllByApp(TEST_APP);
    assertEquals(0, entitiesInMem.size());
}
 
Example 13
Source File: GatewayFlowRuleControllerTest.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
@Test
public void testUpdateFlowRule() throws Exception {
    String path = "/gateway/flow/save.json";

    // Add one entity into memory repository for update
    GatewayFlowRuleEntity addEntity = new GatewayFlowRuleEntity();
    addEntity.setId(1L);
    addEntity.setApp(TEST_APP);
    addEntity.setIp(TEST_IP);
    addEntity.setPort(TEST_PORT);
    addEntity.setResource("httpbin_route");
    addEntity.setResourceMode(RESOURCE_MODE_ROUTE_ID);
    addEntity.setGrade(FLOW_GRADE_QPS);
    addEntity.setCount(5D);
    addEntity.setInterval(30L);
    addEntity.setIntervalUnit(GatewayFlowRuleEntity.INTERVAL_UNIT_SECOND);
    addEntity.setControlBehavior(CONTROL_BEHAVIOR_DEFAULT);
    addEntity.setBurst(0);
    addEntity.setMaxQueueingTimeoutMs(0);
    Date date = new Date();
    // To make the gmtModified different when do update
    date = DateUtils.addSeconds(date, -1);
    addEntity.setGmtCreate(date);
    addEntity.setGmtModified(date);

    GatewayParamFlowItemEntity addItemEntity = new GatewayParamFlowItemEntity();
    addEntity.setParamItem(addItemEntity);
    addItemEntity.setParseStrategy(PARAM_PARSE_STRATEGY_CLIENT_IP);

    repository.save(addEntity);

    UpdateFlowRuleReqVo reqVo = new UpdateFlowRuleReqVo();
    reqVo.setId(addEntity.getId());
    reqVo.setApp(TEST_APP);
    reqVo.setGrade(FLOW_GRADE_QPS);
    reqVo.setCount(6D);
    reqVo.setInterval(2L);
    reqVo.setIntervalUnit(GatewayFlowRuleEntity.INTERVAL_UNIT_MINUTE);
    reqVo.setControlBehavior(CONTROL_BEHAVIOR_RATE_LIMITER);
    reqVo.setMaxQueueingTimeoutMs(500);

    GatewayParamFlowItemVo itemVo = new GatewayParamFlowItemVo();
    reqVo.setParamItem(itemVo);
    itemVo.setParseStrategy(PARAM_PARSE_STRATEGY_URL_PARAM);
    itemVo.setFieldName("pa");

    given(sentinelApiClient.modifyGatewayFlowRules(eq(TEST_APP), eq(TEST_IP), eq(TEST_PORT), any())).willReturn(true);

    MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post(path);
    requestBuilder.content(JSON.toJSONString(reqVo)).contentType(MediaType.APPLICATION_JSON);

    // Do controller logic
    MvcResult mvcResult = mockMvc.perform(requestBuilder)
            .andExpect(MockMvcResultMatchers.status().isOk())
            .andDo(MockMvcResultHandlers.print()).andReturn();

    // Verify the modifyGatewayFlowRules method has been called
    verify(sentinelApiClient).modifyGatewayFlowRules(eq(TEST_APP), eq(TEST_IP), eq(TEST_PORT), any());

    Result<GatewayFlowRuleEntity> result = JSONObject.parseObject(mvcResult.getResponse().getContentAsString(), new TypeReference<Result<GatewayFlowRuleEntity>>() {
    });
    assertTrue(result.isSuccess());

    GatewayFlowRuleEntity entity = result.getData();
    assertNotNull(entity);
    assertEquals(RESOURCE_MODE_ROUTE_ID, entity.getResourceMode().intValue());
    assertEquals("httpbin_route", entity.getResource());
    assertEquals(6D, entity.getCount().doubleValue(), 0);
    assertEquals(2L, entity.getInterval().longValue());
    assertEquals(GatewayFlowRuleEntity.INTERVAL_UNIT_MINUTE, entity.getIntervalUnit().intValue());
    assertEquals(CONTROL_BEHAVIOR_RATE_LIMITER, entity.getControlBehavior().intValue());
    assertEquals(0, entity.getBurst().intValue());
    assertEquals(500, entity.getMaxQueueingTimeoutMs().intValue());
    assertEquals(date, entity.getGmtCreate());
    // To make sure gmtModified has been set and it's different from gmtCreate
    assertNotNull(entity.getGmtModified());
    assertNotEquals(entity.getGmtCreate(), entity.getGmtModified());

    // Verify the entity which is update in memory repository
    GatewayParamFlowItemEntity itemEntity = entity.getParamItem();
    assertEquals(PARAM_PARSE_STRATEGY_URL_PARAM, itemEntity.getParseStrategy().intValue());
    assertEquals("pa", itemEntity.getFieldName());
}
 
Example 14
Source File: UserAvatarTest.java    From rebuild with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void testGetAvatar() throws Exception {
	MockHttpServletRequestBuilder builder = MockMvcRequestBuilders
			.post("/account/user-avatar/" + SIMPLE_USER);
	System.out.println(perform(builder, SIMPLE_USER));
}
 
Example 15
Source File: GatewayApiControllerTest.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
@Test
public void testUpdateApi() throws Exception {
    String path = "/gateway/api/save.json";

    // Add one entity to memory repository for update
    ApiDefinitionEntity addEntity = new ApiDefinitionEntity();
    addEntity.setApp(TEST_APP);
    addEntity.setIp(TEST_IP);
    addEntity.setPort(TEST_PORT);
    addEntity.setApiName("bbb");
    Date date = new Date();
    // To make the gmtModified different when do update
    date = DateUtils.addSeconds(date, -1);
    addEntity.setGmtCreate(date);
    addEntity.setGmtModified(date);
    Set<ApiPredicateItemEntity> addRedicateItemEntities = new HashSet<>();
    addEntity.setPredicateItems(addRedicateItemEntities);
    ApiPredicateItemEntity addPredicateItemEntity = new ApiPredicateItemEntity();
    addPredicateItemEntity.setMatchStrategy(URL_MATCH_STRATEGY_EXACT);
    addPredicateItemEntity.setPattern("/order");
    addEntity = repository.save(addEntity);

    UpdateApiReqVo reqVo = new UpdateApiReqVo();
    reqVo.setId(addEntity.getId());
    reqVo.setApp(TEST_APP);
    List<ApiPredicateItemVo> itemVos = new ArrayList<>();
    ApiPredicateItemVo itemVo = new ApiPredicateItemVo();
    itemVo.setMatchStrategy(URL_MATCH_STRATEGY_PREFIX);
    itemVo.setPattern("/my_order");
    itemVos.add(itemVo);
    reqVo.setPredicateItems(itemVos);

    given(sentinelApiClient.modifyApis(eq(TEST_APP), eq(TEST_IP), eq(TEST_PORT), any())).willReturn(true);

    MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post(path);
    requestBuilder.content(JSON.toJSONString(reqVo)).contentType(MediaType.APPLICATION_JSON);

    // Do controller logic
    MvcResult mvcResult = mockMvc.perform(requestBuilder)
            .andExpect(MockMvcResultMatchers.status().isOk())
            .andDo(MockMvcResultHandlers.print()).andReturn();

    // Verify the modifyApis method has been called
    verify(sentinelApiClient).modifyApis(eq(TEST_APP), eq(TEST_IP), eq(TEST_PORT), any());

    Result<ApiDefinitionEntity> result = JSONObject.parseObject(mvcResult.getResponse().getContentAsString(), new TypeReference<Result<ApiDefinitionEntity>>() {});
    assertTrue(result.isSuccess());

    ApiDefinitionEntity entity = result.getData();
    assertNotNull(entity);
    assertEquals("bbb", entity.getApiName());
    assertEquals(date, entity.getGmtCreate());
    // To make sure gmtModified has been set and it's different from gmtCreate
    assertNotNull(entity.getGmtModified());
    assertNotEquals(entity.getGmtCreate(), entity.getGmtModified());

    Set<ApiPredicateItemEntity> predicateItemEntities = entity.getPredicateItems();
    assertEquals(1, predicateItemEntities.size());
    ApiPredicateItemEntity predicateItemEntity = predicateItemEntities.iterator().next();
    assertEquals(URL_MATCH_STRATEGY_PREFIX, predicateItemEntity.getMatchStrategy().intValue());
    assertEquals("/my_order", predicateItemEntity.getPattern());

    // Verify the entity which is update in memory repository
    List<ApiDefinitionEntity> entitiesInMem = repository.findAllByApp(TEST_APP);
    assertEquals(1, entitiesInMem.size());
    assertEquals(entity, entitiesInMem.get(0));
}
 
Example 16
Source File: GatewayApiControllerTest.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
@Test
public void testAddApi() throws Exception {
    String path = "/gateway/api/new.json";

    AddApiReqVo reqVo = new AddApiReqVo();
    reqVo.setApp(TEST_APP);
    reqVo.setIp(TEST_IP);
    reqVo.setPort(TEST_PORT);

    reqVo.setApiName("customized_api");

    List<ApiPredicateItemVo> itemVos = new ArrayList<>();
    ApiPredicateItemVo itemVo = new ApiPredicateItemVo();
    itemVo.setMatchStrategy(URL_MATCH_STRATEGY_EXACT);
    itemVo.setPattern("/product");
    itemVos.add(itemVo);
    reqVo.setPredicateItems(itemVos);

    given(sentinelApiClient.modifyApis(eq(TEST_APP), eq(TEST_IP), eq(TEST_PORT), any())).willReturn(true);

    MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post(path);
    requestBuilder.content(JSON.toJSONString(reqVo)).contentType(MediaType.APPLICATION_JSON);

    // Do controller logic
    MvcResult mvcResult = mockMvc.perform(requestBuilder)
        .andExpect(MockMvcResultMatchers.status().isOk())
        .andDo(MockMvcResultHandlers.print()).andReturn();

    // Verify the modifyApis method has been called
    verify(sentinelApiClient).modifyApis(eq(TEST_APP), eq(TEST_IP), eq(TEST_PORT), any());

    Result<ApiDefinitionEntity> result = JSONObject.parseObject(mvcResult.getResponse().getContentAsString(), new TypeReference<Result<ApiDefinitionEntity>>() {});
    assertTrue(result.isSuccess());

    // Verify the result
    ApiDefinitionEntity entity = result.getData();
    assertNotNull(entity);
    assertEquals(TEST_APP, entity.getApp());
    assertEquals(TEST_IP, entity.getIp());
    assertEquals(TEST_PORT, entity.getPort());
    assertEquals("customized_api", entity.getApiName());
    assertNotNull(entity.getId());
    assertNotNull(entity.getGmtCreate());
    assertNotNull(entity.getGmtModified());

    Set<ApiPredicateItemEntity> predicateItemEntities = entity.getPredicateItems();
    assertEquals(1, predicateItemEntities.size());
    ApiPredicateItemEntity predicateItemEntity = predicateItemEntities.iterator().next();
    assertEquals(URL_MATCH_STRATEGY_EXACT, predicateItemEntity.getMatchStrategy().intValue());
    assertEquals("/product", predicateItemEntity.getPattern());

    // Verify the entity which is add in memory repository
    List<ApiDefinitionEntity> entitiesInMem = repository.findAllByApp(TEST_APP);
    assertEquals(1, entitiesInMem.size());
    assertEquals(entity, entitiesInMem.get(0));
}
 
Example 17
Source File: GatewayFlowRuleControllerTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
@Test
public void testUpdateFlowRule() throws Exception {
    String path = "/gateway/flow/save.json";

    // Add one entity into memory repository for update
    GatewayFlowRuleEntity addEntity = new GatewayFlowRuleEntity();
    addEntity.setId(1L);
    addEntity.setApp(TEST_APP);
    addEntity.setIp(TEST_IP);
    addEntity.setPort(TEST_PORT);
    addEntity.setResource("httpbin_route");
    addEntity.setResourceMode(RESOURCE_MODE_ROUTE_ID);
    addEntity.setGrade(FLOW_GRADE_QPS);
    addEntity.setCount(5D);
    addEntity.setInterval(30L);
    addEntity.setIntervalUnit(GatewayFlowRuleEntity.INTERVAL_UNIT_SECOND);
    addEntity.setControlBehavior(CONTROL_BEHAVIOR_DEFAULT);
    addEntity.setBurst(0);
    addEntity.setMaxQueueingTimeoutMs(0);
    Date date = new Date();
    // To make the gmtModified different when do update
    date = DateUtils.addSeconds(date, -1);
    addEntity.setGmtCreate(date);
    addEntity.setGmtModified(date);

    GatewayParamFlowItemEntity addItemEntity = new GatewayParamFlowItemEntity();
    addEntity.setParamItem(addItemEntity);
    addItemEntity.setParseStrategy(PARAM_PARSE_STRATEGY_CLIENT_IP);

    repository.save(addEntity);

    UpdateFlowRuleReqVo reqVo = new UpdateFlowRuleReqVo();
    reqVo.setId(addEntity.getId());
    reqVo.setApp(TEST_APP);
    reqVo.setGrade(FLOW_GRADE_QPS);
    reqVo.setCount(6D);
    reqVo.setInterval(2L);
    reqVo.setIntervalUnit(GatewayFlowRuleEntity.INTERVAL_UNIT_MINUTE);
    reqVo.setControlBehavior(CONTROL_BEHAVIOR_RATE_LIMITER);
    reqVo.setMaxQueueingTimeoutMs(500);

    GatewayParamFlowItemVo itemVo = new GatewayParamFlowItemVo();
    reqVo.setParamItem(itemVo);
    itemVo.setParseStrategy(PARAM_PARSE_STRATEGY_URL_PARAM);
    itemVo.setFieldName("pa");

    given(sentinelApiClient.modifyGatewayFlowRules(eq(TEST_APP), eq(TEST_IP), eq(TEST_PORT), any())).willReturn(true);

    MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post(path);
    requestBuilder.content(JSON.toJSONString(reqVo)).contentType(MediaType.APPLICATION_JSON);

    // Do controller logic
    MvcResult mvcResult = mockMvc.perform(requestBuilder)
            .andExpect(MockMvcResultMatchers.status().isOk())
            .andDo(MockMvcResultHandlers.print()).andReturn();

    // Verify the modifyGatewayFlowRules method has been called
    verify(sentinelApiClient).modifyGatewayFlowRules(eq(TEST_APP), eq(TEST_IP), eq(TEST_PORT), any());

    Result<GatewayFlowRuleEntity> result = JSONObject.parseObject(mvcResult.getResponse().getContentAsString(), new TypeReference<Result<GatewayFlowRuleEntity>>() {
    });
    assertTrue(result.isSuccess());

    GatewayFlowRuleEntity entity = result.getData();
    assertNotNull(entity);
    assertEquals(RESOURCE_MODE_ROUTE_ID, entity.getResourceMode().intValue());
    assertEquals("httpbin_route", entity.getResource());
    assertEquals(6D, entity.getCount().doubleValue(), 0);
    assertEquals(2L, entity.getInterval().longValue());
    assertEquals(GatewayFlowRuleEntity.INTERVAL_UNIT_MINUTE, entity.getIntervalUnit().intValue());
    assertEquals(CONTROL_BEHAVIOR_RATE_LIMITER, entity.getControlBehavior().intValue());
    assertEquals(0, entity.getBurst().intValue());
    assertEquals(500, entity.getMaxQueueingTimeoutMs().intValue());
    assertEquals(date, entity.getGmtCreate());
    // To make sure gmtModified has been set and it's different from gmtCreate
    assertNotNull(entity.getGmtModified());
    assertNotEquals(entity.getGmtCreate(), entity.getGmtModified());

    // Verify the entity which is update in memory repository
    GatewayParamFlowItemEntity itemEntity = entity.getParamItem();
    assertEquals(PARAM_PARSE_STRATEGY_URL_PARAM, itemEntity.getParseStrategy().intValue());
    assertEquals("pa", itemEntity.getFieldName());
}
 
Example 18
Source File: GatewayApiControllerTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
@Test
public void testDeleteApi() throws Exception {
    String path = "/gateway/api/delete.json";

    // Add one entity into memory repository for delete
    ApiDefinitionEntity addEntity = new ApiDefinitionEntity();
    addEntity.setApp(TEST_APP);
    addEntity.setIp(TEST_IP);
    addEntity.setPort(TEST_PORT);
    addEntity.setApiName("ccc");
    Date date = new Date();
    addEntity.setGmtCreate(date);
    addEntity.setGmtModified(date);
    Set<ApiPredicateItemEntity> addRedicateItemEntities = new HashSet<>();
    addEntity.setPredicateItems(addRedicateItemEntities);
    ApiPredicateItemEntity addPredicateItemEntity = new ApiPredicateItemEntity();
    addPredicateItemEntity.setMatchStrategy(URL_MATCH_STRATEGY_EXACT);
    addPredicateItemEntity.setPattern("/user/add");
    addEntity = repository.save(addEntity);

    given(sentinelApiClient.modifyApis(eq(TEST_APP), eq(TEST_IP), eq(TEST_PORT), any())).willReturn(true);

    MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post(path);
    requestBuilder.param("id", String.valueOf(addEntity.getId()));

    // Do controller logic
    MvcResult mvcResult = mockMvc.perform(requestBuilder)
            .andExpect(MockMvcResultMatchers.status().isOk()).andDo(MockMvcResultHandlers.print()).andReturn();

    // Verify the modifyApis method has been called
    verify(sentinelApiClient).modifyApis(eq(TEST_APP), eq(TEST_IP), eq(TEST_PORT), any());

    // Verify the result
    Result<Long> result = JSONObject.parseObject(mvcResult.getResponse().getContentAsString(), new TypeReference<Result<Long>>() {});
    assertTrue(result.isSuccess());

    assertEquals(addEntity.getId(), result.getData());

    // Now no entities in memory
    List<ApiDefinitionEntity> entitiesInMem = repository.findAllByApp(TEST_APP);
    assertEquals(0, entitiesInMem.size());
}
 
Example 19
Source File: GatewayApiControllerTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
@Test
public void testUpdateApi() throws Exception {
    String path = "/gateway/api/save.json";

    // Add one entity to memory repository for update
    ApiDefinitionEntity addEntity = new ApiDefinitionEntity();
    addEntity.setApp(TEST_APP);
    addEntity.setIp(TEST_IP);
    addEntity.setPort(TEST_PORT);
    addEntity.setApiName("bbb");
    Date date = new Date();
    // To make the gmtModified different when do update
    date = DateUtils.addSeconds(date, -1);
    addEntity.setGmtCreate(date);
    addEntity.setGmtModified(date);
    Set<ApiPredicateItemEntity> addRedicateItemEntities = new HashSet<>();
    addEntity.setPredicateItems(addRedicateItemEntities);
    ApiPredicateItemEntity addPredicateItemEntity = new ApiPredicateItemEntity();
    addPredicateItemEntity.setMatchStrategy(URL_MATCH_STRATEGY_EXACT);
    addPredicateItemEntity.setPattern("/order");
    addEntity = repository.save(addEntity);

    UpdateApiReqVo reqVo = new UpdateApiReqVo();
    reqVo.setId(addEntity.getId());
    reqVo.setApp(TEST_APP);
    List<ApiPredicateItemVo> itemVos = new ArrayList<>();
    ApiPredicateItemVo itemVo = new ApiPredicateItemVo();
    itemVo.setMatchStrategy(URL_MATCH_STRATEGY_PREFIX);
    itemVo.setPattern("/my_order");
    itemVos.add(itemVo);
    reqVo.setPredicateItems(itemVos);

    given(sentinelApiClient.modifyApis(eq(TEST_APP), eq(TEST_IP), eq(TEST_PORT), any())).willReturn(true);

    MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post(path);
    requestBuilder.content(JSON.toJSONString(reqVo)).contentType(MediaType.APPLICATION_JSON);

    // Do controller logic
    MvcResult mvcResult = mockMvc.perform(requestBuilder)
            .andExpect(MockMvcResultMatchers.status().isOk())
            .andDo(MockMvcResultHandlers.print()).andReturn();

    // Verify the modifyApis method has been called
    verify(sentinelApiClient).modifyApis(eq(TEST_APP), eq(TEST_IP), eq(TEST_PORT), any());

    Result<ApiDefinitionEntity> result = JSONObject.parseObject(mvcResult.getResponse().getContentAsString(), new TypeReference<Result<ApiDefinitionEntity>>() {});
    assertTrue(result.isSuccess());

    ApiDefinitionEntity entity = result.getData();
    assertNotNull(entity);
    assertEquals("bbb", entity.getApiName());
    assertEquals(date, entity.getGmtCreate());
    // To make sure gmtModified has been set and it's different from gmtCreate
    assertNotNull(entity.getGmtModified());
    assertNotEquals(entity.getGmtCreate(), entity.getGmtModified());

    Set<ApiPredicateItemEntity> predicateItemEntities = entity.getPredicateItems();
    assertEquals(1, predicateItemEntities.size());
    ApiPredicateItemEntity predicateItemEntity = predicateItemEntities.iterator().next();
    assertEquals(URL_MATCH_STRATEGY_PREFIX, predicateItemEntity.getMatchStrategy().intValue());
    assertEquals("/my_order", predicateItemEntity.getPattern());

    // Verify the entity which is update in memory repository
    List<ApiDefinitionEntity> entitiesInMem = repository.findAllByApp(TEST_APP);
    assertEquals(1, entitiesInMem.size());
    assertEquals(entity, entitiesInMem.get(0));
}
 
Example 20
Source File: GatewayApiControllerTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
@Test
public void testAddApi() throws Exception {
    String path = "/gateway/api/new.json";

    AddApiReqVo reqVo = new AddApiReqVo();
    reqVo.setApp(TEST_APP);
    reqVo.setIp(TEST_IP);
    reqVo.setPort(TEST_PORT);

    reqVo.setApiName("customized_api");

    List<ApiPredicateItemVo> itemVos = new ArrayList<>();
    ApiPredicateItemVo itemVo = new ApiPredicateItemVo();
    itemVo.setMatchStrategy(URL_MATCH_STRATEGY_EXACT);
    itemVo.setPattern("/product");
    itemVos.add(itemVo);
    reqVo.setPredicateItems(itemVos);

    given(sentinelApiClient.modifyApis(eq(TEST_APP), eq(TEST_IP), eq(TEST_PORT), any())).willReturn(true);

    MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post(path);
    requestBuilder.content(JSON.toJSONString(reqVo)).contentType(MediaType.APPLICATION_JSON);

    // Do controller logic
    MvcResult mvcResult = mockMvc.perform(requestBuilder)
        .andExpect(MockMvcResultMatchers.status().isOk())
        .andDo(MockMvcResultHandlers.print()).andReturn();

    // Verify the modifyApis method has been called
    verify(sentinelApiClient).modifyApis(eq(TEST_APP), eq(TEST_IP), eq(TEST_PORT), any());

    Result<ApiDefinitionEntity> result = JSONObject.parseObject(mvcResult.getResponse().getContentAsString(), new TypeReference<Result<ApiDefinitionEntity>>() {});
    assertTrue(result.isSuccess());

    // Verify the result
    ApiDefinitionEntity entity = result.getData();
    assertNotNull(entity);
    assertEquals(TEST_APP, entity.getApp());
    assertEquals(TEST_IP, entity.getIp());
    assertEquals(TEST_PORT, entity.getPort());
    assertEquals("customized_api", entity.getApiName());
    assertNotNull(entity.getId());
    assertNotNull(entity.getGmtCreate());
    assertNotNull(entity.getGmtModified());

    Set<ApiPredicateItemEntity> predicateItemEntities = entity.getPredicateItems();
    assertEquals(1, predicateItemEntities.size());
    ApiPredicateItemEntity predicateItemEntity = predicateItemEntities.iterator().next();
    assertEquals(URL_MATCH_STRATEGY_EXACT, predicateItemEntity.getMatchStrategy().intValue());
    assertEquals("/product", predicateItemEntity.getPattern());

    // Verify the entity which is add in memory repository
    List<ApiDefinitionEntity> entitiesInMem = repository.findAllByApp(TEST_APP);
    assertEquals(1, entitiesInMem.size());
    assertEquals(entity, entitiesInMem.get(0));
}