Skip to content

Commit

Permalink
Update generators/app/prompts.js
Browse files Browse the repository at this point in the history
Co-authored-by: François Delbrayelle <fdelbrayelle@gmail.com>
  • Loading branch information
monxxi and fdelbrayelle committed Jun 15, 2020
1 parent 0a09bb1 commit dfc954a
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 25 deletions.
3 changes: 2 additions & 1 deletion generators/app/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ function writeFiles(generator) {
writeProperties(kafkaPreviousConfiguration, kafkaPreviousTestConfiguration, utils.transformToJavaClassNameCase(prefix));
});

if (containsComponent(constants.CONSUMER_COMPONENT) || containsComponent(constants.PRODUCER_COMPONENT)) {
if (generator.topics && generator.topics.length > 0) {
if (!kafkaPreviousConfiguration.kafka.topic) {
kafkaPreviousConfiguration.kafka.topic = {};
}
Expand All @@ -295,6 +295,7 @@ function writeFiles(generator) {
kafkaPreviousTestConfiguration.kafka.topic = {};
}
}

generator.topics.forEach(topic => {
kafkaPreviousConfiguration.kafka.topic[topic.key] = topic.value;
kafkaPreviousTestConfiguration.kafka.topic[topic.key] = topic.value;
Expand Down
37 changes: 13 additions & 24 deletions generators/app/prompts.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,6 @@ module.exports = {
const previousConfiguration = (generator, cleanup = false) =>
utils.getPreviousKafkaConfiguration(generator, `${jhipsterConstants.SERVER_MAIN_RES_DIR}config/application.yml`, cleanup).kafka;

function generationTypeChoices() {
return [
{
name: 'Big Bang Mode (build a configuration from scratch)',
value: constants.BIGBANG_MODE
},
{
name: 'Incremental Mode (upgrade an existing configuration)',
value: constants.INCREMENTAL_MODE
}
];
}

function offsetChoices() {
return [
{
Expand Down Expand Up @@ -119,25 +106,27 @@ function askForOperations(generator) {
when: !generator.options['skip-prompts'],
type: 'confirm',
name: 'cleanup',
message: 'do you want to reset your kafka configuration?',
choices: generationTypeChoices(),
default: false
message: 'Do you want to clean up your current Kafka configuration?',
default: false,
validate: input => (_.lowerCase(input) !== 'o' && _.lowerCase(input) !== 'n' ? 'Please enter Y or N' : true)
}
];

const done = generator.async();
try {
generator.prompt(prompts).then(props => {
generator.props.cleanup = props.cleanup;
askForIncrementalOperations(generator, done);
if (props.cleanup) {
generator.props.cleanup = props.cleanup;
}
askForEntityOperations(generator, done);
});
} catch (e) {
generator.error('An error occurred while asking for operations', e);
done();
}
}

function askForIncrementalOperations(generator, done) {
function askForEntityOperations(generator, done) {
const getConcernedEntities = previousConfiguration => {
const allEntities = entitiesChoices(generator);
const entitiesComponents = utils.extractEntitiesComponents(previousConfiguration);
Expand All @@ -153,7 +142,7 @@ function askForIncrementalOperations(generator, done) {
);
};

const incrementalPrompt = [
const entityPrompts = [
{
when: !generator.options['skip-prompts'],
type: 'list',
Expand Down Expand Up @@ -186,7 +175,7 @@ function askForIncrementalOperations(generator, done) {
}
];

generator.prompt(incrementalPrompt).then(answers => {
generator.prompt(entityPrompts).then(answers => {
generator.props.currentEntity = undefined;

if (answers.currentEntity) {
Expand All @@ -198,7 +187,7 @@ function askForIncrementalOperations(generator, done) {
if (answers.currentPrefix && !generator.props.componentsPrefixes.includes(answers.currentPrefix)) {
generator.props.componentsPrefixes.push(answers.currentPrefix);
}
askForIncrementalEntityOperations(generator, done);
askForComponentsEntityOperation(generator, done);
} else {
done();
}
Expand Down Expand Up @@ -228,7 +217,7 @@ function getAvailableComponentsWithoutEntity(generator, previousConfiguration, p
return availableComponents;
}

function askForIncrementalEntityOperations(generator, done) {
function askForComponentsEntityOperation(generator, done) {
const getConcernedComponents = (previousConfiguration, entityName, currentPrefix) => {
const availableComponents = [];
const allComponentChoices = componentsChoices();
Expand Down Expand Up @@ -366,7 +355,7 @@ function askForIncrementalEntityOperations(generator, done) {
}

if (answers.continueAddingEntitiesComponents) {
askForIncrementalOperations(generator, done);
askForEntityOperations(generator, done);
} else {
done();
}
Expand Down
172 changes: 172 additions & 0 deletions test/app.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const constants = require('../generators/constants');
const FOO_ENTITY = 'Foo';
const AWESOME_ENTITY = 'AwesomeEntity';
const COMPONENT_PREFIX = 'ComponentsWithoutEntity';
const COMPONENT_PREFIX_LOWERCASE = 'componentsWithoutEntity';

const COMPONENTS_CHOSEN = Object.freeze({ all: 1, consumer: 2, producer: 3 });
const CUSTOM_TOPIC_NAME = 'custom_topic_name';
const EXISTING_TOPIC_NAME = 'queuing.message_broker_with_entities.foo';
Expand Down Expand Up @@ -403,6 +405,62 @@ describe('JHipster generator kafka', () => {
});

describe('with an existing previous generation', () => {
describe('with --force --skip-prompts options', () => {
before(done => {
helpers
.run(path.join(__dirname, '../generators/app'))
.inTmpDir(dir => {
fse.copySync(path.join(__dirname, '../test/templates/message-broker-with-entities-2nd-call'), dir);
})
// RunContext from run-context.js (yeoman-test) have 'force' option by default
.withOptions({ skipPrompts: true })
.on('end', done);
});

it('should remove consumer and producer for previous entity and generate generic consumer and akhq.yml ', () => {
const expectedFiles = [
`${jhipsterConstants.SERVER_MAIN_SRC_DIR}com/mycompany/myapp/service/kafka/GenericConsumer.java`,
`${jhipsterConstants.SERVER_MAIN_SRC_DIR}com/mycompany/myapp/service/kafka/deserializer/DeserializationError.java`,
`${jhipsterConstants.MAIN_DIR}docker/akhq.yml`
];

assert.file(expectedFiles);

const notExpectedFiles = [
`${jhipsterConstants.SERVER_MAIN_SRC_DIR}com/mycompany/myapp/service/kafka/consumer/${FOO_ENTITY}Consumer.java`,
`${jhipsterConstants.SERVER_MAIN_SRC_DIR}com/mycompany/myapp/service/kafka/deserializer/${FOO_ENTITY}Deserializer.java`,
`${jhipsterConstants.SERVER_MAIN_SRC_DIR}com/mycompany/myapp/service/kafka/producer/${FOO_ENTITY}Producer.java`,
`${jhipsterConstants.SERVER_MAIN_SRC_DIR}com/mycompany/myapp/service/kafka/serializer/${FOO_ENTITY}Serializer.java`,
`${jhipsterConstants.SERVER_MAIN_SRC_DIR}com/mycompany/myapp/web/rest/kafka/${FOO_ENTITY}KafkaResource.java`
];
assert.noFile(notExpectedFiles);
});

it('should update application.yml', () => {
assert.fileContent(
`${jhipsterConstants.SERVER_MAIN_RES_DIR}config/application.yml`,
/bootstrap.servers: \${KAFKA_BOOTSTRAP_SERVERS:localhost:9092}/
);

assert.fileContent(
`${jhipsterConstants.SERVER_TEST_RES_DIR}config/application.yml`,
/bootstrap.servers: \${KAFKA_BOOTSTRAP_SERVERS:localhost:9092}/
);

assert.noFileContent(`${jhipsterConstants.SERVER_MAIN_RES_DIR}config/application.yml`, /topic:.*/);

assert.noFileContent(`${jhipsterConstants.SERVER_MAIN_RES_DIR}config/application.yml`, /producer:.*/);

assert.noFileContent(`${jhipsterConstants.SERVER_MAIN_RES_DIR}config/application.yml`, /consumer:.*/);

assert.noFileContent(`${jhipsterConstants.SERVER_TEST_RES_DIR}config/application.yml`, /topic:.*/);

assert.noFileContent(`${jhipsterConstants.SERVER_TEST_RES_DIR}config/application.yml`, /producer:.*/);

assert.noFileContent(`${jhipsterConstants.SERVER_TEST_RES_DIR}config/application.yml`, /consumer:.*/);
});
});

describe(`with a cleanup and a single other entity ${AWESOME_ENTITY}`, () => {
before(done => {
helpers
Expand Down Expand Up @@ -458,6 +516,120 @@ describe('JHipster generator kafka', () => {
});
});

describe(`with a cleanup and a single no_entity ${COMPONENT_PREFIX}`, () => {
before(done => {
helpers
.run(path.join(__dirname, '../generators/app'))
.inTmpDir(dir => {
fse.copySync(path.join(__dirname, '../test/templates/message-broker-with-entities-2nd-call'), dir);
})
.withPrompts({
cleanup: true,
currentEntity: constants.NO_ENTITY,
currentPrefix: COMPONENT_PREFIX,
currentEntityComponents: [constants.CONSUMER_COMPONENT, constants.PRODUCER_COMPONENT],
continueAddingEntitiesComponents: false
})
.on('end', done);
});

it(`should remove all files and generate only consumer and Producer files for the entity ${COMPONENT_PREFIX}`, () => {
const expectedFiles = [
`${jhipsterConstants.SERVER_MAIN_SRC_DIR}com/mycompany/myapp/service/kafka/consumer/${COMPONENT_PREFIX}Consumer.java`,
`${jhipsterConstants.SERVER_MAIN_SRC_DIR}com/mycompany/myapp/service/kafka/deserializer/${COMPONENT_PREFIX}Deserializer.java`,
`${jhipsterConstants.SERVER_MAIN_SRC_DIR}com/mycompany/myapp/service/kafka/producer/${COMPONENT_PREFIX}Producer.java`,
`${jhipsterConstants.SERVER_MAIN_SRC_DIR}com/mycompany/myapp/service/kafka/serializer/${COMPONENT_PREFIX}Serializer.java`,
`${jhipsterConstants.SERVER_MAIN_SRC_DIR}com/mycompany/myapp/web/rest/kafka/${COMPONENT_PREFIX}KafkaResource.java`
];
assert.file(expectedFiles);

const notExpectedFiles = [
`${jhipsterConstants.SERVER_MAIN_SRC_DIR}com/mycompany/myapp/service/kafka/consumer/${FOO_ENTITY}Consumer.java`,
`${jhipsterConstants.SERVER_MAIN_SRC_DIR}com/mycompany/myapp/service/kafka/deserializer/${FOO_ENTITY}Deserializer.java`,
`${jhipsterConstants.SERVER_MAIN_SRC_DIR}com/mycompany/myapp/service/kafka/producer/${FOO_ENTITY}Producer.java`,
`${jhipsterConstants.SERVER_MAIN_SRC_DIR}com/mycompany/myapp/service/kafka/serializer/${FOO_ENTITY}Serializer.java`,
`${jhipsterConstants.SERVER_MAIN_SRC_DIR}com/mycompany/myapp/web/rest/kafka/${FOO_ENTITY}KafkaResource.java`
];
assert.noFile(notExpectedFiles);
});

it(`should regenerate all basics properties and producer/consumer properties for only ${COMPONENT_PREFIX}`, () => {
assert.fileContent(
`${jhipsterConstants.SERVER_MAIN_RES_DIR}config/application.yml`,
/bootstrap.servers: \${KAFKA_BOOTSTRAP_SERVERS:localhost:9092}/
);

assert.fileContent(
`${jhipsterConstants.SERVER_TEST_RES_DIR}config/application.yml`,
/bootstrap.servers: \${KAFKA_BOOTSTRAP_SERVERS:localhost:9092}/
);

const { applicationYml, testApplicationYml } = loadApplicationYaml();
assertMinimalProperties(applicationYml, testApplicationYml, COMPONENT_PREFIX, constants.PRODUCER_COMPONENT);
assertMinimalProperties(applicationYml, testApplicationYml, COMPONENT_PREFIX, constants.CONSUMER_COMPONENT);
assertNoProperties(applicationYml, testApplicationYml, FOO_ENTITY, constants.CONSUMER_COMPONENT);
assertNoProperties(applicationYml, testApplicationYml, FOO_ENTITY, constants.PRODUCER_COMPONENT);
});
});

describe(`with a cleanup and a single no_entity with prefix lowerCase '${COMPONENT_PREFIX_LOWERCASE}'`, () => {
before(done => {
helpers
.run(path.join(__dirname, '../generators/app'))
.inTmpDir(dir => {
fse.copySync(path.join(__dirname, '../test/templates/message-broker-with-entities-2nd-call'), dir);
})
.withPrompts({
cleanup: true,
currentEntity: constants.NO_ENTITY,
currentPrefix: COMPONENT_PREFIX_LOWERCASE,
currentEntityComponents: [constants.CONSUMER_COMPONENT, constants.PRODUCER_COMPONENT],
continueAddingEntitiesComponents: false
})
.on('end', done);
});

it(`should remove all files and generate only consumer and Producer files for the entity ${COMPONENT_PREFIX_LOWERCASE}`, () => {
const javaClassNameCase = _.upperFirst(_.camelCase(COMPONENT_PREFIX_LOWERCASE));

const expectedFiles = [
`${jhipsterConstants.SERVER_MAIN_SRC_DIR}com/mycompany/myapp/service/kafka/consumer/${javaClassNameCase}Consumer.java`,
`${jhipsterConstants.SERVER_MAIN_SRC_DIR}com/mycompany/myapp/service/kafka/deserializer/${javaClassNameCase}Deserializer.java`,
`${jhipsterConstants.SERVER_MAIN_SRC_DIR}com/mycompany/myapp/service/kafka/producer/${javaClassNameCase}Producer.java`,
`${jhipsterConstants.SERVER_MAIN_SRC_DIR}com/mycompany/myapp/service/kafka/serializer/${javaClassNameCase}Serializer.java`,
`${jhipsterConstants.SERVER_MAIN_SRC_DIR}com/mycompany/myapp/web/rest/kafka/${javaClassNameCase}KafkaResource.java`
];
assert.file(expectedFiles);

const notExpectedFiles = [
`${jhipsterConstants.SERVER_MAIN_SRC_DIR}com/mycompany/myapp/service/kafka/consumer/${FOO_ENTITY}Consumer.java`,
`${jhipsterConstants.SERVER_MAIN_SRC_DIR}com/mycompany/myapp/service/kafka/deserializer/${FOO_ENTITY}Deserializer.java`,
`${jhipsterConstants.SERVER_MAIN_SRC_DIR}com/mycompany/myapp/service/kafka/producer/${FOO_ENTITY}Producer.java`,
`${jhipsterConstants.SERVER_MAIN_SRC_DIR}com/mycompany/myapp/service/kafka/serializer/${FOO_ENTITY}Serializer.java`,
`${jhipsterConstants.SERVER_MAIN_SRC_DIR}com/mycompany/myapp/web/rest/kafka/${FOO_ENTITY}KafkaResource.java`
];
assert.noFile(notExpectedFiles);
});

it(`should regenerate all basics properties and producer/consumer properties for only ${COMPONENT_PREFIX_LOWERCASE}`, () => {
assert.fileContent(
`${jhipsterConstants.SERVER_MAIN_RES_DIR}config/application.yml`,
/bootstrap.servers: \${KAFKA_BOOTSTRAP_SERVERS:localhost:9092}/
);

assert.fileContent(
`${jhipsterConstants.SERVER_TEST_RES_DIR}config/application.yml`,
/bootstrap.servers: \${KAFKA_BOOTSTRAP_SERVERS:localhost:9092}/
);

const { applicationYml, testApplicationYml } = loadApplicationYaml();
assertMinimalProperties(applicationYml, testApplicationYml, COMPONENT_PREFIX_LOWERCASE, constants.PRODUCER_COMPONENT);
assertMinimalProperties(applicationYml, testApplicationYml, COMPONENT_PREFIX_LOWERCASE, constants.CONSUMER_COMPONENT);
assertNoProperties(applicationYml, testApplicationYml, FOO_ENTITY, constants.CONSUMER_COMPONENT);
assertNoProperties(applicationYml, testApplicationYml, FOO_ENTITY, constants.PRODUCER_COMPONENT);
});
});

describe('with only a consumer for a single entity', () => {
before(done => {
helpers
Expand Down

0 comments on commit dfc954a

Please sign in to comment.