-
Notifications
You must be signed in to change notification settings - Fork 49
Tests/services for Common-API #272
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b0684d4
4034a2c
ba1a1e9
30423c7
5c7230e
95e0ecd
e8664a6
6e15991
d844d6a
7251bf0
dcff787
9d96d04
17e8555
e9b2853
3cfa664
a7bbce5
57ddf11
c7e06f7
82e4364
5692283
c21421d
266bc3c
934ee16
12ff5f8
a33e2f8
5489097
b101e14
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| name: TestCase Coverage Check | ||
|
|
||
| on: | ||
| pull_request: | ||
| types: [opened, synchronize] | ||
|
|
||
| permissions: {} | ||
|
|
||
| jobs: | ||
| build_and_check_coverage: | ||
| runs-on: ubuntu-latest | ||
| permissions: | ||
| contents: read | ||
|
|
||
| steps: | ||
| - name: Checkout code | ||
| uses: actions/checkout@v4 | ||
|
|
||
| - name: Setup JDK 17 | ||
| uses: actions/setup-java@v4 | ||
| with: | ||
| java-version: '17' | ||
| distribution: 'temurin' | ||
|
|
||
| - name: Cache Maven dependencies | ||
| uses: actions/cache@v3 | ||
| with: | ||
| path: ~/.m2 | ||
| key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} | ||
| restore-keys: | | ||
| ${{ runner.os }}-m2- | ||
|
|
||
| - name: Build with Maven | ||
| run: mvn clean verify | ||
|
|
||
| - name: Run Coverage Check | ||
| uses: madrapps/jacoco-report@v1.7.2 | ||
| with: | ||
| paths: target/site/jacoco/jacoco.xml | ||
| token: ${{ secrets.GITHUB_TOKEN }} | ||
| min-coverage-overall: 40 | ||
| min-coverage-changed-files: 60 | ||
| title: Code Coverage Report | ||
| comment-type: pr_comment |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,139 @@ | ||||||
| /* | ||||||
| * AMRIT β Accessible Medical Records via Integrated Technology | ||||||
| * Integrated EHR (Electronic Health Records) Solution | ||||||
| * | ||||||
| * Copyright (C) "Piramal Swasthya Management and Research Institute" | ||||||
| * | ||||||
| * This file is part of AMRIT. | ||||||
| * | ||||||
| * This program is free software: you can redistribute it and/or modify | ||||||
| * it under the terms of the GNU General Public License as published by | ||||||
| * the Free Software Foundation, either version 3 of the License, or | ||||||
| * (at your option) any later version. | ||||||
| * | ||||||
| * This program is distributed in the hope that it will be useful, | ||||||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||
| * GNU General Public License for more details. | ||||||
| * | ||||||
| * You should have received a copy of the GNU General Public License | ||||||
| * along with this program. If not, see https://www.gnu.org/licenses/. | ||||||
| */ | ||||||
| package com.iemr.common.controller.helpline104history; | ||||||
|
|
||||||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||||||
| import com.iemr.common.data.helpline104history.H104BenMedHistory; | ||||||
| import com.iemr.common.service.helpline104history.H104BenHistoryServiceImpl; | ||||||
| import com.iemr.common.utils.response.OutputResponse; | ||||||
| import org.junit.jupiter.api.BeforeEach; | ||||||
| import org.junit.jupiter.api.Test; | ||||||
| import org.junit.jupiter.api.extension.ExtendWith; | ||||||
| import org.mockito.InjectMocks; | ||||||
| import org.mockito.Mock; | ||||||
| import org.mockito.junit.jupiter.MockitoExtension; | ||||||
| import org.springframework.http.MediaType; | ||||||
| import org.springframework.test.web.servlet.MockMvc; | ||||||
| import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||||||
|
|
||||||
| import java.util.ArrayList; | ||||||
| import java.sql.Timestamp; | ||||||
| import java.sql.Date; | ||||||
|
|
||||||
| import static org.mockito.ArgumentMatchers.anyLong; | ||||||
| import static org.mockito.Mockito.when; | ||||||
| import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||||||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||||||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; | ||||||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||||||
| import static org.hamcrest.Matchers.containsString; | ||||||
|
|
||||||
| @ExtendWith(MockitoExtension.class) | ||||||
| class Helpline104BeneficiaryHistoryControllerTest { | ||||||
|
|
||||||
| private MockMvc mockMvc; | ||||||
|
|
||||||
| @Mock | ||||||
| private H104BenHistoryServiceImpl smpleBenHistoryServiceImpl; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix typo in mock variable name. The variable name - private H104BenHistoryServiceImpl smpleBenHistoryServiceImpl;
+ private H104BenHistoryServiceImpl simpleBenHistoryServiceImpl;π Committable suggestion
Suggested change
π€ Prompt for AI Agents |
||||||
|
|
||||||
| @InjectMocks | ||||||
| private Helpline104BeneficiaryHistoryController controller; | ||||||
|
|
||||||
| private ObjectMapper objectMapper; | ||||||
|
|
||||||
| @BeforeEach | ||||||
| void setUp() { | ||||||
| mockMvc = MockMvcBuilders.standaloneSetup(controller).build(); | ||||||
| objectMapper = new ObjectMapper(); | ||||||
| } | ||||||
|
|
||||||
| @Test | ||||||
| void getBenCaseSheet_Success() throws Exception { | ||||||
| Long beneficiaryRegID = 12345L; | ||||||
| String requestJson = "{\"beneficiaryRegID\":" + beneficiaryRegID + "}"; | ||||||
|
|
||||||
| ArrayList<H104BenMedHistory> mockHistoryList = new ArrayList<>(); | ||||||
| H104BenMedHistory history1 = new H104BenMedHistory(beneficiaryRegID, "John Doe", 30, 1, "Male", "Algo1", "Symptom1", "Summary1", "SumID1", "None", "Diagnosis1", "DiagID1", "Advice1", 1L, "HAO1", "CO1", "MO1", "Remarks1", false, "User1", new Timestamp(System.currentTimeMillis()), "User1", new java.sql.Date(System.currentTimeMillis())); | ||||||
| H104BenMedHistory history2 = new H104BenMedHistory(beneficiaryRegID, "Jane Doe", 25, 2, "Female", "Algo2", "Symptom2", "Summary2", "SumID2", "Pollen", "Diagnosis2", "DiagID2", "Advice2", 2L, "HAO2", "CO2", "MO2", "Remarks2", false, "User2", new Timestamp(System.currentTimeMillis()), "User2", new java.sql.Date(System.currentTimeMillis())); | ||||||
| mockHistoryList.add(history1); | ||||||
| mockHistoryList.add(history2); | ||||||
|
|
||||||
| when(smpleBenHistoryServiceImpl.geSmpleBenHistory(beneficiaryRegID)).thenReturn(mockHistoryList); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. π οΈ Refactor suggestion | π Major π§© Analysis chainVerify the service method name is correct. The mocked method name π Script executed: #!/bin/bash
# Verify the actual method name in the service implementation
ast-grep --pattern 'class H104BenHistoryServiceImpl {
$$$
geSmpleBenHistory($$$) {
$$$
}
$$$
}'
# Also search for correctly spelled variants
ast-grep --pattern 'class H104BenHistoryServiceImpl {
$$$
getSimpleBenHistory($$$) {
$$$
}
$$$
}'Length of output: 0 π Script executed: #!/bin/bash
set -e
echo "Occurrences of the mocked method:"
rg -n "geSmpleBenHistory" --glob '*.java' || true
echo -e "\nOccurrences of the correctly-spelled variant:"
rg -n "getSimpleBenHistory" --glob '*.java' || true
echo -e "\nService implementation classes (BenHistoryServiceImpl):"
rg -lP "class .*BenHistoryServiceImpl" --glob '*.java' || true
echo -e "\nContents of identified service impl files (first 200 lines):"
for file in $(rg -lP "class .*BenHistoryServiceImpl" --glob '*.java'); do
echo "### $file"
sed -n '1,200p' "$file"
doneLength of output: 5886 Rename method to getSimpleBenHistory |
||||||
|
|
||||||
| // Assuming H104BenMedHistory fields are not @Expose, OutputMapper.gson().toJson(this) will produce "{}" | ||||||
| // And ArrayList.toString() will produce "[{}, {}]" | ||||||
| String expectedDataJson = "[{}, {}]"; | ||||||
|
|
||||||
| String expectedResponseJson = String.format( | ||||||
| "{\"data\":%s,\"statusCode\":%d,\"errorMessage\":\"%s\",\"status\":\"%s\"}", | ||||||
| expectedDataJson, | ||||||
| OutputResponse.SUCCESS, | ||||||
| "Success", | ||||||
| "Success" | ||||||
| ); | ||||||
|
|
||||||
| mockMvc.perform(post("/beneficiary/get104BenMedHistory") | ||||||
| .header("Authorization", "Bearer token") | ||||||
| .contentType(MediaType.APPLICATION_JSON) | ||||||
| .content(requestJson)) | ||||||
| .andExpect(status().isOk()) | ||||||
| .andExpect(content().contentType(MediaType.APPLICATION_JSON)) | ||||||
| .andExpect(content().json(expectedResponseJson)); | ||||||
| } | ||||||
|
|
||||||
| @Test | ||||||
| void getBenCaseSheet_ServiceException() throws Exception { | ||||||
| Long beneficiaryRegID = 12345L; | ||||||
| String requestJson = "{\"beneficiaryRegID\":" + beneficiaryRegID + "}"; | ||||||
| String errorMessage = "Simulated database error"; | ||||||
|
|
||||||
| when(smpleBenHistoryServiceImpl.geSmpleBenHistory(anyLong())).thenThrow(new RuntimeException(errorMessage)); | ||||||
|
|
||||||
| mockMvc.perform(post("/beneficiary/get104BenMedHistory") | ||||||
| .header("Authorization", "Bearer token") | ||||||
| .contentType(MediaType.APPLICATION_JSON) | ||||||
| .content(requestJson)) | ||||||
| .andExpect(status().isOk()) | ||||||
| .andExpect(content().contentType(MediaType.APPLICATION_JSON)) | ||||||
| .andExpect(jsonPath("$.statusCode").value(OutputResponse.GENERIC_FAILURE)) | ||||||
| .andExpect(jsonPath("$.errorMessage").value(errorMessage)) | ||||||
| .andExpect(jsonPath("$.status", containsString("Failed with " + errorMessage))) | ||||||
| .andExpect(jsonPath("$.data").doesNotExist()); | ||||||
| } | ||||||
|
|
||||||
| @Test | ||||||
| void getBenCaseSheet_InvalidInput() throws Exception { | ||||||
| String invalidRequestJson = "{\"beneficiaryRegID\":\"not_a_long\"}"; | ||||||
| String expectedErrorMessagePart = "Cannot deserialize value of type `java.lang.Long` from String \"not_a_long\""; | ||||||
|
|
||||||
| mockMvc.perform(post("/beneficiary/get104BenMedHistory") | ||||||
| .header("Authorization", "Bearer token") | ||||||
| .contentType(MediaType.APPLICATION_JSON) | ||||||
| .content(invalidRequestJson)) | ||||||
| .andExpect(status().isOk()) | ||||||
| .andExpect(content().contentType(MediaType.APPLICATION_JSON)) | ||||||
| .andExpect(jsonPath("$.statusCode").value(OutputResponse.GENERIC_FAILURE)) | ||||||
| .andExpect(jsonPath("$.errorMessage", containsString(expectedErrorMessagePart))) | ||||||
| .andExpect(jsonPath("$.status", containsString("Failed with " + expectedErrorMessagePart))) | ||||||
| .andExpect(jsonPath("$.data").doesNotExist()); | ||||||
| } | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is RestTemplate being passed here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because , since the rest template is being created and used in the function its not testable ... so passing a rest template allows the code to be tested
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
another approach could also be that , the rest template could be defined at class level, but I wasnt sure that would be appropriate to do
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Zapper9982 Is it really necessary to add this extra parameter to the function? Have you tested whether it will not affect the flow of the application?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vanitha1822 , yes as u can see I have changed all the calls to registereverwellPatient to have the rest template .