Skip to content

Commit

Permalink
fixed resource using PostMapping when the version of JHipster is belo…
Browse files Browse the repository at this point in the history
…w 3.10.0

fix geraldhumphries#30
  • Loading branch information
geraldhumphries committed Aug 29, 2017
1 parent aa59953 commit 9416ba4
Show file tree
Hide file tree
Showing 6 changed files with 7,159 additions and 1 deletion.
58 changes: 58 additions & 0 deletions __tests__/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,64 @@ describe('JHipster Elasticsearch Reindexer', () => {
});
});

describe('App with PostMapping', () => {
beforeAll(() => {
return helpers.run(path.join(__dirname, '../generators/app'))
.withOptions({skipInstall: true, skipChecks: true})
.inTmpDir((dir) => {
// JHipster version >= 3.10.0
fse.copySync(path.join(__dirname, 'templates/postmapping'), dir);
fse.copySync(path.join(__dirname, 'templates/.jhipster'), dir + '/.jhipster');
});
});

it('imports PostMapping', () => {
assert.fileContent(JAVA_PATH + 'web/rest/ElasticsearchIndexResource.java', 'import org.springframework.web.bind.annotation.PostMapping;');
});

it('does not import RequestMethod or MediaType', () => {
assert.noFileContent(JAVA_PATH + 'web/rest/ElasticsearchIndexResource.java', 'import org.springframework.http.MediaType;');
assert.noFileContent(JAVA_PATH + 'web/rest/ElasticsearchIndexResource.java', 'import org.springframework.web.bind.annotation.RequestMethod;');
});

it('uses PostMapping', () => {
assert.fileContent(JAVA_PATH + 'web/rest/ElasticsearchIndexResource.java', '@PostMapping("/elasticsearch/index")');
});

it('does not use RequestMapping', () => {
assert.noFileContent(JAVA_PATH + 'web/rest/ElasticsearchIndexResource.java', '@RequestMapping(value = "/elasticsearch/index",');
});
});

describe('App with RequestMapping', () => {
beforeAll(() => {
return helpers.run(path.join(__dirname, '../generators/app'))
.withOptions({skipInstall: true, skipChecks: true})
.inTmpDir((dir) => {
// JHipster version < 3.10.0
fse.copySync(path.join(__dirname, 'templates/requestmapping'), dir);
fse.copySync(path.join(__dirname, 'templates/.jhipster'), dir + '/.jhipster');
});
});

it('does not import PostMapping', () => {
assert.noFileContent(JAVA_PATH + 'web/rest/ElasticsearchIndexResource.java', 'import org.springframework.web.bind.annotation.PostMapping;');
});

it('imports RequestMethod and MediaType', () => {
assert.fileContent(JAVA_PATH + 'web/rest/ElasticsearchIndexResource.java', 'import org.springframework.http.MediaType;');
assert.fileContent(JAVA_PATH + 'web/rest/ElasticsearchIndexResource.java', 'import org.springframework.web.bind.annotation.RequestMethod;');
});

it('does not use PostMapping', () => {
assert.noFileContent(JAVA_PATH + 'web/rest/ElasticsearchIndexResource.java', '@PostMapping("/elasticsearch/index")');
});

it('uses RequestMapping', () => {
assert.fileContent(JAVA_PATH + 'web/rest/ElasticsearchIndexResource.java', '@RequestMapping(value = "/elasticsearch/index",');
});
});

