Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java Project with Travis CI integration #13

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Gradle Application with CI to Travis CI
# Introduction

This project uses gradle build tool to complete a set of questions aimed at teaching fundamentals of OOP Java, Junit 4 testing, Mockito, and CI pipelines using Travis CI

# Question Implemented in this code base
Turntabl has a number of clients who pay us to write code for them. We want to keep a register of clients so we can contact them.

There are two types of Client: Corporate and Private. Every client has a name, an ID, and a Service Level.

The Service Level is either Gold, Platinum, or Premium.

A Corporate Client has an Account Manager.
A Private Client is just a person.

Turntabl wants to get the Contact Name of a Client. In the case of a Corporate Client, that's the name of the Account Manager. In the case of a Private Client, that's the Client's name.

The Client Register should be initialised with a collection of both Corporate and Private Clients.

Model this scenario appropriately. Ensure you have appropriate tests for your model.
7. Using the Register
Extend your register to accommodate the following requirements.

1. I would like to get a list of the Contact Names of all the Gold clients.

2. I would like to get a Client's name by their ID. This should take into account the fact that there may be no Client with that ID.

3. I would like a Count of all clients at every Service Level. For example, there may be 5 Gold clients, 6 Platinum clients, and 2 Premium clients.
12 changes: 7 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ repositories {
}

dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
implementation 'org.mockito:mockito-all:1.10.19'
testImplementation group: 'junit', name: 'junit', version: '4.12'
// implementation group: 'org.mockito', name: 'junit', version: '4.12'

application {
mainClassName = 'io.turntabl.TCMS'
}
test {
useJUnit()
}

//this will build a start script in
Expand All @@ -31,5 +33,5 @@ tasks.startScripts {

//put your package-qualified main class name here
application {
mainClassName = 'io.turntabl.TCMS'
mainClass.set("io.turntabl.TCMS")
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.3-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
20 changes: 20 additions & 0 deletions src/main/java/io/turntabl/domain/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.turntabl.domain;

public abstract class Client {
private String ID;
private ServiceLevel serviceLevel;

public Client( String ID, ServiceLevel serviceLevel) {
this.ID = ID;
this.serviceLevel = serviceLevel;
}
public abstract String getName();

public ServiceLevel getServiceLevel() {
return serviceLevel;
}

public String getID() {
return ID;
}
}
11 changes: 11 additions & 0 deletions src/main/java/io/turntabl/domain/ClientRegister.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.turntabl.domain;

import java.util.List;

public class ClientRegister {
private List<Client> clients;

public ClientRegister(List<Client> clients) {
this.clients = clients;
}
}
43 changes: 43 additions & 0 deletions src/main/java/io/turntabl/domain/CorporateClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.turntabl.domain;

public class CorporateClient extends Client{
private final CorporateClient.AccountManager accountManager;

public CorporateClient(String name, String ID, ServiceLevel serviceLevel) {
super(ID + "COP", serviceLevel);
accountManager = new AccountManager(name);
}



@Override
public String getName() {
return accountManager.getName();
}

@Override
public String toString() {
return "CorporateClient{" +
"accountManager=" + accountManager +
'}';
}

static class AccountManager {
private final String name;

public AccountManager(String name) {
this.name = name;
}

public String getName() {
return name;
}

@Override
public String toString() {
return "AccountManager{" +
"name='" + name + '\'' +
'}';
}
}
}
21 changes: 21 additions & 0 deletions src/main/java/io/turntabl/domain/PrivateClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.turntabl.domain;

public class PrivateClient extends Client{
private final String name;
public PrivateClient(String name, String ID, ServiceLevel serviceLevel) {
super(ID + "PVT", serviceLevel);
this.name = name;
}

@Override
public String getName() {
return this.name;
}

@Override
public String toString() {
return "PrivateClient{" +
"name='" + name + '\'' +
'}';
}
}
7 changes: 7 additions & 0 deletions src/main/java/io/turntabl/domain/ServiceLevel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.turntabl.domain;

public enum ServiceLevel {
GOLD,
PLATINUM,
PREMIUM
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.turntabl.exceptions;

public class ClientNotFoundException extends Exception{
public ClientNotFoundException() {
super("Client not found");
}
}
15 changes: 15 additions & 0 deletions src/main/java/io/turntabl/service/RegisterService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.turntabl.service;

import io.turntabl.domain.ServiceLevel;
import io.turntabl.exceptions.ClientNotFoundException;

import javax.swing.text.html.Option;
import java.util.List;
import java.util.Map;
import java.util.Optional;

public interface RegisterService {
List<String> getContactNames(ServiceLevel serviceLevel);
String getClientNameById(String clientId) throws ClientNotFoundException;
Map<ServiceLevel, Long> countClientsPerServiceLevel();
}
66 changes: 66 additions & 0 deletions src/main/java/io/turntabl/service/impl/RegisterServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package io.turntabl.service.impl;

import io.turntabl.domain.Client;
import io.turntabl.domain.ServiceLevel;
import io.turntabl.exceptions.ClientNotFoundException;
import io.turntabl.service.RegisterService;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

public class RegisterServiceImpl implements RegisterService {
private List<Client> clients;
private final RegisterService registerService;
//
// This is for the normal tests.
// public RegisterServiceImpl(List<Client> clients) {
// this.clients = clients;
// }
// This helps with mockito
public RegisterServiceImpl(RegisterService registerService) {
this.registerService = registerService;
}

@Override
public List<String> getContactNames(ServiceLevel serviceLevel) {
return this.clients
.stream()
.filter(client -> client.getServiceLevel() == serviceLevel)
.map(Client::getName)
.collect(Collectors.toList());
}

@Override
public String getClientNameById(String clientId) throws ClientNotFoundException {
return this.clients
.stream()
.filter(client -> client.getID().equalsIgnoreCase(clientId))
.findFirst()
.map(Client::getName)
.orElseThrow(ClientNotFoundException::new);
}

@Override
public Map<ServiceLevel, Long> countClientsPerServiceLevel() {


return this.clients
.stream()
.collect(Collectors.groupingBy(Client::getServiceLevel, Collectors.counting()));


}

@Override
public String toString() {
return "RegisterServiceImpl{" +
"clients=" + clients +
'}';
}

public void setClients(List<Client> clients) {
this.clients = clients;
}
}
19 changes: 19 additions & 0 deletions src/test/java/ClientTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import io.turntabl.domain.Client;
import io.turntabl.domain.CorporateClient;
import io.turntabl.domain.PrivateClient;
import io.turntabl.domain.ServiceLevel;
import org.junit.Assert;
import org.junit.Test;

import static org.junit.Assert.*;

public class ClientTest {

@Test
public void getClientName() {
Client privateClient = new PrivateClient("Mickey D", "12", ServiceLevel.GOLD);
Client coporateClient = new CorporateClient("Ghapoha", "122", ServiceLevel.PLATINUM);
assertEquals("Mickey D", privateClient.getName());
assertEquals("Ghapoha", coporateClient.getName());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
package io.turntabl;

import io.turntabl.Numbers;
import org.junit.Test;

import static org.junit.Assert.*;
Expand All @@ -8,24 +7,31 @@ public class NumbersTest {

@Test
public void testLessThanZero() {
System.out.println("Running the first test");
boolean result = Numbers.isGreaterThanZeroAndLessThanAThousand(-1);
assertFalse(result);
}

@Test
public void testZero() {
System.out.println("==============Test Zero===========");

boolean result = Numbers.isGreaterThanZeroAndLessThanAThousand(0);
assertFalse(result);
}

@Test
public void testGreaterThanZero() {
System.out.println("==============testGreaterThanZero===========");

boolean result = Numbers.isGreaterThanZeroAndLessThanAThousand(1);
assertTrue(result);
}

@Test
public void testLessThanAThousand() {
System.out.println("==============testLessThanAThousand===========");

boolean result = Numbers.isGreaterThanZeroAndLessThanAThousand(999);
assertTrue(result);
}
Expand Down
75 changes: 75 additions & 0 deletions src/test/java/RegisterServiceImplTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import io.turntabl.domain.Client;
import io.turntabl.domain.CorporateClient;
import io.turntabl.domain.PrivateClient;
import io.turntabl.domain.ServiceLevel;
import io.turntabl.exceptions.ClientNotFoundException;
import io.turntabl.service.RegisterService;
import io.turntabl.service.impl.RegisterServiceImpl;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;

@RunWith(MockitoJUnitRunner.class)
public class RegisterServiceImplTest {
@Mock
private RegisterService registerService;
private RegisterServiceImpl registerServiceImpl;

@Before
public void init() {
Client coporateClient = new CorporateClient("ECG", "2332", ServiceLevel.GOLD);
Client coporateClient2 = new CorporateClient("FIDELITY BANK", "2332", ServiceLevel.GOLD);
Client privateClientDee = new PrivateClient("Mickey D Luffy", "233", ServiceLevel.PLATINUM);
Client privateClientJhey = new PrivateClient("Jhey", "2348", ServiceLevel.PLATINUM);
Client privateClientHancok = new PrivateClient("Boa Hancok", "78", ServiceLevel.PLATINUM);

Client privateClientMikey = new PrivateClient("Mike", "23", ServiceLevel.PREMIUM);
Client privateClientMikey2 = new PrivateClient("Restre", "34", ServiceLevel.PREMIUM);
registerServiceImpl = new RegisterServiceImpl(registerService);
registerServiceImpl.setClients(List.of(coporateClient,privateClientMikey,privateClientMikey2, coporateClient2, privateClientDee,privateClientJhey,privateClientHancok));
// registerServiceImpl =
// new RegisterServiceImpl(List.of(coporateClient,privateClientMikey,privateClientMikey2, coporateClient2, privateClientDee,privateClientJhey,privateClientHancok));

}
@Test
public void getContactNames() {
when(registerService.getContactNames(ServiceLevel.GOLD))
.thenReturn(List.of("ECG", "FIDELITY BANK"));
assertEquals(registerServiceImpl.getContactNames(ServiceLevel.GOLD), List.of("ECG", "FIDELITY BANK"));
// assertEquals("Only ECG and FIDELITY clients belong to the gold Service",
// registerServiceImpl.getContactNames(ServiceLevel.GOLD), List.of("ECG", "FIDELITY BANK"));

}

@Test
public void getClientNameById() throws ClientNotFoundException {
when(registerService.getClientNameById("2332COP")).thenReturn("ECG");
assertEquals(registerServiceImpl.getClientNameById("2332COP"), "ECG");
}

@Test(expected = ClientNotFoundException.class)
public void getClientNameByIdThrows() throws ClientNotFoundException {
registerServiceImpl.getClientNameById("2332d");
when(registerService.getClientNameById("2332d"))
.thenThrow(new ClientNotFoundException());
// assert(registerServiceImpl.getClientNameById("2332"), "ECG");
}


@Test
public void countClientsPerServiceLevel() {
Map<ServiceLevel, Long> countPerLevel = Map.of(ServiceLevel.GOLD, 2L, ServiceLevel.PLATINUM, 3L, ServiceLevel.PREMIUM, 2L);
assertEquals(registerServiceImpl.countClientsPerServiceLevel(), countPerLevel);
registerServiceImpl.countClientsPerServiceLevel();
}

}
Loading