/**
* personium.io
* Copyright 2014 FUJITSU LIMITED
*
* Licensed 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 com.fujitsu.dc.test.jersey.bar;
import static org.junit.Assert.assertEquals;
import java.io.File;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.ws.rs.core.MediaType;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpStatus;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import com.fujitsu.dc.core.DcCoreConfig;
import com.fujitsu.dc.core.DcCoreConfig.OData;
import com.fujitsu.dc.core.model.progress.ProgressInfo;
import com.fujitsu.dc.test.categories.Unit;
import com.fujitsu.dc.test.jersey.DcRunner;
import com.fujitsu.dc.test.jersey.ODataCommon;
import com.fujitsu.dc.test.setup.Setup;
import com.fujitsu.dc.test.unit.core.UrlUtils;
import com.fujitsu.dc.test.utils.CellUtils;
import com.fujitsu.dc.test.utils.Http;
import com.fujitsu.dc.test.utils.TResponse;
import com.fujitsu.dc.test.utils.UserDataUtils;
import com.sun.jersey.test.framework.JerseyTest;
import com.sun.jersey.test.framework.WebAppDescriptor;
/**
* ユーザデータの$links制限値チェック向けのbarファイルインストール用テスト.
*/
@RunWith(DcRunner.class)
@Category({Unit.class })
public class BarInstallUserDataLinksLimitTest extends JerseyTest {
private static final Map<String, String> INIT_PARAMS = new HashMap<String, String>();
static {
INIT_PARAMS.put("com.sun.jersey.config.property.packages", "com.fujitsu.dc.core.rs");
INIT_PARAMS.put("com.sun.jersey.spi.container.ContainerRequestFilters",
"com.fujitsu.dc.core.jersey.filter.DcCoreContainerFilter");
INIT_PARAMS.put("com.sun.jersey.spi.container.ContainerResponseFilters",
"com.fujitsu.dc.core.jersey.filter.DcCoreContainerFilter");
}
private static final String INSTALL_TARGET = "installBox";
private static final String REQ_CONTENT_TYPE = "application/zip";
private static final String REQUEST_NORM_FILE = "bar-install.txt";
private static final String RESOURCE_PATH = "requestData/barInstall";
private static final String DEFAULT_SCHEMA_URL = "https://fqdn/testcell1/";
private static final String MASTER_TOKEN_NAME = Setup.MASTER_TOKEN_NAME;
private static int linkNtoNMaxSize = 40;
private static final int DEFAULT_LINKS_NTON_MAX_SIZE = DcCoreConfig.getLinksNtoNMaxSize();
/**
* コンストラクタ.
*/
public BarInstallUserDataLinksLimitTest() {
super(new WebAppDescriptor.Builder(BarInstallUserDataLinksLimitTest.INIT_PARAMS).build());
}
/**
* すべてのテストで必ず1度実行される処理.
*/
@BeforeClass
public static void beforeClass() {
DcCoreConfig.set(OData.NN_LINKS_MAX_NUM,
String.valueOf(linkNtoNMaxSize));
}
/**
* すべてのテスト完了時に実行される処理.
*/
@AfterClass
public static void afterClass() {
DcCoreConfig.set(OData.NN_LINKS_MAX_NUM,
String.valueOf(DEFAULT_LINKS_NTON_MAX_SIZE));
}
/**
* barファイルインストールでAST対ASTのユーザデータLinkの登録が上限値を超えた場合異常終了すること.
*/
@Test
public final void barファイルインストールでAST対ASTのユーザデータLinkの登録が上限値を超えた場合異常終了すること() {
final String reqCell = "BoxInstallLinksLimitCell";
final String reqPath = INSTALL_TARGET;
final String odataColName = "col1";
final String srcEntityName = "entity1";
final String targetEntityName = "entity2";
TResponse res = null;
File barFile = new File(RESOURCE_PATH + "/V1_1_2_bar_userdata_create_link_limit_nn.bar");
byte[] body = BarInstallTestUtils.readBarFile(barFile);
Map<String, String> headers = new LinkedHashMap<String, String>();
headers.put(HttpHeaders.CONTENT_TYPE, REQ_CONTENT_TYPE);
headers.put(HttpHeaders.CONTENT_LENGTH, String.valueOf(body.length));
try {
// 登録用のCellを作成
CellUtils.create(reqCell, MASTER_TOKEN_NAME, HttpStatus.SC_CREATED);
res = BarInstallTestUtils.request(REQUEST_NORM_FILE, reqCell, reqPath, headers, body);
res.statusCode(HttpStatus.SC_ACCEPTED);
String location = res.getHeader(HttpHeaders.LOCATION);
String expected = UrlUtils.cellRoot(reqCell) + reqPath;
assertEquals(expected, location);
BarInstallTestUtils.assertBarInstallStatus(location, DEFAULT_SCHEMA_URL, ProgressInfo.STATUS.FAILED);
// $links登録数のチェック
// 1. $linksが上限値分作成されていること
String query = String.format("?\\$top=%s&\\$inlinecount=allpages", 0);
TResponse response = Http.request("box/odatacol/list.txt")
.with("cell", reqCell)
.with("box", reqPath)
.with("collection", odataColName + "/" + srcEntityName + "('barInstallTest')")
.with("entityType", "_" + targetEntityName)
.with("query", query)
.with("accept", MediaType.APPLICATION_JSON)
.with("token", MASTER_TOKEN_NAME)
.returns()
.statusCode(HttpStatus.SC_OK)
.debug();
// レスポンスボディの件数のチェック
JSONArray results = (JSONArray) ((JSONObject) response.bodyAsJson().get("d")).get("results");
assertEquals(0, results.size());
// __countのチェック
ODataCommon.checkResponseBodyCount(response.bodyAsJson(),
linkNtoNMaxSize);
// 2. 上限値を超えた分のUserDataはlinkされていないこと
int userDataIndex = linkNtoNMaxSize + 1;
for (; userDataIndex < linkNtoNMaxSize + 4; userDataIndex++) {
response = UserDataUtils.listLink(reqCell, reqPath, odataColName, targetEntityName, "barInstallTest"
+ Integer.toString(userDataIndex), srcEntityName);
results = (JSONArray) ((JSONObject) response.bodyAsJson().get("d")).get("results");
assertEquals(0, results.size());
}
} finally {
// 登録用Cellを再帰的削除
Setup.cellBulkDeletion(reqCell);
}
}
/**
* barファイルインストールでONE対ASTのユーザデータLinkの登録が上限値を超えた場合登録できること.
*/
@Test
public final void barファイルインストールでONE対ASTのユーザデータLinkの登録が上限値を超えた場合登録できること() {
// Linkの関係がAST対ASTの場合のみ、Linkの数の上限値を設けるようにしている。
// 本テストでは、Linkの関係がONE対ASTの場合に上限値が有効にならないことを観点にしている。
final String reqCell = "BoxInstallLinksLimitCell";
final String reqPath = INSTALL_TARGET;
final String odataColName = "col1";
final String srcEntityName = "entity1";
final String targetEntityName = "entity2";
TResponse res = null;
File barFile = new File(RESOURCE_PATH + "/V1_1_2_bar_userdata_create_link_limit_1n.bar");
byte[] body = BarInstallTestUtils.readBarFile(barFile);
Map<String, String> headers = new LinkedHashMap<String, String>();
headers.put(HttpHeaders.CONTENT_TYPE, REQ_CONTENT_TYPE);
headers.put(HttpHeaders.CONTENT_LENGTH, String.valueOf(body.length));
try {
// 登録用のCellを作成
CellUtils.create(reqCell, MASTER_TOKEN_NAME, HttpStatus.SC_CREATED);
res = BarInstallTestUtils.request(REQUEST_NORM_FILE, reqCell, reqPath, headers, body);
res.statusCode(HttpStatus.SC_ACCEPTED);
String location = res.getHeader(HttpHeaders.LOCATION);
String expected = UrlUtils.cellRoot(reqCell) + reqPath;
assertEquals(expected, location);
BarInstallTestUtils.assertBarInstallStatus(location, DEFAULT_SCHEMA_URL, ProgressInfo.STATUS.COMPLETED);
// $links登録数のチェック
String query = String.format("?\\$top=%s&\\$inlinecount=allpages", 0);
TResponse response = Http.request("box/odatacol/list.txt")
.with("cell", reqCell)
.with("box", reqPath)
.with("collection", odataColName + "/" + srcEntityName + "('barInstallTest')")
.with("entityType", "_" + targetEntityName)
.with("query", query)
.with("accept", MediaType.APPLICATION_JSON)
.with("token", MASTER_TOKEN_NAME)
.returns()
.statusCode(HttpStatus.SC_OK)
.debug();
// レスポンスボディの件数のチェック
JSONArray results = (JSONArray) ((JSONObject) response.bodyAsJson().get("d")).get("results");
assertEquals(0, results.size());
// __countのチェック
ODataCommon.checkResponseBodyCount(response.bodyAsJson(),
linkNtoNMaxSize + 3);
} finally {
// 登録用Cellを再帰的削除
Setup.cellBulkDeletion(reqCell);
}
}
}