Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/release-5u21'
Browse files Browse the repository at this point in the history
  • Loading branch information
opengl-8080 committed Mar 25, 2022
2 parents 3565e7d + c95ab50 commit 5446b7d
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 44 deletions.
27 changes: 23 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.nablarch.example</groupId>
<artifactId>nablarch-example-rest</artifactId>
<version>5u20</version>
<version>5u21</version>
<packaging>war</packaging>

<properties>
Expand Down Expand Up @@ -39,7 +39,14 @@
<dependency>
<groupId>com.nablarch.profile</groupId>
<artifactId>nablarch-bom</artifactId>
<version>5u20</version>
<version>5u21</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.8.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down Expand Up @@ -138,7 +145,7 @@
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
<version>2.1.1</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -180,14 +187,25 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.nablarch.framework</groupId>
<artifactId>nablarch-testing-junit5</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>jp.co.tis.gsp</groupId>
<artifactId>gsp-dba-maven-plugin</artifactId>
<version>4.4.1</version>
<version>4.5.0</version>
<configuration>
<erdFile>src/main/resources/entity/data-model.edm</erdFile>

Expand Down Expand Up @@ -274,6 +292,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.2</version>
<configuration>
<webXml>${webxml.path}</webXml>
<classifier>${env.classifier}</classifier>
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/com/nablarch/example/action/ProjectAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.nablarch.example.dto.ProjectSearchDto;
import com.nablarch.example.entity.Project;
import com.nablarch.example.form.ProjectForm;
import com.nablarch.example.form.ProjectRenameForm;
import com.nablarch.example.form.ProjectSearchForm;
import com.nablarch.example.form.ProjectUpdateForm;
import nablarch.common.dao.EntityList;
Expand All @@ -16,6 +17,7 @@
import javax.validation.Valid;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PATCH;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
Expand Down Expand Up @@ -86,4 +88,22 @@ public HttpResponse update(ProjectUpdateForm form) {

return new HttpResponse(HttpResponse.Status.OK.getStatusCode());
}

/**
* プロジェクト名を更新する。
*
* @param form プロジェクト情報(プロジェクト名)
* @return HTTPレスポンス
*/
@PATCH
@Consumes(MediaType.APPLICATION_JSON)
@Valid
public HttpResponse patch(ProjectRenameForm form) {
Project project = UniversalDao.findById(Project.class, form.getProjectId());
project.setProjectName(form.getProjectName());

UniversalDao.update(project);

return new HttpResponse(HttpResponse.Status.NO_CONTENT.getStatusCode());
}
}
63 changes: 63 additions & 0 deletions src/main/java/com/nablarch/example/form/ProjectRenameForm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.nablarch.example.form;

import nablarch.core.validation.ee.Domain;
import nablarch.core.validation.ee.Required;

import javax.validation.constraints.AssertTrue;
import java.io.Serializable;

/**
* プロジェクト更新フォーム。
* @author Nabu Rakutaro
*/
public class ProjectRenameForm implements Serializable {

/** シリアルバージョンUID */
private static final long serialVersionUID = 1L;

/** プロジェクトID */
@Required
@Domain("id")
private String projectId;

/** プロジェクト名 */
@Required
@Domain("projectName")
private String projectName;

/**
* プロジェクトIDを取得する。
*
* @return プロジェクトID
*/
public String getProjectId() {
return projectId;
}

/**
* プロジェクト名を取得する。
*
* @return プロジェクト名
*/
public String getProjectName() {
return projectName;
}

/**
* プロジェクトIDを設定する。
*
* @param projectId プロジェクトID
*/
public void setProjectId(String projectId) {
this.projectId = projectId;
}

/**
* プロジェクト名を設定する。
*
* @param projectName プロジェクト名
*/
public void setProjectName(String projectName) {
this.projectName = projectName;
}
}
107 changes: 81 additions & 26 deletions src/test/java/com/nablarch/example/action/ProjectActionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import com.jayway.jsonpath.JsonPath;
import com.nablarch.example.entity.Project;
import com.nablarch.example.form.ProjectForm;
import com.nablarch.example.form.ProjectRenameForm;
import com.nablarch.example.form.ProjectUpdateForm;
import nablarch.core.beans.BeanUtil;
import nablarch.fw.web.HttpResponse;
import nablarch.test.core.http.RestTestSupport;
import nablarch.test.junit5.extension.http.RestTest;
import org.json.JSONException;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;

Expand All @@ -19,60 +21,63 @@
import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasJsonPath;
import static com.jayway.jsonpath.matchers.JsonPathMatchers.isJson;
import static com.jayway.jsonpath.matchers.JsonPathMatchers.withJsonPath;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertThat;

