If you want to make your world a better place (Miss u MJ) and/or if you just want to get rid of all those newsletters you keep receiving, you can try Cleanfox.
It will automatically detect newsletters / spam and will let you choose what you want to remove. It can also unsubscribe you from those newsletters.
It can be use on all main mailboxes an is easy to use.
Trust this nice french company. ;)
Thursday, July 18, 2019
Thursday, July 4, 2019
|| operator, a coalesce like in Javascript
When I first encounter coalesce in SQL, I was amazed by the simplicity and the usefullness of the function. So I searched for it in other languages.
The closest I get in Javascript was to use the || operator. Using it on multiple operand will result in taking the first non null, non undefined, non false variable. See done below:
The closest I get in Javascript was to use the || operator. Using it on multiple operand will result in taking the first non null, non undefined, non false variable. See done below:
const a = null; const b = false; const c = 8; a || b || c; 8 const d = true; a || d || c; true const e = undefined; a || e || c; 8
Tuesday, July 2, 2019
Remove leading and trailing spaces on Strings across your spring application with Jackson
If you are using Jackson to convert http messages, you can add a custom String Deserializer to removes heading and trailing spaces across you application in a few lines.
import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.deser.std.StdDeserializer; import org.apache.commons.lang3.StringUtils; import java.io.IOException; public class StringDeserializer extends StdDeserializer<string> { private static final long serialVersionUID = 1623333240815834335L; public StringDeserializer() { this(null); } private StringDeserializer(Class vc) { super(vc); } @Override public String deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { String valueAsString = jp.getValueAsString(); if (StringUtils.isEmpty(valueAsString)) { return null; } return valueAsString.trim(); } }To activate it, add this bean to your WebConfig file.
@Configuration @EnableWebMvc public class WebConfig implements WebMvcConfigurer { @Bean public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() { MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter(); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); objectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY); jsonConverter.setObjectMapper(objectMapper); SimpleModule module = new SimpleModule(); module.addDeserializer(String.class, new StringDeserializer()); objectMapper.registerModule(module); return jsonConverter; } }
Angular app: Add unit test coverage on Sonar
We will consider here that you already have an angular application running with some unit tests based on karma / jasmine.
To see the coverage, we will need a few things :
We will also need to find a launcher (Chrome, PhantomJs ...). We chose the second one in order not to rely on the plateform.
First things first, we will update our karma.conf.js
The karma sonarqube Reporter will generate unitTestsReport.xml which contains unit tests information (number, success, fail, elapsed time ...).
(If you want to generate html report immediatly in your workspace, you can use Karma Html Reporter).
You can now launch tests using angular-cli
$ ng test --code-coverage
02 07 2019 13:45:10.192:INFO [karma-server]: Karma v3.1.3 server started at http://0.0.0.0:9876/
02 07 2019 13:45:10.195:INFO [launcher]: Launching browsers PhantomJS with concurrency unlimited
02 07 2019 13:45:10.201:INFO [launcher]: Starting browser PhantomJS
02 07 2019 13:45:18.845:INFO [PhantomJS 2.1.1 (Windows 7.0.0)]: Connected on socket DAOh28181XJnSgGxAAAA with id 31414509
TOTAL: 21 SUCCESS
If you are using typescript, you will probably need to set the property "sourceMap" to true in the "test" section of your angular.json file. Without it, when compiling from TypeScript to JavaScript, you will get lcov error because the report contains the line numbers for the JavaScript code. (See here).
You might also encounter some problems with PhantomJs. I had those weird errors although it was working fine with Chrome.
phantomjs Attempting to configure 'style' with descriptor '{"enumerable":true,"configurable":true}'
evaluating 'b.style._clear(a.propertyName(c)) phantomjs
I had to edit the polyfill.js (use to make your application compatible on multiple browsers).
Switching the import of web-animations-js and zone.js was the solution.
It is now time to tell sonar how to handle these informations. Add a file named sonar-project.properties to your workspace.
sonar.projectKey=project-key
sonar.projectName=project-name
sonar.sources=src
sonar.tests=src/app
sonar.exclusions=**/node_modules/**,**/*.spec.ts,**/coverage/**
sonar.coverage.exclusions=src/*.ts, src/*.js, **/*Dto.ts,**/*Enum.ts,**/*constants.ts, **/*module.ts, **/*config.ts
sonar.test.inclusions=**/*.spec.ts
sonar.typescript.lcov.reportPaths=coverage/lcov.info
sonar.testExecutionReportPaths=reports/unitTestsReport.xml
If you want to run test before pushing, you can use Husky and add test on pre-push.
"husky": {
"hooks": {
"pre-push": "ng test"
}
}
To see the coverage, we will need a few things :
npm install karma-coverage-istanbul-reporter --save-dev
npm install karma-sonarqube-reporter --save-dev
First things first, we will update our karma.conf.js
module.exports = function(config) { config.set({ basePath: '', frameworks: ['jasmine', '@angular-devkit/build-angular'], plugins: [ require('@angular-devkit/build-angular/plugins/karma'), require('karma-jasmine'), require('karma-phantomjs-launcher'), require('karma-coverage-istanbul-reporter'), require('karma-sonarqube-reporter') ], coverageIstanbulReporter: { dir: require('path').join(__dirname, '../coverage'), reports: ['lcovonly'], fixWebpackSourcePaths: true }, sonarqubeReporter : { basePath: 'src/app', filePattern: '**/*spec.ts', encoding: 'utf-8', outputFolder: 'reports', reportName: () => { return 'unitTestsReport.xml'; } }, reporters: ['progress', 'sonarqube'], port: 9876, logLevel: config.LOG_INFO, browsers: ['PhantomJS'], singleRun: true }); };The karma coverage istanbul reporter will generate the lcov.info file which contains code coverage information in an LCOV formatted file.
The karma sonarqube Reporter will generate unitTestsReport.xml which contains unit tests information (number, success, fail, elapsed time ...).
(If you want to generate html report immediatly in your workspace, you can use Karma Html Reporter).
You can now launch tests using angular-cli
$ ng test --code-coverage
02 07 2019 13:45:10.192:INFO [karma-server]: Karma v3.1.3 server started at http://0.0.0.0:9876/
02 07 2019 13:45:10.195:INFO [launcher]: Launching browsers PhantomJS with concurrency unlimited
02 07 2019 13:45:10.201:INFO [launcher]: Starting browser PhantomJS
02 07 2019 13:45:18.845:INFO [PhantomJS 2.1.1 (Windows 7.0.0)]: Connected on socket DAOh28181XJnSgGxAAAA with id 31414509
TOTAL: 21 SUCCESS
If you are using typescript, you will probably need to set the property "sourceMap" to true in the "test" section of your angular.json file. Without it, when compiling from TypeScript to JavaScript, you will get lcov error because the report contains the line numbers for the JavaScript code. (See here).
You might also encounter some problems with PhantomJs. I had those weird errors although it was working fine with Chrome.
phantomjs Attempting to configure 'style' with descriptor '{"enumerable":true,"configurable":true}'
evaluating 'b.style._clear(a.propertyName(c)) phantomjs
I had to edit the polyfill.js (use to make your application compatible on multiple browsers).
Switching the import of web-animations-js and zone.js was the solution.
It is now time to tell sonar how to handle these informations. Add a file named sonar-project.properties to your workspace.
sonar.projectKey=project-key
sonar.projectName=project-name
sonar.sources=src
sonar.tests=src/app
sonar.exclusions=**/node_modules/**,**/*.spec.ts,**/coverage/**
sonar.coverage.exclusions=src/*.ts, src/*.js, **/*Dto.ts,**/*Enum.ts,**/*constants.ts, **/*module.ts, **/*config.ts
sonar.test.inclusions=**/*.spec.ts
sonar.typescript.lcov.reportPaths=coverage/lcov.info
sonar.testExecutionReportPaths=reports/unitTestsReport.xml
To watch the result on the sonar server, run it in command line (path : sonarQubePath/bin/yourOs). It will be accessible on http:localhost:9000
Add the SonarQubeScanner to your path and then run the command sonar-scanner in the base directory of your project. It will find the sonar-project.properties and send information to the sonarQube server.
Now you can find your coverage information !
If you want to run test before pushing, you can use Husky and add test on pre-push.
"husky": {
"hooks": {
"pre-push": "ng test"
}
}
Labels:
JASMINE,
JAVASCRIPT,
JENKINS,
KARMA,
SONAR,
TYPESCRIPT
Subscribe to:
Posts (Atom)