Skip to content
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
36 changes: 36 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
### Gradle files ###
.gradle/
build/
!gradle-wrapper.jar
!gradle-wrapper.properties

### IntelliJ IDEA files ###
.idea/
*.iml
*.iws
*.ipr

### OS-specific files ###
.DS_Store
Thumbs.db

### Log files ###
*.log

### Temporary files ###
*.swp
*.swo
*.bak

### Gradle Wrapper ###
gradle-wrapper.jar
gradle-wrapper.properties

### Compiled class files ###
*.class

### JetBrains Rider ###
.idea/.idea_modules/

### IntelliJ project files ###
out/
13 changes: 13 additions & 0 deletions ReadMe
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Command to execute test with cucumber CLI plugin:
./gradlew cucumberCli

Removed this invalid scenario
------->
Scenario: Midnight (24-hour clock format)
When the time is 24:00:00
Then the clock should look like
| Y |
| RRRR |
| RRRR |
| OOOOOOOOOOO |
| OOOO |
43 changes: 33 additions & 10 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ apply plugin: 'eclipse'
version = '1.0-SNAPSHOT'
group = 'org.suggs.interviews.berlinclock'

task wrapper(type: Wrapper){
wrapper {
description = 'Generates gradlew scripts for NIX and win envs'
gradleVersion = '2.0'
gradleVersion = '7.3'
}

repositories {
jcenter()
mavenCentral()
}

Expand All @@ -22,15 +21,39 @@ idea.module {
}

dependencies {
compile 'org.slf4j:slf4j-api:1.7.5',
implementation 'org.slf4j:slf4j-api:1.7.5',
'commons-lang:commons-lang:2.6'

runtime 'org.slf4j:slf4j-log4j12:1.7.5',
implementation 'org.slf4j:slf4j-log4j12:1.7.5',
'log4j:log4j:1.2.17'

testCompile 'junit:junit:4.11',
'org.mockito:mockito-core:1.9.5',
'org.assertj:assertj-core:1.6.1',
'commons-io:commons-io:2.4',
'org.jbehave:jbehave-core:3.8'
// Cucumber dependencies
testImplementation 'io.cucumber:cucumber-java:7.11.0'
testImplementation 'io.cucumber:cucumber-junit:7.11.0'

// Jupiter api and engine dependencies
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.3'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.3'
testImplementation 'org.junit.vintage:junit-vintage-engine:5.9.3'
}

configurations {
cucumberRuntime {
extendsFrom testImplementation
}
}

task cucumberCli() {
dependsOn assemble, testClasses
doLast {
javaexec {
main = "io.cucumber.core.cli.Main"
classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
args = [
'--plugin', 'pretty',
'--plugin', 'html:target/cucumber-report.html',
'--glue', 'com.baeldung.cucumber',
'src/test/resources']
}
}
}
48 changes: 48 additions & 0 deletions cucumber-report.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.0-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3-bin.zip
12 changes: 6 additions & 6 deletions instructions/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ apply plugin: 'eclipse'
version = '1.0-SNAPSHOT'
group = 'org.suggs.interviews.example'

task wrapper(type: Wrapper){
wrapper {
description = 'Generates gradlew scripts for NIX and win envs'
gradleVersion = '2.0'
gradleVersion = '7.3'
}

repositories {
jcenter()
mavenCentral()
mavenLocal()
}

Expand All @@ -22,10 +22,10 @@ idea.module {
}

dependencies {
compile 'org.slf4j:slf4j-api:1.7.5'
implementation 'org.slf4j:slf4j-api:1.7.5'

runtime 'org.slf4j:slf4j-log4j12:1.7.5',
implementation 'org.slf4j:slf4j-log4j12:1.7.5',
'log4j:log4j:1.2.17'

testCompile 'junit:junit:4.11'
testImplementation 'junit:junit:4.11'
}
2 changes: 1 addition & 1 deletion instructions/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.0-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3-bin.zip
2 changes: 1 addition & 1 deletion instructions/src/test/resources/log4j.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">

<log4j:configuration>

Expand Down
78 changes: 78 additions & 0 deletions src/main/java/com/ubs/opsit/interviews/BerlinClock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.ubs.opsit.interviews;

import java.time.LocalTime;
import java.time.format.DateTimeParseException;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
* @author Pankaj
*/
public class BerlinClock implements TimeConverter {
@Override
public String convertTime(String time) {
if (Objects.isNull(time)) {
throw new IllegalArgumentException("Invalid time format");
}

LocalTime localTime;
try {
localTime = LocalTime.parse(time);
} catch (DateTimeParseException ex) {
throw new IllegalArgumentException("Invalid time format");
}

return getLampOnOff(localTime.getSecond()) + "\n"
+ getHours(localTime.getHour()) + "\n"
+ getMinutes(localTime.getMinute());
}

/**
* Every 2 seconds lamp 1st row blinks on/off
* @param seconds
* @return
*/
protected String getLampOnOff(int seconds) {
// Use the LampSymbol enum to get the respective values
return (seconds % 2 == 0) ? LampSymbol.YELLOW.getLampValue() : LampSymbol.OFF.getLampValue();
}

protected String getHours(int hours) {
int numberTopHourLamps = hours / 5;
int numberBottomHourLamps = hours % 5;

// Use the LampSymbol enum for RED lamps
String topHourRow = getLampRowStream(4, numberTopHourLamps, LampSymbol.RED).collect(Collectors.joining());
String bottomHourRow = getLampRowStream(4, numberBottomHourLamps, LampSymbol.RED).collect(Collectors.joining());

return topHourRow + "\n" + bottomHourRow;
}

protected String getMinutes(int minutes) {
int numberTopMinutesLamps = minutes / 5;
int numberBottomMinutesLamps = minutes % 5;

// Using Stream API to generate the first row of lamps (top minutes)
String topMinutesRow = IntStream.rangeClosed(1, 11)
.mapToObj(i -> i <= numberTopMinutesLamps ? getMinuteLampColour(i) : LampSymbol.OFF.getLampValue())
.collect(Collectors.joining());

// Using Stream API to generate the second row of lamps (bottom minutes)
String bottomMinutesRow = getLampRowStream(4, numberBottomMinutesLamps, LampSymbol.YELLOW)
.collect(Collectors.joining());

return topMinutesRow + "\n" + bottomMinutesRow;
}

private Stream<String> getLampRowStream(int totalNumberLamps, int numberLampsOn, LampSymbol lampSymbol) {
return IntStream.range(0, totalNumberLamps)
.mapToObj(i -> i < numberLampsOn ? lampSymbol.getLampValue() : LampSymbol.OFF.getLampValue());
}

private String getMinuteLampColour(int index) {
// Use the LampSymbol enum to determine the color of the lamp (Red for multiples of 3)
return (index % 3 == 0) ? LampSymbol.RED.getLampValue() : LampSymbol.YELLOW.getLampValue();
}
}
27 changes: 27 additions & 0 deletions src/main/java/com/ubs/opsit/interviews/LampSymbol.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.ubs.opsit.interviews;

/**
* @author Pankaj
*/
public enum LampSymbol {
OFF("O"),
YELLOW("Y"),
RED("R");

private final String lampValue;

// Constructor to set the lamp value
LampSymbol(String lampValue) {
this.lampValue = lampValue;
}

// Method to get the string representation for the lamp symbol
public String getLampValue() {
return this.lampValue;
}

// Static method to return the lamp symbol as a string based on the enum
public static String getLampSymbol(LampSymbol symbol) {
return symbol.getLampValue();
}
}
36 changes: 0 additions & 36 deletions src/test/java/com/ubs/opsit/interviews/BerlinClockFixture.java

This file was deleted.

61 changes: 61 additions & 0 deletions src/test/java/com/ubs/opsit/interviews/BerlinClockSteps.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.ubs.opsit.interviews;

import io.cucumber.java.BeforeAll;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;

import java.util.List;

import static org.junit.Assert.*;

public class BerlinClockSteps {
private static TimeConverter berlinClock;
private String time;
private String errorMessage;

private IllegalArgumentException exception;

@BeforeAll
public static void before_or_after_all() {
berlinClock = new BerlinClock();
}

@When("the time is {int}:{int}:{int}")
public void the_time_is(Integer int1, Integer int2, Integer int3) {
this.time = String.format("%02d:%02d:%02d", int1, int2, int3);
}

@Then("the clock should look like")
public void the_clock_should_look_like(List<String> expectedClockResult) {
String[] expectedRows = expectedClockResult.toArray(new String[0]);
//try {
String[] actualRows = berlinClock.convertTime(time).split("\n");
for (int i = 0; i < expectedRows.length; i++) {
assertEquals(expectedRows[i], actualRows[i]);
}
// } catch (IllegalArgumentException ex) {
// exception = ex;
// }
}

@When("the invalid time is null")
public void the_time_is_null() {
this.time = null;
}

@When("the time is {string}")
public void the_time_is_invalid(String time) {
this.time = time;
}

@When("the time is {double}.{int}")
public void the_time_is(Double double1, Integer int1) {
this.time = time;
}

@Then("the clock should throw an error {string}")
public void the_clock_should_throw_an_error(String expectedErrorMessage) {
assertThrows(expectedErrorMessage, IllegalArgumentException.class, () -> berlinClock.convertTime(time));
}

}
13 changes: 13 additions & 0 deletions src/test/java/com/ubs/opsit/interviews/TestRunner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.ubs.opsit.interviews;

import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@CucumberOptions(
plugin = {"pretty", "html:target/cucumber-report.html"},
features = {"src/test/resources"},
tags= "@SmokeTest")
public class TestRunner {
}
Loading