public class ProjectActionTest extends RestTestSupport {
@RestTest
class ProjectActionTest {
RestTestSupport support;

@Test
public void プロジェクト一覧が取得できること() throws JSONException {
void プロジェクト一覧が取得できること() throws JSONException {
String message = "プロジェクト一覧取得";
HttpResponse response = sendRequest(get("/projects"));
assertStatusCode(message, HttpResponse.Status.OK, response);
HttpResponse response = support.sendRequest(support.get("/projects"));
support.assertStatusCode(message, HttpResponse.Status.OK, response);

assertThat(response.getBodyString(), hasJsonPath("$", hasSize(10)));
JSONAssert.assertEquals(message, readTextResource("プロジェクト一覧が取得できること.json")
JSONAssert.assertEquals(message, support.readTextResource(ProjectActionTest.class, "プロジェクト一覧が取得できること.json")
, response.getBodyString(), JSONCompareMode.LENIENT);
}

@Test
public void プロジェクトを新規登録できること() {
void プロジェクトを新規登録できること() {
// プロジェクトが1件も登録されていないこと
String message1 = "プロジェクト一覧取得(登録前)";
HttpResponse beforeRegisterResponse = sendRequest(get("/projects"));
assertStatusCode(message1, HttpResponse.Status.OK, beforeRegisterResponse);
HttpResponse beforeRegisterResponse = support.sendRequest(support.get("/projects"));
support.assertStatusCode(message1, HttpResponse.Status.OK, beforeRegisterResponse);
assertThat(beforeRegisterResponse.getBodyString(), hasJsonPath("$", empty()));

// 新規登録
String message2 = "プロジェクト新規登録";
ProjectForm projectForm = createInsertProject();
HttpResponse registerResponse = sendRequest(post("/projects").setBody(projectForm));
assertStatusCode(message2, HttpResponse.Status.CREATED, registerResponse);
HttpResponse registerResponse = support.sendRequest(support.post("/projects").setBody(projectForm));
support.assertStatusCode(message2, HttpResponse.Status.CREATED, registerResponse);

// 登録したプロジェクトが取得できること
String message3 = "プロジェクト一覧取得(登録後)";
HttpResponse afterRegisterResponse = sendRequest(get("/projects"));
assertStatusCode(message3, HttpResponse.Status.OK, afterRegisterResponse);
HttpResponse afterRegisterResponse = support.sendRequest(support.get("/projects"));
support.assertStatusCode(message3, HttpResponse.Status.OK, afterRegisterResponse);
assertProjectEquals(BeanUtil.createAndCopy(Project.class, projectForm), afterRegisterResponse);

assertTableEquals("プロジェクトを新規登録できること");
support.assertTableEquals("プロジェクトを新規登録できること");
}

@Test
public void プロジェクトを更新できること() throws IOException {
void プロジェクトを更新できること() throws IOException {
String project001Uri = "/projects?projectName=プロジェクト001";
String project888Uri = "/projects?projectName=プロジェクト888";

String message1 = "変更前に変更しようとするプロジェクト名に一致するデータが存在しないこと";
HttpResponse projectNameNotFoundResponse = sendRequest(get(project888Uri));
assertStatusCode(message1, HttpResponse.Status.OK, projectNameNotFoundResponse);
HttpResponse projectNameNotFoundResponse = support.sendRequest(support.get(project888Uri));
support.assertStatusCode(message1, HttpResponse.Status.OK, projectNameNotFoundResponse);
assertThat(message1, projectNameNotFoundResponse.getBodyString(), hasJsonPath("$", empty()));

String message2 = "変更対象取得";
HttpResponse getTargetProjectResponse = sendRequest(get(project001Uri));
assertStatusCode(message2, HttpResponse.Status.OK, getTargetProjectResponse);
HttpResponse getTargetProjectResponse = support.sendRequest(support.get(project001Uri));
support.assertStatusCode(message2, HttpResponse.Status.OK, getTargetProjectResponse);
assertThat(message2, getTargetProjectResponse.getBodyString(), isJson(allOf(
withJsonPath("$", hasSize(1))
, withJsonPath("$[0]", hasEntry("projectName", "プロジェクト001")))));
Expand All @@ -81,21 +86,58 @@ public class ProjectActionTest extends RestTestSupport {
ProjectUpdateForm updateForm = setUpdateProject(String.valueOf(projectId));

String message3 = "プロジェクト更新";
HttpResponse updateResponse = sendRequest(put("/projects").setBody(updateForm));
assertStatusCode(message3, HttpResponse.Status.OK, updateResponse);
HttpResponse updateResponse = support.sendRequest(support.put("/projects").setBody(updateForm));
support.assertStatusCode(message3, HttpResponse.Status.OK, updateResponse);

String message4 = "変更前のプロジェクト名に一致するデータが存在しないこと";
HttpResponse previousNameNotFoundResponse = sendRequest(get(project001Uri));
assertStatusCode(message4, HttpResponse.Status.OK, previousNameNotFoundResponse);
HttpResponse previousNameNotFoundResponse = support.sendRequest(support.get(project001Uri));
support.assertStatusCode(message4, HttpResponse.Status.OK, previousNameNotFoundResponse);
assertThat(message4, previousNameNotFoundResponse.getBodyString(), hasJsonPath("$", empty()));

String message5 = "取得したプロジェクトが変更した内容と一致すること";
HttpResponse projectNameFoundResponse = sendRequest(get(project888Uri));
assertStatusCode(message5, HttpResponse.Status.OK, projectNameFoundResponse);
HttpResponse projectNameFoundResponse = support.sendRequest(support.get(project888Uri));
support.assertStatusCode(message5, HttpResponse.Status.OK, projectNameFoundResponse);
// Projectとのアサート
assertProjectEquals(BeanUtil.createAndCopy(Project.class, updateForm), projectNameFoundResponse);
}

@Test
void プロジェクト名を更新できること() throws IOException {
String project002Uri = "/projects?projectName=プロジェクト002";
String project777Uri = "/projects?projectName=プロジェクト777";

String message1 = "変更前に変更しようとするプロジェクト名に一致するデータが存在しないこと";
HttpResponse projectNameNotFoundResponse = support.sendRequest(support.get(project777Uri));
support.assertStatusCode(message1, HttpResponse.Status.OK, projectNameNotFoundResponse);
assertThat(message1, projectNameNotFoundResponse.getBodyString(), hasJsonPath("$", empty()));

String message2 = "変更対象取得";
HttpResponse getTargetProjectResponse = support.sendRequest(support.get(project002Uri));
support.assertStatusCode(message2, HttpResponse.Status.OK, getTargetProjectResponse);
assertThat(message2, getTargetProjectResponse.getBodyString(), isJson(allOf(
withJsonPath("$", hasSize(1))
, withJsonPath("$[0]", hasEntry("projectName", "プロジェクト002")))));

int projectId = JsonPath.read(getTargetProjectResponse.getBodyStream(), "$[0].projectId");
ProjectRenameForm renameForm = setRenameProject(String.valueOf(projectId));

String message3 = "プロジェクト名更新";
HttpResponse renamedResponse = support.sendRequest(support.patch("/projects").setBody(renameForm));
support.assertStatusCode(message3, HttpResponse.Status.NO_CONTENT, renamedResponse);

String message4 = "変更前のプロジェクト名に一致するデータが存在しないこと";
HttpResponse previousNameNotFoundResponse = support.sendRequest(support.get(project002Uri));
support.assertStatusCode(message4, HttpResponse.Status.OK, previousNameNotFoundResponse);
assertThat(message4, previousNameNotFoundResponse.getBodyString(), hasJsonPath("$", empty()));

String message5 = "取得したプロジェクトが変更した内容と一致すること";
HttpResponse projectNameFoundResponse = support.sendRequest(support.get(project777Uri));
support.assertStatusCode(message5, HttpResponse.Status.OK, projectNameFoundResponse);
with(projectNameFoundResponse.getBodyString())
.assertThat("$", hasSize(1))
.assertThat("$[0]", hasEntry("projectId", Integer.parseInt(renameForm.getProjectId())))
.assertThat("$[0]", hasEntry("projectName", renameForm.getProjectName()));
}

private void assertProjectEquals(Project expected, HttpResponse response) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
Expand Down Expand Up @@ -163,4 +205,17 @@ private static ProjectUpdateForm setUpdateProject(String projectId) {
form.setAllocationOfCorpExpenses("4000");
return form;
}

/**
* プロジェクト名変更用プロジェクト情報設定
*
* @param projectId 更新対象プロジェクトID
* @return プロジェクト名変更用プロジェクト情報
*/
private static ProjectRenameForm setRenameProject(String projectId) {
ProjectRenameForm form = new ProjectRenameForm();
form.setProjectId(projectId);
form.setProjectName("プロジェクト777");
return form;
}
}
Loading

0 comments on commit 5446b7d

Please sign in to comment.