Skip to content

Commit

Permalink
Merge branch 'main' into JAVA-S1050-Non-final-static-fields
Browse files Browse the repository at this point in the history
Signed-off-by: Piyush Acharya <[email protected]>
  • Loading branch information
VerisimilitudeX authored Oct 31, 2023
2 parents 1103d19 + 08451c2 commit 6410da2
Show file tree
Hide file tree
Showing 14 changed files with 160 additions and 184 deletions.
14 changes: 5 additions & 9 deletions src/main/java/DNAnalyzer/DNAnalyzerApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* Main class to run the DNAnalyzer program with a webserver.
*/
/** Main class to run the DNAnalyzer program with a webserver. */
@SpringBootApplication
public class DNAnalyzerApplication {

/**
* Main method to run the DNAnalyzer program with a webserver.
*/
public static void main(final String[] args) {
SpringApplication.run(DNAnalyzerApplication.class);
}
/** Main method to run the DNAnalyzer program with a webserver. */
public static void main(final String[] args) {
SpringApplication.run(DNAnalyzerApplication.class);
}
}
8 changes: 2 additions & 6 deletions src/main/java/DNAnalyzer/adapter/AnalyseController.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
package DNAnalyzer.adapter;

/**
* Controller for the analysis of DNA.
*/
public class AnalyseController {

}
/** Controller for the analysis of DNA. */
public class AnalyseController {}
70 changes: 34 additions & 36 deletions src/main/java/DNAnalyzer/adapter/ApiKeyController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,46 @@
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
* Controller to get and set the API key.
*/
/** Controller to get and set the API key. */
@RequiredArgsConstructor
@RestController
@RequestMapping("/api-key")
public class ApiKeyController {

/**
* Contructor of the controller.
*
* @param getApiKeyUseCase the use case to get the API key
* @param setApiKeyUseCase the use case to set the API key
*/
public ApiKeyController(GetApiKeyUseCase getApiKeyUseCase, SetApiKeyUseCase setApiKeyUseCase) {
this.getApiKeyUseCase = getApiKeyUseCase;
this.setApiKeyUseCase = setApiKeyUseCase;
}
/**
* Contructor of the controller.
*
* @param getApiKeyUseCase the use case to get the API key
* @param setApiKeyUseCase the use case to set the API key
*/
public ApiKeyController(GetApiKeyUseCase getApiKeyUseCase, SetApiKeyUseCase setApiKeyUseCase) {
this.getApiKeyUseCase = getApiKeyUseCase;
this.setApiKeyUseCase = setApiKeyUseCase;
}

private GetApiKeyUseCase getApiKeyUseCase;
private SetApiKeyUseCase setApiKeyUseCase;
private GetApiKeyUseCase getApiKeyUseCase;
private SetApiKeyUseCase setApiKeyUseCase;

/**
* Get the API key.
*
* @return the API key
*/
@GetMapping
public ApiKeyResponse getApiKey() {
String apiKey = getApiKeyUseCase.getApiKey();
return new ApiKeyResponse(apiKey);
}
/**
* Get the API key.
*
* @return the API key
*/
@GetMapping
public ApiKeyResponse getApiKey() {
String apiKey = getApiKeyUseCase.getApiKey();
return new ApiKeyResponse(apiKey);
}

/**
* Set the API key.
*
* @param request the request containing the new API key
* @return the new API key
*/
@PutMapping
public ApiKeyResponse setApiKey(@RequestBody SetApiKeyRequest request) {
String apiKey = setApiKeyUseCase.setApiKey(request.apiKey());
return new ApiKeyResponse(apiKey);
}
/**
* Set the API key.
*
* @param request the request containing the new API key
* @return the new API key
*/
@PutMapping
public ApiKeyResponse setApiKey(@RequestBody SetApiKeyRequest request) {
String apiKey = setApiKeyUseCase.setApiKey(request.apiKey());
return new ApiKeyResponse(apiKey);
}
}
4 changes: 2 additions & 2 deletions src/main/java/DNAnalyzer/adapter/ApiKeyResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/**
* Response to return the API key.
*
* @param apiKey the API key.
*/
public record ApiKeyResponse(String apiKey) {
}
public record ApiKeyResponse(String apiKey) {}
4 changes: 2 additions & 2 deletions src/main/java/DNAnalyzer/adapter/SetApiKeyRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/**
* Request to set the API key.
*
* @param apiKey the API key to set.
*/
public record SetApiKeyRequest(String apiKey) {
}
public record SetApiKeyRequest(String apiKey) {}
19 changes: 9 additions & 10 deletions src/main/java/DNAnalyzer/core/ApiKeyMissingException.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package DNAnalyzer.core;

/**
* Exception thrown when the API key is missing.
*/
/** Exception thrown when the API key is missing. */
public class ApiKeyMissingException extends RuntimeException {

/**
* Constructor of ApiKeyMissingException.
* @param message the message of the exception.
*/
public ApiKeyMissingException(String message) {
super(message);
}
/**
* Constructor of ApiKeyMissingException.
*
* @param message the message of the exception.
*/
public ApiKeyMissingException(String message) {
super(message);
}
}
31 changes: 14 additions & 17 deletions src/main/java/DNAnalyzer/core/ApiKeyService.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
package DNAnalyzer.core;


import DNAnalyzer.core.port.in.GetApiKeyUseCase;
import DNAnalyzer.core.port.in.SetApiKeyUseCase;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
* The purpose of this service is to handle all operations concerning the OpenAI API key.
*/
/** The purpose of this service is to handle all operations concerning the OpenAI API key. */
@Service
public class ApiKeyService implements GetApiKeyUseCase, SetApiKeyUseCase {

@Value("${openai.api.key}")
private String apiKey;
@Value("${openai.api.key}")
private String apiKey;

@Override
public String getApiKey() {
if (apiKey == null) {
throw new ApiKeyMissingException("No API-Key defined.");
}
return apiKey;
@Override
public String getApiKey() {
if (apiKey == null) {
throw new ApiKeyMissingException("No API-Key defined.");
}
return apiKey;
}

@Override
public String setApiKey(String apiKey) {
this.apiKey = apiKey;
return this.apiKey;
}
@Override
public String setApiKey(String apiKey) {
this.apiKey = apiKey;
return this.apiKey;
}
}
7 changes: 2 additions & 5 deletions src/main/java/DNAnalyzer/core/port/in/AnalyzeDnaRequest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
package DNAnalyzer.core.port.in;

/**
* The request body holding the parameters for the analyze DNA use case.
*/
public record AnalyzeDnaRequest() {
}
/** The request body holding the parameters for the analyze DNA use case. */
public record AnalyzeDnaRequest() {}
17 changes: 8 additions & 9 deletions src/main/java/DNAnalyzer/core/port/in/AnalyzeDnaUseCase.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package DNAnalyzer.core.port.in;

/**
* Use case interface to analyze DNA.
*/
/** Use case interface to analyze DNA. */
public interface AnalyzeDnaUseCase {

/**
* Analyze the DNA.
* @param request the request to analyze the DNA
* @return the result of the analysis
*/
AnalyzeResult analyzeDna(AnalyzeDnaRequest request);
/**
* Analyze the DNA.
*
* @param request the request to analyze the DNA
* @return the result of the analysis
*/
AnalyzeResult analyzeDna(AnalyzeDnaRequest request);
}
7 changes: 2 additions & 5 deletions src/main/java/DNAnalyzer/core/port/in/AnalyzeResult.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
package DNAnalyzer.core.port.in;

/**
* Result of an DNA analysis.
*/
public class AnalyzeResult {
}
/** Result of an DNA analysis. */
public class AnalyzeResult {}
15 changes: 7 additions & 8 deletions src/main/java/DNAnalyzer/core/port/in/GetApiKeyUseCase.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package DNAnalyzer.core.port.in;

/**
* Use case interface to get the API key.
*/
/** Use case interface to get the API key. */
public interface GetApiKeyUseCase {

/**
* Use case to get the set API key.
* @return the api key
*/
String getApiKey();
/**
* Use case to get the set API key.
*
* @return the api key
*/
String getApiKey();
}
17 changes: 8 additions & 9 deletions src/main/java/DNAnalyzer/core/port/in/SetApiKeyUseCase.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package DNAnalyzer.core.port.in;

/**
* Use case interface to set the API key.
*/
/** Use case interface to set the API key. */
public interface SetApiKeyUseCase {

/**
* Set the API key for the OpenAI API.
* @param apiKey the new API key.
* @return the api key
*/
String setApiKey(String apiKey);
/**
* Set the API key for the OpenAI API.
*
* @param apiKey the new API key.
* @return the api key
*/
String setApiKey(String apiKey);
}
79 changes: 39 additions & 40 deletions src/test/java/DNAnalyzer/adapter/ApiKeyControllerTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package DNAnalyzer.adapter;

import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
import static org.springframework.http.HttpHeaders.*;

import DNAnalyzer.core.port.in.GetApiKeyUseCase;
import DNAnalyzer.core.port.in.SetApiKeyUseCase;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
Expand All @@ -15,52 +20,46 @@
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
import static org.springframework.http.HttpHeaders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;

@WebMvcTest(ApiKeyController.class)
class ApiKeyControllerTest {

@Autowired
private MockMvc mockMvc;
@Autowired private MockMvc mockMvc;

@MockBean
private GetApiKeyUseCase getApiKeyUseCase;
@MockBean private GetApiKeyUseCase getApiKeyUseCase;

@MockBean
private SetApiKeyUseCase setApiKeyUseCase;
@MockBean private SetApiKeyUseCase setApiKeyUseCase;

@Test
void getApiKey() throws Exception {
when(getApiKeyUseCase.getApiKey()).thenReturn("mock-key");
@Test
void getApiKey() throws Exception {
when(getApiKeyUseCase.getApiKey()).thenReturn("mock-key");

MvcResult mvcResult = this.mockMvc
.perform(MockMvcRequestBuilders.get("/api-key")
.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().is(200))
.andExpect(MockMvcResultMatchers.jsonPath("$.apiKey", is("mock-key")))
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON))
.andDo(MockMvcResultHandlers.print())
.andReturn();
}
MvcResult mvcResult =
this.mockMvc
.perform(
MockMvcRequestBuilders.get("/api-key")
.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().is(200))
.andExpect(MockMvcResultMatchers.jsonPath("$.apiKey", is("mock-key")))
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON))
.andDo(MockMvcResultHandlers.print())
.andReturn();
}

@Test
void setApiKey() throws Exception {
when(setApiKeyUseCase.setApiKey(anyString())).thenAnswer(i -> i.getArguments()[0]);
@Test
void setApiKey() throws Exception {
when(setApiKeyUseCase.setApiKey(anyString())).thenAnswer(i -> i.getArguments()[0]);

MvcResult mvcResult = this.mockMvc
.perform(MockMvcRequestBuilders.put("/api-key")
.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.content("{\"apiKey\":\"new-key\"}"))
.andExpect(MockMvcResultMatchers.status().is(200))
.andExpect(MockMvcResultMatchers.jsonPath("$.apiKey", is("new-key")))
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON))
.andDo(MockMvcResultHandlers.print())
.andReturn();
}
MvcResult mvcResult =
this.mockMvc
.perform(
MockMvcRequestBuilders.put("/api-key")
.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.content("{\"apiKey\":\"new-key\"}"))
.andExpect(MockMvcResultMatchers.status().is(200))
.andExpect(MockMvcResultMatchers.jsonPath("$.apiKey", is("new-key")))
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON))
.andDo(MockMvcResultHandlers.print())
.andReturn();
}
}
Loading

0 comments on commit 6410da2

Please sign in to comment.