Browse Source

Removed greeting controller and service

master
Jim Martens 10 months ago
parent
commit
be6fb20550
  1. 6
      oparl-server/src/main/java/de/twomartens/oparlservice/configs/OParlServiceProperties.java
  2. 24
      oparl-server/src/main/java/de/twomartens/oparlservice/control/GreetingController.java
  3. 30
      oparl-server/src/main/java/de/twomartens/oparlservice/service/GreetingService.java
  4. BIN
      oparl-server/src/main/resources/application-prod.yaml
  5. BIN
      oparl-server/src/main/resources/application-qs.yaml
  6. BIN
      oparl-server/src/main/resources/application-test.yaml
  7. BIN
      oparl-server/src/main/resources/application.yaml
  8. 37
      oparl-server/src/test/java/de/twomartens/oparlservice/control/GreetingControllerTests.java
  9. 37
      oparl-server/src/test/java/de/twomartens/oparlservice/service/GreetingServiceTest.java

6
oparl-server/src/main/java/de/twomartens/oparlservice/configs/OParlServiceProperties.java

@ -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;
}
}

24
oparl-server/src/main/java/de/twomartens/oparlservice/control/GreetingController.java

@ -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();
}
}

30
oparl-server/src/main/java/de/twomartens/oparlservice/service/GreetingService.java

@ -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);
}
}

BIN
oparl-server/src/main/resources/application-prod.yaml

BIN
oparl-server/src/main/resources/application-qs.yaml

BIN
oparl-server/src/main/resources/application-test.yaml

BIN
oparl-server/src/main/resources/application.yaml

37
oparl-server/src/test/java/de/twomartens/oparlservice/control/GreetingControllerTests.java

@ -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();
}
}

37
oparl-server/src/test/java/de/twomartens/oparlservice/service/GreetingServiceTest.java

@ -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…
Cancel
Save