generated from 2martens/template-service
Removed greeting controller and service
This commit is contained in:
parent
e5a8dbf8b4
commit
be6fb20550
|
@ -9,11 +9,5 @@ import org.springframework.context.annotation.Configuration;
|
|||
@ConfigurationProperties(prefix = "de.twomartens.oparlservice")
|
||||
public class OParlServiceProperties {
|
||||
|
||||
private final Template template = new Template();
|
||||
private String url;
|
||||
|
||||
@Data
|
||||
public static class Template {
|
||||
private String greeting;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,24 +0,0 @@
|
|||
package de.twomartens.oparlservice.control;
|
||||
|
||||
import de.twomartens.oparlservice.entity.Greeting;
|
||||
import de.twomartens.oparlservice.service.GreetingService;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class GreetingController {
|
||||
|
||||
private final GreetingService service;
|
||||
|
||||
GreetingController(GreetingService service) {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
@GetMapping("/greeting")
|
||||
@ResponseBody
|
||||
public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
|
||||
return Greeting.builder().message(service.createGreeting(name)).build();
|
||||
}
|
||||
}
|
|
@ -1,30 +0,0 @@
|
|||
package de.twomartens.oparlservice.service;
|
||||
|
||||
import de.twomartens.oparlservice.configs.OParlServiceProperties;
|
||||
import io.micrometer.core.instrument.Counter;
|
||||
import io.micrometer.core.instrument.MeterRegistry;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class GreetingService {
|
||||
|
||||
private final MeterRegistry meterRegistry;
|
||||
private final OParlServiceProperties properties;
|
||||
private final Counter counter;
|
||||
|
||||
public GreetingService(MeterRegistry meterRegistry, OParlServiceProperties properties) {
|
||||
this.meterRegistry = meterRegistry;
|
||||
this.properties = properties;
|
||||
counter = meterRegistry.counter("infodb.callCounter");
|
||||
}
|
||||
|
||||
public String createGreeting(String name) {
|
||||
log.info("Create greeting for '{}'", name);
|
||||
counter.increment();
|
||||
meterRegistry.gauge("infodb.nameLength", name.length());
|
||||
String greeting = properties.getTemplate().getGreeting();
|
||||
return String.format(greeting, name);
|
||||
}
|
||||
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -1,37 +0,0 @@
|
|||
package de.twomartens.oparlservice.control;
|
||||
|
||||
import de.twomartens.oparlservice.service.GreetingService;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.BDDMockito;
|
||||
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.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
|
||||
|
||||
@WebMvcTest(GreetingController.class)
|
||||
class GreetingControllerTests {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mvc;
|
||||
|
||||
@MockBean
|
||||
private GreetingService service;
|
||||
|
||||
@Test
|
||||
void testGreetingRest() throws Exception {
|
||||
BDDMockito.given(service.createGreeting("Template"))
|
||||
.willReturn("Hello Template!");
|
||||
|
||||
mvc.perform(MockMvcRequestBuilders.get("/greeting")
|
||||
.param("name", "Template")
|
||||
.contentType(MediaType.APPLICATION_JSON))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.aMapWithSize(1)))
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.message", Matchers.is("Hello Template!")))
|
||||
.andReturn();
|
||||
}
|
||||
}
|
|
@ -1,37 +0,0 @@
|
|||
package de.twomartens.oparlservice.service;
|
||||
|
||||
import de.twomartens.oparlservice.configs.OParlServiceProperties;
|
||||
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import static org.mockito.BDDMockito.given;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class GreetingServiceTest {
|
||||
|
||||
private GreetingService service;
|
||||
|
||||
@Mock
|
||||
private OParlServiceProperties properties;
|
||||
|
||||
@Mock
|
||||
private OParlServiceProperties.Template template;
|
||||
|
||||
@BeforeEach
|
||||
void beforeEach() {
|
||||
given(template.getGreeting()).willReturn("Hello %s");
|
||||
given(properties.getTemplate()).willReturn(template);
|
||||
service = new GreetingService(new SimpleMeterRegistry(), properties);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createGreeting() {
|
||||
String result = service.createGreeting("Test");
|
||||
Assertions.assertThat(result).isEqualTo("Hello Test");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue