Skip to content

Commit 6eb320a

Browse files
committed
added test case for factory-service
1 parent 9963850 commit 6eb320a

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.cloudcode.springcloud;
2+
3+
import static org.mockito.ArgumentMatchers.any;
4+
import static org.mockito.ArgumentMatchers.anyMap;
5+
import static org.mockito.Mockito.when;
6+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
7+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
8+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
9+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
10+
11+
import java.util.List;
12+
import java.util.Optional;
13+
import java.util.Set;
14+
15+
import org.junit.jupiter.api.Test;
16+
import org.springframework.beans.factory.annotation.Autowired;
17+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
18+
import org.springframework.boot.test.context.SpringBootTest;
19+
import org.springframework.boot.test.mock.mockito.MockBean;
20+
import org.springframework.http.HttpStatus;
21+
import org.springframework.http.MediaType;
22+
import org.springframework.http.ResponseEntity;
23+
import org.springframework.test.web.servlet.MockMvc;
24+
25+
import com.cloudcode.springcloud.client.ProductServiceProxy;
26+
import com.cloudcode.springcloud.dao.AppDataJpaRepo;
27+
import com.cloudcode.springcloud.model.Factory;
28+
import com.cloudcode.springcloud.model.FactoryResponse;
29+
import com.cloudcode.springcloud.model.Product;
30+
import com.fasterxml.jackson.databind.ObjectMapper;
31+
import com.fasterxml.jackson.databind.ObjectWriter;
32+
33+
@SpringBootTest
34+
@AutoConfigureMockMvc
35+
public class FactoryControllerTest {
36+
37+
private static final String CONTEXT_PATH = "/factory-service";
38+
39+
private static final String END_POINT_V1_PATH = "/factory-service/v1/";
40+
41+
@Autowired
42+
private MockMvc mockMvc;
43+
44+
@MockBean
45+
private AppDataJpaRepo appRepo;
46+
47+
@MockBean
48+
private ProductServiceProxy productService;
49+
50+
private ObjectWriter objectWriter = new ObjectMapper().writer().withDefaultPrettyPrinter();
51+
52+
private Product product = new Product("ABC", 99.99D);
53+
private Factory factory = new Factory("ABC", Set.of("prod01"));
54+
private FactoryResponse factoryResponse = new FactoryResponse(factory.getFactoryName(), List.of(product));
55+
56+
@Test
57+
public void test_getProducts() throws Exception {
58+
when(appRepo.findById(factory.getFactoryName())).thenReturn(Optional.of(factory));
59+
when(productService.getProducts(anyMap())).thenReturn(new ResponseEntity<>(List.of(product), HttpStatus.OK));
60+
61+
mockMvc.perform(get(END_POINT_V1_PATH + factory.getFactoryName() + "/products").contextPath(CONTEXT_PATH))
62+
.andExpect(status().isOk())
63+
.andExpect(content().json(objectWriter.writeValueAsString(factoryResponse)))
64+
.andReturn();
65+
}
66+
67+
@Test
68+
public void test_createFactory() throws Exception {
69+
when(appRepo.findById(any())).thenReturn(Optional.empty());
70+
when(appRepo.save(any())).thenReturn(factory);
71+
72+
mockMvc.perform(post(END_POINT_V1_PATH + "factory/" + factory.getFactoryName()).contextPath(CONTEXT_PATH))
73+
.andExpect(status().isCreated())
74+
.andExpect(content().json(objectWriter.writeValueAsString(factory)))
75+
.andReturn();
76+
}
77+
78+
@Test
79+
public void test_createProduct() throws Exception {
80+
factory.setProductNames(null);
81+
when(appRepo.findById(any())).thenReturn(Optional.of(factory));
82+
when(appRepo.save(any())).thenReturn(factory);
83+
when(productService.saveProduct(anyMap(), any())).thenReturn(new ResponseEntity<>(product, HttpStatus.OK));
84+
mockMvc.perform(post(END_POINT_V1_PATH + factory.getFactoryName() + "/product").contextPath(CONTEXT_PATH)
85+
.contentType(MediaType.APPLICATION_JSON).content(objectWriter.writeValueAsString(product)))
86+
.andExpect(status().isCreated())
87+
.andExpect(content().json(objectWriter.writeValueAsString(factoryResponse)))
88+
.andReturn();
89+
}
90+
91+
}

0 commit comments

Comments
 (0)