Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
wip: add cli test
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan-Zollinger committed Jun 12, 2024
1 parent 4883ad3 commit e8f6c80
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 9 deletions.
15 changes: 12 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.graqr</groupId>
<artifactId>threshr</artifactId>
<version>0.0.12</version>
<version>0.0.13-SNAPSHOT</version>
<packaging>${packaging}</packaging>

<parent>
<groupId>io.micronaut.platform</groupId>
<artifactId>micronaut-parent</artifactId>
<version>4.4.1</version>
<version>4.4.2</version>
</parent>

<properties>
Expand Down Expand Up @@ -44,6 +44,16 @@
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>info.picocli</groupId>
<artifactId>picocli</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut.picocli</groupId>
<artifactId>micronaut-picocli</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
Expand Down Expand Up @@ -127,7 +137,6 @@
<version>42.7.2</version>
<scope>test</scope>
</dependency>

</dependencies>
<build>
<plugins>
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/com/graqr/threshr/ThreshrCli.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.graqr.threshr;

import com.graqr.threshr.model.queryparam.Tcin;
import io.micronaut.configuration.picocli.PicocliRunner;
import jakarta.inject.Singleton;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;

@Singleton
@Command(name = "threshr grocery query tool", mixinStandardHelpOptions = true)
public class ThreshrCli implements Runnable {

ThreshrController controller;
@Option(
names = {"--tcin", "-t", "product-id-number"},
required = false,
description = "", converter = TcinsConverter.class)
Tcin[] tcinValues;

public ThreshrCli(ThreshrController threshr) {
this.controller = threshr;
}


public static void main(String[] args) {
PicocliRunner.run(ThreshrCli.class, args);
}

public void run() {
// TODO: do all the things :P
}

static class TcinsConverter implements CommandLine.ITypeConverter<Tcin[]> {
@Override
public Tcin[] convert(String s) throws ThreshrException {
return new Tcin[]{new Tcin(s.split(","))};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
import java.util.List;

@Controller("/constructor")
public class Threshr {
public class ThreshrController {

private final ThreshrClient threshrClient;

@Inject
public Threshr(@SuppressWarnings("ClassEscapesDefinedScope") ThreshrClient threshrClient){
public ThreshrController(@SuppressWarnings("ClassEscapesDefinedScope") ThreshrClient threshrClient){
this.threshrClient = threshrClient;
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/graqr/threshr/model/queryparam/Place.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.graqr.threshr.model.queryparam;

import com.graqr.threshr.ThreshrController;
import io.micronaut.serde.annotation.Serdeable;
import lombok.Data;

/**
* Place is a query parameter used when querying nearby target store locations. The query accepts either a zipcode
* or a city-state combo. See {@link com.graqr.threshr.Threshr#getStores(Place)}
* or a city-state combo. See {@link ThreshrController#getStores(Place)}
*/
@Serdeable
@Data
Expand Down
54 changes: 54 additions & 0 deletions src/test/groovy/com/graqr/threshr/ThreshrCliSpec.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.graqr.threshr

import io.micronaut.configuration.picocli.PicocliRunner
import io.micronaut.context.ApplicationContext
import io.micronaut.context.env.Environment
import spock.lang.AutoCleanup
import spock.lang.Shared

class ThreshrCliSpec extends ThreshrSpec {

@Shared
final PrintStream originalOut = System.out
@Shared
final PrintStream originalErr = System.err

@Shared
ByteArrayOutputStream outputStream = new ByteArrayOutputStream()
ByteArrayOutputStream errStream = new ByteArrayOutputStream()


@Shared
@AutoCleanup
ApplicationContext ctx = ApplicationContext.run(Environment.CLI, Environment.TEST)

void execute(String... args) {
PicocliRunner.run(ThreshrCli, ctx, args)
}

def setup() {
outputStream.reset()
errStream.reset()
System.setOut(new PrintStream(outputStream))
System.setErr(new PrintStream(errStream))
}

def cleanup() {
System.setOut(originalOut)
System.setErr(originalErr)
}

def "cli help can be queried successfully"() {
when:
execute('--help')

then:
outputStream.toString() ==
"Usage: threshr grocery query tool [-hV] [-t=<tcinValues>]...\n" +
" -h, --help Show this help message and exit.\n" +
" -t, --tcin, product-id-number=<tcinValues>\n" +
"\n" +
" -V, --version Print version information and exit.\n"

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import java.util.stream.Collectors
* This test class is necessary despite similarity to the controller test. please don't delete this as the
* httpclient logs are visible in this test but not in the controller test.
*/
class ThreshrClientSpec extends ThreshrSpec {
class ThreshrControllerClientSpec extends ThreshrSpec {


void "no error requesting product summaries"() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import io.micronaut.test.extensions.spock.annotation.MicronautTest
import spock.lang.Shared

@MicronautTest
class ThreshrControllerSpec extends ThreshrSpec {
class ThreshrControllerControllerSpec extends ThreshrSpec {

@Shared
@Value('${test.datasources.default.url}')
Expand Down
2 changes: 1 addition & 1 deletion src/test/groovy/com/graqr/threshr/ThreshrSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ThreshrSpec extends Specification {
// ----------- Resources / Fields -----------
@Inject
@Shared
Threshr threshrController
ThreshrController threshrController

@Inject
@Shared
Expand Down

0 comments on commit e8f6c80

Please sign in to comment.