Skip to content

Instantly share code, notes, and snippets.

import java.math.BigInteger;
import java.util.Collections;
import java.util.UUID;
import java.util.stream.Stream;
public class ShorterUuid {
private static void doit() {
String original = UUID.randomUUID().toString();
String hex = original.replaceAll("-", "");
String s = new BigInteger(hex, 16).toString(36);

Mocking HttpClient calls in Angular 2 when using Protractor

I just started writing an app using Spring Boot/Angular 2 combo. Once I had the initial demo up and working and cracked wiring in Spring security (another blog entirely), my attention turned to writing some tests that would drive the next phases.

My Spring Boot back-end can be unit/integration tested on it's own. I was interested in making sure the user experience was tested. Tradionally I might have turned to Geb for a full server side project, but this is mostly just javascript and I know how fast Jasmine/Karma tests run, so there must be a nice way of doing this?

Indeed I was not disappointed to find that Angular ng new webapp had already created me a e2e folder with a skeleton Protractor test to point the way.

But how to stub out the back-end? I have used many mocking frameworks before; Sinon, for example, is excellent in unit tests. There must by a simple way of doing it for Protractor/An

@jamescookie
jamescookie / pipeline.html
Created June 21, 2017 12:21
Jenkins pipeline view
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Pipeline Status</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.7/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment.js"></script>
<link rel="stylesheet" href="/plugin/build-monitor-plugin/styles/normalize.css">
@jamescookie
jamescookie / setJavaHome.zsh
Last active September 19, 2020 19:37
To switch JAVA_HOME in the current YADR zsh shell on a mac put this file in .zsh.after
function setJavaHome
{
javaVersions=($(/usr/libexec/java_home -V 2>&1 | grep x86 | awk -F ":" '{print $1}' | awk -F "," '{print $1}' | xargs echo -n))
newJava=""
while [[ $newJava = "" ]]; do
echo "Select your Java version for this shell"
select newJava in ${javaVersions[@]}; do
if [[ $newJava = "" ]]; then
echo "Please enter a valid option."
@jamescookie
jamescookie / index.html
Last active October 20, 2015 22:54
Docker monitor web ui
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Docker Monitor</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.7/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.14.2/ui-bootstrap-tpls.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.css"/>
@jamescookie
jamescookie / DockerSpringBootLoggingUsingSyslogAndLogstash.md
Last active May 28, 2024 17:22
Centralised logging for Docker containers running Spring Boot applications

Centralised logging for Docker containers running Spring Boot applications

Log flow through the system

Log flow

About

We have a bunch of Spring Boot apps running in various Docker containers and wanted a centralised place to view the logs

import java.util.Random
Random rand = new Random()
def colours = ['blue', 'green', 'red', 'yellow']
def likihood = [blue: 25, green: 22, red: 22, yellow: 5]
def views = [blue: 1, green: 1, red: 1, yellow: 1]
def clicks = [blue: 1, green: 1, red: 1, yellow: 1]
def percentages
for (def i = 0; i < 10000; i++) {
@jamescookie
jamescookie / UnitTestingVelocityAndJavaScriptInteractionsWithMocha.md
Last active August 29, 2015 14:11
Unit testing Velocity and JavaScript interactions with Mocha

Unit testing Velocity and JavaScript interactions with Mocha

Here's how we were able to reduce our functional testing and greatly speed up our build times by generating fully rendered [Velocity] (http://velocity.apache.org/) templates on demand and testing their interaction with our JavaScript.

Why bother?

The conventional wisdom is that unit testing views is hard, then therefore best left to functional tests, but these tests tend to be slow and not really designed for testing error scenarios/multiple cases/etc. In our case unit testing the JavaScript without the views would be largly pointless as most of the logic is directly linked to elements on the page. We could have just had some fixture data that emulated our views, but then you get into a maintenance nightmare trying to keep them both in sync. The ideal solution would be to test against the real views.

Why Mocha not Jasmine?

@jamescookie
jamescookie / fizzbuzz.groovy
Last active August 29, 2015 13:55
Obfuscated fizzbuzz in groovy
def l=_=1,t,u,v;char p,e,d,a,r=y=100;_=++l++;r-=l;_*=++l;a=r+(l*_)+1;p=y-l+--_;(e,d)=[p+l,a+_];t=/$d$d/;u=''+p+e+t;v=''+r+a+t;for(O in 1..y){println((1>(O%_)&&(O%l)>0)?u:(1>(O%l)&&1>(O%_))?u+v:(1>(O%l)&&(O%_)>0)?v:O)}
//or in as few characters as possible:
//for(i in 1..100){println "${i%3?'':'Fizz'}${i%5?'':'Buzz'}"?:i}