Skip to content
Snippets Groups Projects
Commit 1ecacb73 authored by Appleton, Joseph Dr (Comp Sci & Elec Eng)'s avatar Appleton, Joseph Dr (Comp Sci & Elec Eng)
Browse files

initial commit

parent d0144208
No related branches found
No related tags found
No related merge requests found
Showing
with 562 additions and 0 deletions
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="target/generated-sources/annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
.project 0 → 100644
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>TravelAgency</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
<filteredResources>
<filter>
<id>1740642098171</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
eclipse.preferences.version=1
org.eclipse.jdt.apt.aptEnabled=false
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
org.eclipse.jdt.core.compiler.processAnnotations=disabled
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
pom.xml 0 → 100644
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.com1028.lab7</groupId>
<artifactId>TravelAgency</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>MyAgency</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.8.0</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package com.com1028.travelagency;
/**
*
* Defines the properties and behaviour for a Currency Converter class
*
* @author Mariam Cirovic
*
*/
public class CurrencyConverter {
CurrencyService service;
public CurrencyConverter() {
this.service = null;
}
// The CurrencyService is a dependency passed via a setter method
public void setCurrencyService(CurrencyService service) {
this.service = service;
}
/**
* Retrieve the new currency amount given a amount in currency 1 to be converted to currency 2
*
* @return currency amount
*/
public double convertCurrency(double amount, String currency1, String currency2) {
double newCurrencyAmount;
// Call the currency service to get the exchange rate between currencies 1 & 2
double exchangeRate = service.getExchangeRate(currency1, currency2);
newCurrencyAmount = amount * exchangeRate;
return newCurrencyAmount;
}
}
package com.com1028.travelagency;
/**
*
* Interface that defines the behaviour of a Currency Service
*
* @author Mariam Cirovic
*
*/
public interface CurrencyService {
// This method returns the exchange rate given the names of two currencies
public double getExchangeRate(String currency1, String currency2);
}
package com.com1028.travelagency;
import java.util.List;
/**
*
* Interface that defines the behaviour of an Excursion Service
*
* @author Mariam Cirovic
*
*/
public interface ExcursionService {
// This method returns the list of excursions that are provided for a given city
public List<String> excursionList(String city);
}
package com.com1028.travelagency;
import java.util.ArrayList;
import java.util.List;
/**
*
* Defines the properties and behaviour for a Trip Advisor class
* It provides weather info and available excursions for a given city
*
* @author Mariam Cirovic
*
*/
public class TripAdvisor {
ExcursionService excursionService;
WeatherService weatherService;
public TripAdvisor() {
excursionService = null;
weatherService = null;
}
// The excursionService is a dependency passed via a setter method
public void setExcursionService(ExcursionService excursionService) {
this.excursionService = excursionService;
}
public void setWeatherService(WeatherService weatherService) {
this.weatherService = weatherService;
}
/**
* Retrieve the excursions of a given type offered in a specified city
* e.g. beach, island, historic
*
* @return list of excursions
*/
public List<String> getExcursionList(String city, String type){
List<String> allExcursions = new ArrayList<String>();
// Call the excursion service to get all the excursions listed for a particular city
allExcursions = excursionService.excursionList(city);
List<String> chosenExcursions = new ArrayList<String>();
for(String excursion: allExcursions) {
//Checking to see if the excursion is of the type specifies e.g. beach
if (excursion.toLowerCase().contains(type.toLowerCase())){
chosenExcursions.add(excursion);
}
}
return chosenExcursions;
}
/**
* Retrieve the weather information for specified city in a specific month
*
* @return the weather info as a string
*/
public String weatherInfo(String city, Integer month) {
Integer temp = weatherService.predictedAverageTemperature(city, month);
Double rainprob = weatherService.predictedRainProbability(city, month);
String weather = null;
if (temp >= 21 && rainprob >= 0.5) {
weather = "Warm and Rainy";
} else if (temp >= 21 && rainprob < 0.5) {
weather = "Warm and Dry";
} else if (temp < 21 && rainprob < 0.5){
weather = "Cold and Dry";
} else {
weather = "Cold and Rainy";
}
return weather;
}
}
package com.com1028.travelagency;
import java.util.List;
/**
*
* Interface that defines the behaviour of a Trip Database Service
*
* @author Mariam Cirovic
*
*/
public interface TripDatabase {
// This method returns the total cost of a trip for a given customer ID
public double totalTripCost(String CustomerID);
// This method returns the list of cities to be visited in order by the customer given the ID
public List<String> cityItinerary(String CustomerID);
}
package com.com1028.travelagency;
import java.util.List;
/**
*
* Defines the properties and behaviour for TripManager
* to apply a discount to the total trip cost and get the itinerary
*
* @author Mariam Cirovic
*
*/
public class TripManager {
private TripDatabase tripDB;
/**
* Constructor sets the trip database
* TripDatabase dependency is passed via the constructor
* @param tripDB
* The database with details on Customers' Trips
*/
public TripManager(TripDatabase tripDB) {
this.tripDB = tripDB;
}
/**
* Retrieve the cost of a customers trip after applying a discount
*
* @return discounted cost
*/
public double applyDiscount(int percentageDiscount, String CustomerID) {
double cost = tripDB.totalTripCost(CustomerID);
return cost - (cost *percentageDiscount/100);
//return 1600.00;
}
/**
* Retrieve the itinerary of a customer's trip given the customer id
*
* @return a list of places a traveller will visit in order
*/
public List<String> getItinerary(String CustomerID){
return tripDB.cityItinerary(CustomerID);
}
}
package com.com1028.travelagency;
/**
*
* Interface that defines the behaviour of a Weather Service
*
* @author Mariam Cirovic
*
*/
public interface WeatherService {
// This method returns the predicted average temperature for a given city in a given month
// The month is expressed as a digit e.g. September = 9
public Integer predictedAverageTemperature(String city, Integer month);
// This method returns the predicted rain probability for a given city in a given month
// The month is expressed as a digit e.g. September = 9
public Double predictedRainProbability(String city, Integer month);
}
package com.com1028.travelagency;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
/**
*
* Tests that the CurencyConverter class is returning the correct exchange amount
* Annotations are used in these tests
*
* @author Mariam Cirovic
*
*/
@RunWith(MockitoJUnitRunner.class)
public class CurrencyConverterTest {
// @InjectMocks annotation is used to create and inject the mock object
@InjectMocks
CurrencyConverter converter = new CurrencyConverter();
// @Mock annotation is used to create the mock object to be injected
@Mock
CurrencyService service;
@Test
public void testConvertCurrency() {
// Method chain to return 1.12 as the exchange rate between pound and euro
when(service.getExchangeRate("pound", "euro")).thenReturn(1.12);
assertEquals(1120.0, converter.convertCurrency(1000.0, "pound", "euro"), 0);
verify(service).getExchangeRate("pound", "euro");
}
@Test(expected = RuntimeException.class)
public void testConvertCurrencyException() {
doThrow(new RuntimeException("ExchangeRate not implemented")).when(service.getExchangeRate("pound", "euro"));
assertEquals(1120.0, converter.convertCurrency(1000.0, "pound", "euro"), 0);
verify(service).getExchangeRate("pound", "euro");
}
}
\ No newline at end of file
package com.com1028.travelagency;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InOrder;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
/**
*
* Tests that the TripAdvisor class is retrieving an excursion list of the required type
* as well as retrieving the correct weather information
* Annotations are used in these tests
*
* @author Mariam Cirovic
*
*/
@RunWith(MockitoJUnitRunner.class)
public class TripAdvisorTest {
@InjectMocks
TripAdvisor tripAdvisor = new TripAdvisor();
// @Mock annotation is used to create the mock object to be injected
@Mock
ExcursionService excursionService;
@Mock
WeatherService weatherService;
@Test
public void testExcursionList() {
String city = "Chania";
List<String> excursions = new ArrayList<String>();
excursions.add("Samaria Gorge");
excursions.add("Santorini Island");
excursions.add("Elafonisi Beach");
excursions.add("Gavdos Island");
excursions.add("Preveli Beach");
String type = "beach";
List<String> beachExcursions = new ArrayList<String>();
beachExcursions.add("Elafonisi Beach");
beachExcursions.add("Preveli Beach");
// Method chain to return the above lsit of excursions when a city "Chania" is specified
when(excursionService.excursionList(city)).thenReturn(excursions);
assertEquals(beachExcursions, tripAdvisor.getExcursionList(city, type));
verify(excursionService).excursionList(city);
}
@Test
public void testWeatherInfo() {
String city = "Chania";
Integer month = 9;
InOrder inOrder = inOrder(weatherService);
when(weatherService.predictedAverageTemperature(city, month)).thenReturn(27);
when(weatherService.predictedRainProbability(city, month)).thenReturn(0.15);
assertEquals("Warm and Dry", tripAdvisor.weatherInfo(city, month));
inOrder.verify(weatherService).predictedAverageTemperature(city, month);
inOrder.verify(weatherService).predictedRainProbability(city, month);
}
}
package com.com1028.travelagency;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
/**
*
* Tests that the TripManager class is calculating the discount correctly
* Also a Spy on an itinerary object is created and tested
* Static methods of Mockito are used
*
* @author Mariam Cirovic
*
*/
public class TripManagerTest {
TripManager manager;
// Mock the Trip Database class
TripDatabase db = mock(TripDatabase.class);
@Before
public void setUp() {
manager = new TripManager(db);
}
@Test
public void testApplyDiscount() {
// Method chain (stub) to state that the customerID c11 should return the value 2000.00
when(db.totalTripCost("c11")).thenReturn(2000.0);
assertEquals(1600.0, manager.applyDiscount(20, "c11"), 0);
verify(db).totalTripCost("c11");
when(db.totalTripCost("c20")).thenReturn(5000.0);
assertEquals(4000.0, manager.applyDiscount(20, "c20"), 0);
//assertEquals(4000.0, manager.applyDiscount(20, "c20"), 0);
verify(db).totalTripCost("c20");
//check if method is called once
verify(db, times(1)).totalTripCost("c20");
//verify that method was never called on a mock
verify(db, never()).cityItinerary("c11");
}
@Test
public void testItinerarySpy() {
List<String> itinerary = new ArrayList<String>();
itinerary.add("London");
itinerary.add("Paris");
itinerary.add("Rome");
// Create a spy to spy on the real instance itinerary
List<String> itinerarySpy = spy(itinerary);
assertEquals("London", itinerarySpy.get(0));
// This is a stub - it states that return "Madrid" in position 1 instead of "Paris"
// You can use a spy to do a partial mock
doReturn("Madrid").when(itinerarySpy).get(1);
assertEquals("Madrid", itinerarySpy.get(1));
assertEquals("Rome", itinerarySpy.get(2));
System.out.println("Number of cities: " + itinerarySpy.size());
// Add another city to the spy object
itinerarySpy.add("Athens");
System.out.println("Number of cities (spy): " + itinerarySpy.size());
// The size of itinerary is unchanged - the object was only added to the spy
// The spy has all the behaviour of the real object so it is mostly used to
// to explore legacy code that does not come with a clearly defined interface
System.out.println("Number of cities (original): " + itinerary.size());
}
}
File added
File added
File added
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment