Skip to content

Commit

Permalink
Merge pull request #1818 from matthieuRioual/AddLoggerToVueJS
Browse files Browse the repository at this point in the history
Add Logger to Vue common application
  • Loading branch information
pascalgrimaud authored May 24, 2022
2 parents ec4e4b8 + 961c5c0 commit 9cadd15
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ public class VueDomainService implements VueService {
public static final String NEEDLE_IMPORT = "// jhipster-needle-main-ts-import";
public static final String NEEDLE_PROVIDER = "// jhipster-needle-main-ts-provider";
public static final String SOURCE_PRIMARY = getPath(SOURCE, "webapp/app/common/primary");
public static final String SOURCE_DOMAIN = getPath(SOURCE, "webapp/app/common/domain");
public static final String SOURCE_SECONDARY = getPath(SOURCE, "webapp/app/common/secondary");
public static final String SOURCE_TEST_PRIMARY = getPath(SOURCE, "test/spec/common/primary");
public static final String SOURCE_PRIMARY_APP = getPath(SOURCE_PRIMARY, "app");
public static final String DESTINATION_PRIMARY = "src/main/webapp/app/common/primary";
public static final String DESTINATION_DOMAIN = "src/main/webapp/app/common/domain";
public static final String DESTINATION_SECONDARY = "src/main/webapp/app/common/secondary";
public static final String DESTINATION_PRIMARY_APP = DESTINATION_PRIMARY + "/app";
public static final String DESTINATION_PRIMARY_TEST = "src/test/javascript/spec/common/primary";
public static final String DESTINATION_PRIMARY_ROUTER = DESTINATION_PRIMARY + "/app";
Expand Down Expand Up @@ -60,6 +64,7 @@ private void addCommonVue(Project project) {
addRootFiles(project);
addAppFiles(project);
addRouter(project);
addLogger(project);
}

public void addDependencies(Project project) {
Expand Down Expand Up @@ -120,6 +125,38 @@ private void addAxiosFile(Project project) {
);
}

public void addLogger(Project project) {
addLoggerFiles(project);
addLoggerTestFiles(project);
}

private void addLoggerFiles(Project project) {
projectRepository.template(
ProjectFile.forProject(project).withSource(SOURCE_DOMAIN, "Logger.ts").withDestinationFolder(DESTINATION_DOMAIN)
);
projectRepository.template(
ProjectFile.forProject(project).withSource(SOURCE_DOMAIN, "Message.ts").withDestinationFolder(DESTINATION_DOMAIN)
);
projectRepository.template(
ProjectFile.forProject(project).withSource(SOURCE_SECONDARY, "ConsoleLogger.ts").withDestinationFolder(DESTINATION_SECONDARY)
);
}

private void addLoggerTestFiles(Project project) {
projectRepository.template(
ProjectFile
.forProject(project)
.withSource(getPath(SOURCE, "test/spec/common/domain"), "Logger.fixture.ts")
.withDestinationFolder("src/test/javascript/spec/common/domain")
);
projectRepository.template(
ProjectFile
.forProject(project)
.withSource(getPath(SOURCE, "test/spec/common/secondary"), "ConsoleLogger.spec.ts")
.withDestinationFolder("src/test/javascript/spec/common/secondary")
);
}

public void addDevDependencies(Project project) {
Vue.devDependencies().forEach(devDependency -> addDevDependency(project, devDependency));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import sinon, { SinonStub } from 'sinon';
import { Logger } from '@/common/domain/Logger';

export interface LoggerFixture extends Logger {
error: SinonStub;
}

export const stubLogger = (): LoggerFixture => ({
error: sinon.stub(),
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import sinon from 'sinon';

import ConsoleLogger from '@/common/secondary/ConsoleLogger';

describe('ConsoleLogger', () => {
it('should log an error', () => {
const logger = {
error: sinon.stub(),
};
const consoleLogger = new ConsoleLogger(logger as any);
const error = new Error('Error message');

consoleLogger.error('An error occurs', error);

const [message, errorPassed] = logger.error.getCall(0).args;
expect(message).toBe('An error occurs\n');
expect(errorPassed).toBeInstanceOf(Error);
expect(errorPassed.message).toBe('Error message');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Message } from '@/common/domain/Message';

export interface Logger {
error(message: Message, error: Error): void;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Message = string;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Logger } from '@/common/domain/Logger';
import { Message } from '@/common/domain/Message';

export default class ConsoleLogger implements Logger {
constructor(private logger: Console) {}

error(message: Message, error: Error) {
this.logger.error(`${message}\n`, error);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ void shouldAddVue() {
VueAssert.assertAppFiles(project);
VueAssert.assertAppWithCss(project);
VueAssert.assertLogos(project);
VueAssert.assertLogger(project);
VueAssert.assertAxiosFile(project);

VueAssert.assertJestSonar(project);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,12 @@ public static void assertAxiosFile(Project project) {
assertFileExist(project, "src/test/javascript/spec/http/AxiosHttpStub.ts");
assertFileExist(project, "src/test/javascript/spec/http/AxiosStub.ts");
}

public static void assertLogger(Project project) {
assertFileExist(project, "src/main/webapp/app/common/domain/Logger.ts");
assertFileExist(project, "src/main/webapp/app/common/domain/Message.ts");
assertFileExist(project, "src/main/webapp/app/common/secondary/ConsoleLogger.ts");
assertFileExist(project, "src/test/javascript/spec/common/domain/Logger.fixture.ts");
assertFileExist(project, "src/test/javascript/spec/common/secondary/ConsoleLogger.spec.ts");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,13 @@ void shouldAddAxiosFile() {

verify(projectRepository, times(4)).template(any(ProjectFile.class));
}

@Test
void shouldAddLoggerFiles() {
Project project = tmpProjectWithPackageJson();

vueDomainService.addLogger(project);

verify(projectRepository, times(5)).template(any(ProjectFile.class));
}
}

0 comments on commit 9cadd15

Please sign in to comment.