Skip to content

Commit

Permalink
Refactor Whisper calls for integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sprestrelski committed Nov 11, 2023
1 parent ede6d35 commit d700a81
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 206 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
# CSE 110 Project Team 39 Culinary Companion Crew
PantryPals - the application that provides you with recipes that YOU have currently in your possession!
PantryPals - the application that provides you with recipes that YOU have currently in your possession!

## How to run the app
1. Clone the repository
2. Change or create a `.vscode/launch.json`, and change the `vmArgs` to point to your JavaFX lib.
3. Run from `Main.java`.
7 changes: 1 addition & 6 deletions app/src/main/java/code/client/Controllers/AudioRecorder.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package code.client.Controllers;

import javafx.scene.control.Label;
import java.io.*;
import javax.sound.sampled.*;

public class AudioRecorder {
private AudioFormat audioFormat;
private TargetDataLine targetDataLine;
private Label recordingLabel;
private boolean recordingState;

public AudioRecorder(Label recordingLabel) {
public AudioRecorder() {
this.audioFormat = getAudioFormat();
this.recordingLabel = recordingLabel;
}

private AudioFormat getAudioFormat() {
Expand Down Expand Up @@ -54,7 +51,6 @@ public void run() {
targetDataLine = (TargetDataLine) AudioSystem.getLine(dataLineInfo);
targetDataLine.open(audioFormat);
targetDataLine.start();
recordingLabel.setVisible(true);

// the AudioInputStream that will be used to write the audio data to a file
AudioInputStream audioInputStream = new AudioInputStream(
Expand All @@ -66,7 +62,6 @@ public void run() {
audioInputStream,
AudioFileFormat.Type.WAVE,
audioFile);
recordingLabel.setVisible(false);
} catch (Exception ex) {
ex.printStackTrace();
}
Expand Down
8 changes: 0 additions & 8 deletions app/src/main/java/code/client/Model/IVoiceToText.java

This file was deleted.

146 changes: 0 additions & 146 deletions app/src/main/java/code/client/Model/VoiceToText.java

This file was deleted.

12 changes: 5 additions & 7 deletions app/src/main/java/code/client/Model/WhisperHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import org.json.JSONObject;
import org.json.JSONException;

Expand All @@ -23,7 +21,6 @@ public WhisperHandler(String API_ENDPOINT, String TOKEN, String MODEL) throws IO
this.API_ENDPOINT = API_ENDPOINT;
this.TOKEN = TOKEN;
this.MODEL = MODEL;
this.connection = sendHttpRequest();
}

public WhisperHandler(String API_ENDPOINT, String TOKEN, String MODEL, CustomHttpConnection connection) {
Expand Down Expand Up @@ -52,11 +49,13 @@ public String processAudio() throws IOException, URISyntaxException {
return response;
}

public void setHttpConnection(CustomHttpConnection connection) {
this.connection = connection;
}

public CustomHttpConnection sendHttpRequest() throws IOException, URISyntaxException {
// Set up request headers
File file = new File("recording.wav");
CustomHttpConnection connection = new RealHttpConnection(API_ENDPOINT);

String boundary = "Boundary-" + System.currentTimeMillis();
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
connection.setRequestProperty("Authorization", "Bearer " + TOKEN);
Expand All @@ -66,7 +65,7 @@ public CustomHttpConnection sendHttpRequest() throws IOException, URISyntaxExcep

// Set up output stream to write request body
OutputStream outputStream = connection.getOutputStream();

System.out.println(outputStream);
// Write model parameter to request body
writeParameterToOutputStream(outputStream, "model", MODEL, boundary);

Expand Down Expand Up @@ -129,7 +128,6 @@ private static String handleSuccessResponse(CustomHttpConnection connection)
response.append(inputLine);
}
in.close();

JSONObject responseJson = new JSONObject(response.toString());

String generatedText = responseJson.getString("text");
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/java/code/client/View/NewRecipeUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public void setScenes(Stage primaryStage, ArrayList<IWindowUI> scenes) {

public void addListeners() throws IOException, URISyntaxException {

AudioRecorder recorder = new AudioRecorder(new Label("Recording..."));
AudioRecorder recorder = new AudioRecorder();
WhisperHandler audioProcessor = new WhisperHandler(API_ENDPOINT, TOKEN, MODEL);

recordButton1.setOnAction(e -> {
Expand All @@ -206,6 +206,8 @@ public void addListeners() throws IOException, URISyntaxException {
recorder.stopRecording();
recording = false;
try {
audioProcessor.setHttpConnection(new RealHttpConnection(API_ENDPOINT));
audioProcessor.sendHttpRequest();
mealType = audioProcessor.processAudio();
} catch (IOException | URISyntaxException e2) {
e2.printStackTrace();
Expand All @@ -224,6 +226,8 @@ public void addListeners() throws IOException, URISyntaxException {
recorder.stopRecording();
recording = false;
try {
audioProcessor.setHttpConnection(new RealHttpConnection(API_ENDPOINT));
audioProcessor.sendHttpRequest();
ingredients = audioProcessor.processAudio();
} catch (IOException | URISyntaxException e2) {
e2.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package code.client.Model;
package code;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import code.client.Model.CustomHttpConnection;

public class MockHttpConnection implements CustomHttpConnection {
private int responseCode;
private InputStream inputStream;
Expand Down
73 changes: 37 additions & 36 deletions app/src/test/java/code/VoiceRetrievalTest.java
Original file line number Diff line number Diff line change
@@ -1,55 +1,56 @@
package code;

import javafx.scene.control.Label;

import java.io.*;
import org.junit.jupiter.api.Test;
import java.net.URISyntaxException;

import code.client.Controllers.AudioRecorder;
import org.junit.jupiter.api.Test;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import javafx.application.Platform;
import code.client.Model.CustomHttpConnection;
import code.client.Model.WhisperHandler;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;

/**
* Work In Progress Voice Retrieval Unit Test
*/
public class VoiceRetrievalTest {
private AudioRecorder audioRecorder;
private Label recordingLabel;

@BeforeAll
static void initJfxRuntime() {
Platform.startup(() -> {
});
}
/*
* Integration Tests
*/
@Test
void testSuccessfulProcessAudio() throws IOException, URISyntaxException {
CustomHttpConnection connection = new MockHttpConnection(200,
new ByteArrayInputStream("{\"text\":\"Breakfast.\"}".getBytes()), new ByteArrayOutputStream());
WhisperHandler audioProcessor = new WhisperHandler("API_ENDPOINT", "TOKEN", "MODEL", connection);
String response = audioProcessor.processAudio();
assertEquals("Breakfast.", response);

audioProcessor.setHttpConnection(new MockHttpConnection(200,
new ByteArrayInputStream("{\"text\":\"Chicken, cheese.\"}".getBytes()), null));
response = audioProcessor.processAudio();
assertEquals("Chicken, cheese.", response);

@BeforeEach
void setUp() {
recordingLabel = new Label("Recording");
audioRecorder = new AudioRecorder(recordingLabel);
}

void testAudioSave() throws InterruptedException {
audioRecorder.startRecording();
Thread.sleep(1000);
audioRecorder.stopRecording();
File file = new File("recording.wav");
assertTrue(file.exists());
@Test
void testFailedProcessAudio() throws IOException, URISyntaxException {
CustomHttpConnection connection = new MockHttpConnection(404,
new ByteArrayInputStream("Error text".getBytes()),
null);
WhisperHandler audioProcessor = new WhisperHandler("API_ENDPOINT", "TOKEN", "MODEL", connection);
String response = audioProcessor.processAudio();
assertEquals("Error text", response);
}

void testLabelChanges() throws InterruptedException {
audioRecorder.startRecording();
assertTrue(recordingLabel.isVisible());
Thread.sleep(1000);
/*
* Unit tests
*/
@Test
void testMockHttpCreation() throws IOException {
CustomHttpConnection connection = new MockHttpConnection(200, new ByteArrayInputStream("hello".getBytes()),
null);
int responseCode = connection.getResponseCode();
assertEquals(responseCode, 200);
assertEquals(connection.getInputStream(), connection.getErrorStream());

audioRecorder.stopRecording();
assertFalse(recordingLabel.isVisible());
}

}

0 comments on commit d700a81

Please sign in to comment.