Syed Sayem blog

Learning with Treehouse

Treehouse is my code learning platform of choice. There are other online training solutions available like Pluralsight, Infinite Skills, Codecademy and Lynda.com, but I liked Treehouse better because their gamification keeps me motivated to learn more. You earn more points and achievements the further along you go.

My Treehouse profile first-screenshot

Treehouse has a nice UI and slight gamification which lets you score points in different skill sets and collect achievement badges.

second-screenshot

Overall, I highly recommend Treehouse. They’ve been a huge help for me, the courses are enjoyable.

Microservice testing

Microservice are hot buzz words in the development community right now. There are quite a lot of learning materials of microservices, but there are very few resources on how to test microservices. My company is in the process implementing ‘microservices’, and I’ve been asked to build a test automation framework to test microservice.

In this post, I’ll demonstrate how to write a basic hello server and hello client to test microservices.

first-screenshot

  1. Create a maven project
  2. Add the following dependency to your pom.xml:
    <dependencyManagement>

        <dependencies>
            <!-- Logging -->
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>${slf4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-simple</artifactId>
                <version>${slf4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-jdk14</artifactId>
                <version>${slf4j.version}</version>
            </dependency>
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
                <version>1.1.2</version>
            </dependency>
            <!-- No commons logging -->
            <dependency>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
                <version>${commons-logging.version}</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>jcl-over-slf4j</artifactId>
                <version>${slf4j.version}</version>
            </dependency>

            <!-- Protobuf -->
            <dependency>
                <groupId>com.google.protobuf</groupId>
                <artifactId>protobuf-java</artifactId>
                <version>${protobuf.version}</version>
            </dependency>

            <!-- GRPC -->
            <dependency>
                <groupId>io.grpc</groupId>
                <artifactId>grpc-core</artifactId>
                <version>${grpc.version}</version>
            </dependency>
            <dependency>
                <groupId>io.grpc</groupId>
                <artifactId>grpc-stub</artifactId>
                <version>${grpc.version}</version>
            </dependency>
            <dependency>
                <groupId>io.grpc</groupId>
                <artifactId>grpc-protobuf</artifactId>
                <version>${grpc.version}</version>
            </dependency>
            <dependency>
                <groupId>io.grpc</groupId>
                <artifactId>grpc-netty</artifactId>
                <version>${grpc.version}</version>
            </dependency>
            <dependency>
                <groupId>io.grpc</groupId>
                <artifactId>grpc-okhttp</artifactId>
                <version>${grpc.version}</version>
            </dependency>

            <dependency>
                <groupId>io.grpc</groupId>
                <artifactId>protoc-gen-grpc-java</artifactId>
                <version>${grpc.version}</version>
            </dependency>

            <!-- Test scope -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.8.2</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.mockito</groupId>
                <artifactId>mockito-all</artifactId>
                <version>1.9.0</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

Create a hello.proto

syntax = "proto3";

package sayem.hello;

option java_package = "org.sayem.hello";

option java_outer_classname = "HelloModel";

//
// Request / Response
//

message GreetingRequest {
    string name = 1;
}

message GreetingReply {
    string greeting = 2;
}

//
// Service
//

service HelloService {
    rpc GetGreeting (GreetingRequest) returns (GreetingReply) {
    }
}

Create a HelloClient Module

public final class HelloClient {

    private final Logger log = LoggerFactory.getLogger(getClass());
    private final ManagedChannel channel;
    private final HelloServiceGrpc.HelloServiceBlockingClient blockingStub;

    /**
     * Construct client connecting to HelloWorld server at {@code host:port}.
     */
    public HelloClient(String host, int port) {
        channel = ManagedChannelBuilder.forAddress(host, port)
                .usePlaintext(true) // - for debug only
                .build();
        blockingStub = HelloServiceGrpc.newBlockingStub(channel);
    }

    public static void main(String[] args) throws InterruptedException {
        HelloClient client = new HelloClient("localhost", 8081);
        try {
      /* Access a service running on the local machine on port 50051 */
            String user = "world";
            if (args.length > 0) {
                user = args[0]; /* Use the arg as the name to greet if provided */
            }
            client.greet(user);

            String user2 = "GRPC client";
            if (args.length > 1) {
                user2 = args[1]; /* Use the arg as the name to greet if provided */
            }
            client.greet(user2);
        } finally {
            client.shutdown();
        }
    }

    public void shutdown() throws InterruptedException {
        channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
    }

    /**
     * Say hello to server.
     */
    public void greet(String name) {
        log.info("Will try to greet {} ...", name);
        HelloModel.GreetingRequest request = HelloModel.GreetingRequest.newBuilder()
                .setName(name)
                .build();
        HelloModel.GreetingReply response;
        try {
            response = blockingStub.getGreeting(request);
        } catch (StatusRuntimeException e) {
            log.warn("RPC failed: {}", e.getStatus());
            return;
        }
        log.info("Greeting: {}", response.getGreeting());
    }
}

Now, create a client module

public final class HelloClient {

    private final Logger log = LoggerFactory.getLogger(getClass());
    private final ManagedChannel channel;
    private final HelloServiceGrpc.HelloServiceBlockingClient blockingStub;

    /**
     * Construct client connecting to HelloWorld server at {@code host:port}.
     */
    public HelloClient(String host, int port) {
        channel = ManagedChannelBuilder.forAddress(host, port)
                .usePlaintext(true) // - for debug only
                .build();
        blockingStub = HelloServiceGrpc.newBlockingStub(channel);
    }

    public static void main(String[] args) throws InterruptedException {
        HelloClient client = new HelloClient("localhost", 8081);
        try {
      /* Access a service running on the local machine on port 50051 */
            String user = "world";
            if (args.length > 0) {
                user = args[0]; /* Use the arg as the name to greet if provided */
            }
            client.greet(user);

            String user2 = "GRPC client";
            if (args.length > 1) {
                user2 = args[1]; /* Use the arg as the name to greet if provided */
            }
            client.greet(user2);
        } finally {
            client.shutdown();
        }
    }

    public void shutdown() throws InterruptedException {
        channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
    }

    /**
     * Say hello to server.
     */
    public void greet(String name) {
        log.info("Will try to greet {} ...", name);
        HelloModel.GreetingRequest request = HelloModel.GreetingRequest.newBuilder()
                .setName(name)
                .build();
        HelloModel.GreetingReply response;
        try {
            response = blockingStub.getGreeting(request);
        } catch (StatusRuntimeException e) {
            log.warn("RPC failed: {}", e.getStatus());
            return;
        }
        log.info("Greeting: {}", response.getGreeting());
    }
}

You can see the full source code in this repository

Setup printer in Arch Linux

Recently I wrote a guide on how to setup Arch Linux on your PC. Today I am going to show you how to setup printer in Arch Linux.

In order to get printing working in Arch Linux, you have to install the following libraries:

$ yaourt -S libcups
$ pacman -S cups cups-filters ghostscript gsfonts

Now, Install printer driver

I have a Brother HL-2270DW series. So, I am going to install Brother HL-2270DW driver. You can search for printer driver in Aur Package

$ yaourt -S brother-hl2270dw

Enable cups so it starts with system boot

$ systemctl enable org.cups.cupsd.service
$ systemctl daemon-reload

Start CUPS

$ systemctl start org.cups.cupsd.service

Open the CUPS interface from this URL (http://localhost:631/admin) and log in as root. Here you can add and configure your printer.

Audiobook

Over the last few months, I have increasingly been buying and downloading audiobook from audible.com and listen to them on my commute to and from work, here are some books I think worth reading.

Developer

Self-Help

Fiction

Java Generic Builder Pattern

Implementing the Builder pattern in Java without repeating code

public class App {

    public static void main(String[] args) {

        Person value = GenericBuilder.of(Person::new)
                .with(Person::setName, "Syed")
                .with(Person::setAge, 50)
                .build();

        System.out.println(value.getAge());
        System.out.println(value.getName());
    }
}
public class GenericBuilder<T> {

    private final Supplier<T> instantiator;

    private List<Consumer<T>> instanceModifiers = new ArrayList<>();

    public GenericBuilder(Supplier<T> instantiator) {
        this.instantiator = instantiator;
    }

    public static <T> GenericBuilder<T> of(Supplier<T> instantiator) {
        return new GenericBuilder<T>(instantiator);
    }

    public <U> GenericBuilder<T> with(BiConsumer<T, U> consumer, U value) {
        Consumer<T> c = instance -> consumer.accept(instance, value);
        instanceModifiers.add(c);
        return this;
    }

    public T build() {
        T value = instantiator.get();
        instanceModifiers.forEach(modifier -> modifier.accept(value));
        instanceModifiers.clear();
        return value;
    }
}
public class Person {

    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}