describe('App with field injection', () => {
beforeAll(() => {
return helpers.run(path.join(__dirname, '../generators/app'))
Expand Down
33 changes: 33 additions & 0 deletions __tests__/templates/postmapping/.yo-rc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"generator-jhipster": {
"applicationType": "monolith",
"baseName": "testApp",
"packageName": "com.mycompany.myapp",
"packageFolder": "com/mycompany/myapp",
"authenticationType": "session",
"hibernateCache": "ehcache",
"clusteredHttpSession": false,
"websocket": false,
"databaseType": "sql",
"devDatabaseType": "h2Disk",
"prodDatabaseType": "mysql",
"searchEngine": false,
"buildTool": "maven",
"enableSocialSignIn": false,
"rememberMeKey": "2bb60a80889aa6e6767e9ccd8714982681152aa5",
"testFrameworks": [
"gatling"
],
"skipClient": true,
"jhipsterVersion": "3.10.0",
"serverPort": "8080",
"messageBroker": false,
"serviceDiscoveryType": false,
"enableTranslation": true,
"jhiPrefix": "jhi",
"clientPackageManager": "npm",
"languages": [
"fr"
]
}
}
33 changes: 33 additions & 0 deletions __tests__/templates/requestmapping/.yo-rc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"generator-jhipster": {
"applicationType": "monolith",
"baseName": "testApp",
"packageName": "com.mycompany.myapp",
"packageFolder": "com/mycompany/myapp",
"authenticationType": "session",
"hibernateCache": "ehcache",
"clusteredHttpSession": false,
"websocket": false,
"databaseType": "sql",
"devDatabaseType": "h2Disk",
"prodDatabaseType": "mysql",
"searchEngine": false,
"buildTool": "maven",
"enableSocialSignIn": false,
"rememberMeKey": "2bb60a80889aa6e6767e9ccd8714982681152aa5",
"testFrameworks": [
"gatling"
],
"skipClient": true,
"jhipsterVersion": "3.9.2",
"serverPort": "8080",
"messageBroker": false,
"serviceDiscoveryType": false,
"enableTranslation": true,
"jhiPrefix": "jhi",
"clientPackageManager": "npm",
"languages": [
"fr"
]
}
}
4 changes: 3 additions & 1 deletion generators/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ module.exports = JhipsterGenerator.extend({
} else {
this.jhipsterMajorVersion = config.jhipsterVersion[0];
}

this.requiresSetLocation = this.jhipsterVersion ? semver.lt(this.jhipsterVersion, '4.4.4') : false;
this.usePostMapping = this.jhipsterVersion ? semver.gte(this.jhipsterVersion, '3.10.0') : false;

this.entityFiles = shelljs.ls(jhipsterVar.jhipsterConfigDirectory).filter(function (file) {
return file.match(/\.json$/);
});
Expand All @@ -83,7 +86,6 @@ module.exports = JhipsterGenerator.extend({
this.appFolder = 'scripts/app/admin/elasticsearch-reindex/';
this.serviceFolder = 'scripts/components/admin/';
}

jhipsterVar.javaDir = `${jhipsterConstants.SERVER_MAIN_SRC_DIR + this.packageFolder}/`;
jhipsterVar.resourceDir = jhipsterConstants.SERVER_MAIN_RES_DIR;
jhipsterVar.webappDir = jhipsterConstants.CLIENT_MAIN_SRC_DIR;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,18 @@
import <%=packageName%>.web.rest.util.HeaderUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
<%_ if (!usePostMapping) { _%>
import org.springframework.http.MediaType;
<%_ } _%>
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.annotation.Secured;
<%_ if (usePostMapping) { _%>
import org.springframework.web.bind.annotation.PostMapping;
<%_ } _%>
import org.springframework.web.bind.annotation.RequestMapping;
<%_ if (!usePostMapping) { _%>
import org.springframework.web.bind.annotation.RequestMethod;
<%_ } _%>
import org.springframework.web.bind.annotation.RestController;

<%_ if (jhipsterMajorVersion < 4) { _%>
Expand Down Expand Up @@ -41,7 +49,13 @@ public ElasticsearchIndexResource(ElasticsearchIndexService elasticsearchIndexSe
/**
* POST /elasticsearch/index -> Reindex all Elasticsearch documents
*/
<%_ if (usePostMapping) { _%>
@PostMapping("/elasticsearch/index")
<%_ } else { _%>
@RequestMapping(value = "/elasticsearch/index",
method = RequestMethod.POST,
produces = MediaType.TEXT_PLAIN_VALUE)
<%_ } _%>
@Timed
@Secured(AuthoritiesConstants.ADMIN)
public ResponseEntity<Void> reindexAll() throws URISyntaxException {
Expand Down
Loading

0 comments on commit 9416ba4

Please sign in to comment.