react testing
Posted: June 21, 2017 Filed under: programing | Tags: enzyme, jest, reactjs, sonar, sonarcloud.io, SonarQube, tests, travisci Leave a commentI have to say that hardest part with react was to figure out testing. (testing components is easy with thousands examples). But how to test props, state vlaues, clicking events, helping functions,… There are many projects in github and almost all of them are without any tests.
I have put together small app, where you can find code with tests.
I have tested:
* components
* props values
* state values
* events on components like clicking,…
* functions
Frankly I haven’t seen any project where they would cover all of the above.
I am not gonna write step by step, just small info I am using Jest & Enzyme testing framework.
For details better check my code. Check for package.json for packages I am using and __tests__ folders for tests.
Here are:
* github link
* travis link
* sonarcloud.io link
enjoy
node/react -> github -> travis -> sonar
Posted: June 16, 2017 Filed under: programing | Tags: node, reactjs, sonar, sonarcloud.io, SonarQube, travisci Leave a commentWhen programming I like to keep my code as clean and automated as possible.
I like sonar (does analysis on your code and provide you output how many duplicate, code smells, unused variables,..)
I like node.js / react.js for its quick prototyping and agile and others advantages. So lastly I was looking if I can have setup where I could automate my infrastructure code -> push to VCS (github) -> CI (travis) -> code analysis (Sonar).
Here are steps what you need to do:
- github.com -> on project settings turn on “travis-ci” integration
- github.com -> project -> settings -> Integration & services -> Add Service -> Travis CI
- travis-ci.com -> make sure after code push code build is triggered
- how to setup travis for sonar
- only thing I did in travis was to define SONAR_TOKEN variable (see above for details)
- sonarcloud.io – get started
- project:
.travis.yml
dist: trusty
sudo: required
addons:
sonarqube:
organization: "czechsource"
branches:
- master
- develop
jdk:
- oraclejdk8
script:
- sonar-scanner
cache:
directories:
- '$HOME/.sonar/cache'
sonar-project.properties
sonar.projectKey=czechsource:reactBlog
sonar.projectName=React.js blog
sonar.projectVersion=1.0
sonar.sources=src
sonar.language=js
sonar.sourceEncoding=UTF-8
sonar.branch=develop,master
Make sure you define sonacloud.io organization and token and use it in travis.
Update: I am using git flow and find out that branches “develop” were not sent to sonar. Make sure you add them in .travis.yml as well as in sonar-project.properties files (marked them in bold)
Update2: there is another option using sonarqube-scanner-node project . (I am assuming your project is made by create-react-app by facebook)
yarn add sonarqube-scanner-node --dev
yarn test -- --coverage
- package.json -> add in script section:
"sonarqube": "sonarqube-scanner-node"
- from command line:
yarn sonarqube -Dsonar.host.url=https://sonarcloud.io -Dsonar.organization=chowanioksource -Dsonar.sources=src -Dsonar.projectKey=chowanioksource:reactBlog -Dsonar.login=<sonarcloud.io token>
unable to find valid certification path to requested target
Posted: March 13, 2014 Filed under: java, Linux, programing | Tags: certificate, InstallCert.java, java, jenkins, jssecacerts, keystore, sonar, SonarQube, ssl Leave a commentIt is always preferred to use https instead of http (specially when using passwords and so on…)
We have switched our SonarQube (tool for Continuous Inspection of code quality) to use https for security reasons. Anyway I have noticed that Jenkins stop sending new quality codes to our sonar. When I have checked the logs I have seen this stacktrace:
Exception in thread "main" java.lang.IllegalStateException: Fail to request server version at org.sonar.runner.Bootstrapper.getServerVersion(Bootstrapper.java:73) at org.sonar.runner.Runner.checkSonarVersion(Runner.java:220) at org.sonar.runner.Runner.execute(Runner.java:150) at org.sonar.runner.Main.execute(Main.java:84) at org.sonar.runner.Main.main(Main.java:56) Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:150) at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1584) at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.java:174) at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.java:168) at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:848) at com.sun.net.ssl.internal.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:106) at com.sun.net.ssl.internal.ssl.Handshaker.processLoop(Handshaker.java:495) at com.sun.net.ssl.internal.ssl.Handshaker.process_record(Handshaker.java:433) at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:877) at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1089) at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1116) at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1100) at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:402) at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:166) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:951) at java.net.URLConnection.getContent(URLConnection.java:682) at sun.net.www.protocol.https.HttpsURLConnectionImpl.getContent(HttpsURLConnectionImpl.java:406) at org.sonar.runner.Bootstrapper.remoteContent(Bootstrapper.java:125) at org.sonar.runner.Bootstrapper.getServerVersion(Bootstrapper.java:71) ... 4 more
So here you can see that Jenkins has problem to “handshake” ssl certificate.
follow these steps:
Download InstallCert.java
Search google – it originally was done in Sun, but you can find this program on google codes or somewhere else. You can even download binaries of this file.
Add Trusted Keystore
Run “InstallCert.java” on server (where you run your https service). something like java InstallCert localhost:443 -> press “1” when asked. It will add your “localhost” as a trusted keystore, and generate a file named “jssecacerts“.
[user@sonar ~]$ java InstallCert localhost:443 Loading KeyStore /usr/java/jdk1.6.0_37/jre/lib/security/cacerts... Opening connection to localhost:443... Starting SSL handshake.. Server sent 1 certificate(s): 1 Subject CN=Unknown, OU=Unknown, O=Vendavo, L=Unknown, ST=Czech republic, C=CZ Enter certificate to add to trusted keystore or 'q' to quit: [1] 1 Added certificate to keystore 'jssecacerts' using alias 'localhost-1'
I have removed most of the parts but the main parts are here:
a) press 1 when assked – you agree to add certificate for this domain into keystore
b) it created jssecacerts file
Verify Trusted Keystore
Run same command again 🙂 (this is full export – removed hashed data)
[mchowaniok@sonar ~]$ java InstallCert sonar.vmcz.vendavo.com:443 Loading KeyStore jssecacerts... Opening connection to sonar.vmcz.vendavo.com:443... Starting SSL handshake... No errors, certificate is already trusted Server sent 1 certificate(s): 1 Subject CN=Unknown, OU=Unknown, O=Vendavo, L=Unknown, ST=Czech republic, C=CZ Issuer CN=Unknown, OU=Unknown, O=Vendavo, L=Unknown, ST=Czech republic, C=CZ sha1 md5 Enter certificate to add to trusted keystore or 'q' to quit: [1] q KeyStore not changed
Copy jssecacerts
copy jssecacerts file into java/jre/lib/security folder (I had to done it under sudo )
[user@sonar ~]$ sudo cp jssecacerts /usr/java/default/jre/lib/security/
Done
verify it 🙂 in my case, run Jenkins job and verify data are uploaded to Sonar.
SonarQube part #2 – let’s feed it with some data
Posted: December 7, 2013 Filed under: java, Linux, Mac, programing | Tags: maven, sonar, SonarQube, SonarQube Runner Leave a commentIn previous article, I have showed you how to build sonarqube 4.0 on openshift , but let’s feed it with some data.
- Let’s create dummy j2ee project using maven
-
mvn archetype:generate
- cd <<project_name>>
-
mvn clean compile
-
- openshift port forward to access database
-
rhc port-forward <<APPLICATION_NAME>>
- you should see something like this:
Service Local OpenShift
——- ————– —- —————
java 127.0.0.1:8080 => 127.X.X.X:8080
mysql 127.0.0.1:3306 => 127.X.X.X:3306 - database can be accessed on localhost:3306
-
Now we have few options how to feed sonar
- maven – best for maven projects
- sonarqube runner – best for java legacy code
- jenkins,….
Maven projects
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>eu.chowaniok</groupId> | |
<artifactId>j2eeApplication</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<packaging>jar</packaging> | |
<name>j2eeApplication</name> | |
<url>http://maven.apache.org</url> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
<sonar.language>java</sonar.language> | |
<sonar.host.url>http://127.0.0.1:8080</sonar.host.url> | |
<sonar.jdbc.url>jdbc:mysql://localhost:3306/APPLICATION_NAME?useUnicode=true&characterEncoding=utf8</sonar.jdbc.url> | |
<sonar.jdbc.username>mysql_username</sonar.jdbc.username> | |
<sonar.jdbc.password>mysql_password</sonar.jdbc.password> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>3.8.1</version> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
<build> | |
<pluginManagement> | |
<plugins> | |
<plugin> | |
<groupId>org.codehaus.mojo</groupId> | |
<artifactId>sonar-maven-plugin</artifactId> | |
<version>2.1</version> | |
</plugin> | |
</plugins> | |
</pluginManagement> | |
</build> | |
</project> |
5 things to change or consider:
- jdbc url – make sure you have correct url, port, application name
- mysql username
- mysql password
- sonar host url
- I am using maven version 3 – in case you have maven version 2 you have set different dependences
to run:
mvn clean compile sonar:sonar or (including profile name to be used) mvn clean compile sonar:sonar -Dsonar.profile="Sun checks"
SonarQube Runner
sonar.projectKey=eu.chowaniok:j2eeApplication | |
sonar.projectName=j2eeApplication | |
sonar.projectVersion=1.0-SNAPSHOT | |
sonar.sources=src/main/java | |
sonar.language=java | |
sonar.sourceEncoding=UTF-8 | |
sonar.binaries=target | |
sonar.host.url=http://127.0.0.1:8080 | |
sonar.jdbc.url=jdbc:mysql://localhost:3306/APPLICATION_NAME?useUnicode=true&characterEncoding=utf8 | |
sonar.jdbc.username=mysql_username | |
sonar.jdbc.password=mysql_password |
4 things to change or consider:
- jdbc url – make sure you have correct url, port, application name
- mysql username
- mysql password
- sonar host url
to run:
- you have to download SonarQube runner & set PATH variable
export SONAR_RUNNER_HOME=/Path/to/SonnarRunner/sonar-runner-2.3 export PATH=$SONAR_RUNNER_HOME/bin:$PATH
- create file called “sonar-project.properties” and fill it with info mentioned above in gist
- run:
sonar-runner
Now change code (i.e. add empty private method, named with upper case letter, ….) and run analysis again to feed sonar. Check sonar – it shows you all violations and issues in your code, … (also it depends on plugins you have installed on your sonar)
Example project can be found on Bitbucket
How to run SonarQube 4.0 on openshift
Posted: December 6, 2013 Filed under: Linux, Mac, programing | Tags: git, openshift, redhat, sonar, SonarQube 4 CommentsI have managed to run latest SonarQube on openshift for free.
Because openshift has bug you can’t just have one-line command to do all setup for you, but I had to separate it into several commands and two git repos.
Bug
you can’t have .openshift folder in repo – so I have to have 2 git repos
- git repo with sonar without (.openshift folder)
- git repo with .openshift folder with start & stop commands
How to get SonarQube 4.0 running on openshift
rhc create-app sonar diy-0.1 mysql-5.1 –from-code https://bitbucket.org/majecek/sonarqube-openshift.git | |
cd sonar | |
git remote add upstream -m master https://bitbucket.org/majecek/sonarqube-openshift_addons.git | |
git pull upstream master | |
git pull origin master | |
git push origin master |
Description
- when RedHat fixes the bug, you should be fine, just with line #1
- cd into project
- add another git repo which holds .openshift folder with start & stop commands
- get changes from repo above
- pull from origin repo – git was complaining when I didn’t do pull
- push into openshift
- wait several minutes until sonar gets running
Thanks
Big thanks goes to Rui Rodrigues(@rodriguesrmb) as he managed to solve port binding problems and update java wrapper with new version