/*
 * Copyright (c) 2016-2016, fastquery.org and/or its affiliates. All rights reserved.
 *
 * 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.
 * 
 * For more information, please see http://www.fastquery.org/.
 * 
 */

package org.fastquery.test;

import org.fastquery.core.RepositoryException;
import org.fastquery.example.UserInfoDBService;
import org.fastquery.example.UserInfoDBServiceProxyImpl;
import org.fastquery.service.FQuery;
import org.junit.Before;
import org.junit.Test;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import static org.junit.Assert.assertThat;

import static org.hamcrest.Matchers.*;

/**
 * 
 * @author xixifeng (fastquery@126.com)
 */
public class UserInfoDBServiceTest {

	private UserInfoDBService userInfoDBService;
	
	@Before
	public void before(){
		userInfoDBService = FQuery.getRepository(UserInfoDBService.class);
	}
	
	@Test
	public void userInfoDBServiceNotNull(){
		assertThat(userInfoDBService, notNullValue());
	}
	
	@Test
	public void testFindUserInfoByAge(){
		int age = 20;
		JSONArray jsonArray = userInfoDBService.findUserInfoByAge(age);
		for (int i = 0; i < jsonArray.size(); i++) {
			JSONObject jsonObject = jsonArray.getJSONObject(i);
			assertThat(jsonObject.getInteger("age"),greaterThan(age));
		}
	}
	
	@Test
	public void testUpdateBatch() {
		new UserInfoDBServiceProxyImpl().updateBatch("小张张", 26, 1);
	}
	
	// 断言: 它会抛出RepositoryException异常
	@Test(expected=RepositoryException.class)
	public void testUpdateBatch2() {
		int effect = userInfoDBService.updateBatch2("小不点", 6, 2);
		// updateBatch2 中途会报错,因此修改影响的行数为0
		assertThat(effect, equalTo(0));
	}
	
	@Test
	public void testUpdateBatch3() {
		int[] effects = userInfoDBService.updateBatch3("白云", 66, 2);
		// updateBatch2 中途会报错,因此修改影响的行数为0
		for (int effect : effects) {
			assertThat(effect, greaterThanOrEqualTo(1));
		}
	}
}

@@ -1085,65 +1085,8 @@
 ce;%0A
-import org.fastquery.example.UserInfoDBServiceProxyImpl;%0A
 impo
@@ -1118,16 +1118,16 @@
 FQuery;%0A
+
 import o
@@ -1586,110 +1586,8 @@
 est%0A
-%09public void userInfoDBServiceNotNull()%7B%0A%09%09assertThat(userInfoDBService, notNullValue());%0A%09%7D%0A%09%0A%09@Test%0A
 %09pub
@@ -1921,13 +1921,22 @@
 %7B%0A%09%09
-new U
+int effect = u
 serI
@@ -1951,19 +1951,8 @@
 vice
-ProxyImpl()
 .upd
@@ -1975,16 +1975,68 @@
 26, 1);%0A
+%09%09assertThat(%22%E6%96%AD%E8%A8%80%E8%AF%A5%E8%A1%8C%E4%BF%AE%E6%94%B9%E6%93%8D%E4%BD%9C%E4%B8%80%E5%85%B1%E5%BD%B1%E5%93%8D%E4%BA%863%E8%A1%8C%22,effect, equalTo(3));%0A
 %09%7D%0A%09%0A%09//

update example