Skip to content
Snippets Groups Projects
Commit e268a6c6 authored by CipherChen0's avatar CipherChen0
Browse files

terraform

parent c64b6e93
Branches
No related tags found
No related merge requests found
variable "flavor" { default = "m1.large" }
variable "image" { default = "Rocky Linux 9 20220830" }
variable "name1" { default = "RockyMatrix" }
variable "name1" { default = "JenkinsServer" }
variable "keypair" { default = "lc" } # you may need to change this,original=openstack_keypair
variable "network" { default = "lc" } # you need to change this
......
......@@ -42,6 +42,12 @@ dependencies {
testImplementation("io.github.bonigarcia:webdrivermanager:5.2.0")
testImplementation group: 'net.sourceforge.htmlunit', name: 'htmlunit', version: '2.32'
testImplementation group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '4.1.0'
testImplementation 'junit:junit:4.13.2'
testImplementation 'org.seleniumhq.selenium:selenium-java:4.18.1'
}
// 确保不会重复创建任务
......@@ -52,3 +58,7 @@ tasks.named('jacocoTestReport').configure {
tasks.named('test') {
useJUnitPlatform()
}
test {
outputs.upToDateWhen { false } // 强制每次运行都重新生成
}
\ No newline at end of file
package com.cardiff.cmyTest;
import com.cardiff.client_project.controller.admin.CommonAdminHospitalController;
import com.cardiff.client_project.service.CommonAdminHospitalService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import java.util.Collections;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@ExtendWith(SpringExtension.class)
@WebMvcTest(CommonAdminHospitalController.class)
@AutoConfigureMockMvc(addFilters = false) // 跳过 Spring Security 过滤器
public class ComponentTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private CommonAdminHospitalService hospitalService;
@Test
void testGetAllHospitals() throws Exception {
when(hospitalService.getAllHospitals()).thenReturn(Collections.emptyList());
mockMvc.perform(get("/commonAdmin/hospital/all"))
.andExpect(status().isOk());
}
}
package com.cardiff.cmyTest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpStatus;
import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class HttpTest {
@Value("${local.server.port}")
private int port;
@Autowired
private TestRestTemplate restTemplate;
@Test
void testGetAllHospitalsHttp() {
String url = "http://localhost:" + port + "/commonAdmin/hospital/all";
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
// 输出调试信息
System.out.println("Response Status: " + response.getStatusCode());
System.out.println("Response Body: " + response.getBody());
// 断言 HTTP 状态码
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
}
}
package com.cardiff.lcTest;
import com.cardiff.client_project.controller.nursinghome.NursingHomeController;
import com.cardiff.client_project.pojo.dto.HospitalDTO;
import com.cardiff.client_project.service.NursingHomeService;
import com.cardiff.client_project.utils.Result;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import java.util.Collections;
import java.util.List;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@WebMvcTest(NursingHomeController.class)
@ExtendWith(SpringExtension.class)
public class LightWeightTest {
//@Autowired:这两个注解使得Spring自动注入MockMvc和ObjectMapper对象。
// MockMvc用于模拟HTTP请求和响应,而ObjectMapper用于在JSON和Java对象之间进行转换。
//
//@MockBean:该注解是Spring Boot的特有注解,用于创建一个虚拟的(mock)NursingHomeService对象,并注入到Controller中。
// 通过这种方式,Service层的逻辑被“mock”掉了,测试可以专注于Controller的行为,而无需依赖真实的Service层逻辑。
@Autowired
private MockMvc mockMvc;
@MockBean
private NursingHomeService nursingHomeService;
@Autowired
private ObjectMapper objectMapper;
//测试:获取可用床位
@Test
public void testGetAvailableBeds() throws Exception {
List<HospitalDTO> mockList = Collections.singletonList(new HospitalDTO());
when(nursingHomeService.getAvailableBeds()).thenReturn(Result.success(mockList));
mockMvc.perform(get("/api/hospitals/available"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(1));
}
//测试:添加医院信息
@Test
public void testAddHospital() throws Exception {
HospitalDTO hospitalDTO = new HospitalDTO();
hospitalDTO.setName("Test Hospital");
when(nursingHomeService.insertPatientInfo(any(HospitalDTO.class)))
.thenReturn(Result.success(hospitalDTO));
mockMvc.perform(post("/api/hospitals")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(hospitalDTO)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(1));
}
//测试:更新床位数
@Test
public void testUpdateBedCount() throws Exception {
when(nursingHomeService.updateBedCount(1, 20)).thenReturn(Result.success("Updated"));
mockMvc.perform(put("/api/hospitals/1/beds")
.param("currentPatients", "20"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(1));
}
//测试:删除医院信息
@Test
public void testDeleteHospital() throws Exception {
when(nursingHomeService.deletePatientById(List.of(1)))
.thenReturn(Result.success("Deleted"));
mockMvc.perform(delete("/api/hospitals/1"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(1));
}
//测试:搜索医院(带参数)
@Test
public void testSearchHospitalsWithName() throws Exception {
when(nursingHomeService.searchHospitals("Test"))
.thenReturn(Result.success(List.of(new HospitalDTO())));
mockMvc.perform(get("/api/hospitals/search")
.param("name", "Test"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(1));
}
//测试:搜索医院(无参数,返回全部)
@Test
public void testSearchHospitalsWithoutName() throws Exception {
when(nursingHomeService.getAvailableBeds())
.thenReturn(Result.success(List.of(new HospitalDTO())));
mockMvc.perform(get("/api/hospitals/search"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(1));
}
//测试:更新审批状态
@Test
public void testUpdateApprovalStatus() throws Exception {
when(nursingHomeService.updateApprovalStatus(1, "approved"))
.thenReturn(Result.success("Approved"));
mockMvc.perform(put("/api/hospitals/1/approval")
.param("status", "approved"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(1));
}
//测试:获取待审批医院
@Test
public void testGetPendingApprovals() throws Exception {
when(nursingHomeService.getPendingApprovals())
.thenReturn(Result.success(List.of(new HospitalDTO())));
mockMvc.perform(get("/api/hospitals/pending"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(1));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment