package io.teiler.api.endpoint;

import com.google.gson.Gson;
import io.teiler.api.endpoint.util.Util;
import io.teiler.api.service.GroupService;
import io.teiler.server.Tylr;
import io.teiler.server.dto.Group;
import io.teiler.server.util.exceptions.NotAuthorizedException;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Tylr.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@TestPropertySource(properties = {"local.server.port=4567"})
@AutoConfigureTestDatabase
public class GroupEndpointTest {
    
    @LocalServerPort
    private int port;
    
    @Value("${server.ip}")
    private String ip;
    
    @Autowired
    private TestRestTemplate testRestTemplate;

    @Autowired
    private GroupService groupService;
    
    @Autowired
    private Util util;
    
    @Test(expected = NotAuthorizedException.class)
    public void shouldReturnNotAuthorizedWhenViewingGroupWithoutValidUUID()
        throws NotAuthorizedException {
        groupService.viewGroup("");
    }

//    @Test
    public void shouldReturn401WhenViewingGroupWithInvalidUUID() {
        String url = "http://" + ip + ":" + util.getPort() + "/v1/group";
        HttpEntity entity = Util.getHttpEntityWithHeader("abcdef");
        ResponseEntity<String> response = testRestTemplate.exchange(url, HttpMethod.GET, entity, String.class);
        Assert.assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode());
    }

//    @Test
    public void shouldReturnGroupNameWhenCreating() {
        String url = "http://" + ip + ":" + util.getPort() + "/v1/group";
        String testString = "Group Name";
        Group testGroup = new Group("", testString);
        Gson gson = new Gson();

        ResponseEntity<String> response = testRestTemplate.postForEntity(url, gson.toJson(testGroup), String.class);
        Group responseGroup = gson.fromJson(response.getBody(), Group.class);
        Assert.assertEquals(testString, responseGroup.getName());
    }

}

@@ -428,135 +428,8 @@
 d;%0D%0A
-import org.springframework.beans.factory.annotation.Value;%0D%0Aimport org.springframework.boot.context.embedded.LocalServerPort;%0D%0A
 impo
@@ -641,192 +641,8 @@
 e;%0D%0A
-import org.springframework.http.HttpEntity;%0D%0Aimport org.springframework.http.HttpMethod;%0D%0Aimport org.springframework.http.HttpStatus;%0D%0Aimport org.springframework.http.ResponseEntity;%0D%0A
 impo
@@ -1024,110 +1024,30 @@
     
-%0D%0A    @LocalServerPort%0D%0A    private int port;%0D%0A    %0D%0A    @Value(%22$%7Bserver.ip%7D%22)%0D%0A    private String ip
+Gson gson = new Gson()
 ;%0D%0A 
@@ -1433,34 +1433,32 @@
 (%22%22);%0D%0A    %7D%0D%0A%0D%0A
-//
     @Test%0D%0A    p
@@ -1442,32 +1442,73 @@
   %7D%0D%0A%0D%0A    @Test
+(expected = NotAuthorizedException.class)
 %0D%0A    public voi
@@ -1525,11 +1525,21 @@
 turn
-401
+NotAuthorized
 When
@@ -1567,18 +1567,16 @@
 idUUID()
- %7B
 %0D%0A      
@@ -1581,334 +1581,80 @@
     
-String url = %22http://%22 + ip + %22:%22 + util.getPort() + %22/v1/group%22;%0D%0A        HttpEntity entity = Util.getHttpEntityWithHeader(%22abcdef%22);%0D%0A        ResponseEntity%3CString%3E response = testRestTemplate.exchange(url, HttpMethod.GET, entity, String.class);%0D%0A        Assert.assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode()
+throws NotAuthorizedException %7B%0D%0A        groupService.viewGroup(%22abcdef%22
 );%0D%0A
@@ -1662,18 +1662,16 @@
    %7D%0D%0A%0D%0A
-//
     @Tes
@@ -1747,314 +1747,97 @@
 ing 
-url = %22http://%22 + ip + %22:%22 + util.getPort() + %22/v1/group%22;%0D%0A        String testString = %22Group Name%22;%0D%0A        Group testGroup = new Group(%22%22, testString);%0D%0A        Gson gson = new Gson();%0D%0A%0D%0A        ResponseEntity%3CString%3E response = testRestTemplate.postForEntity(url, gson.toJson(testGroup), String.class
+testString = %22Group Name%22;%0D%0A        String response = groupService.createGroup(testString
 );%0D%0A
@@ -1892,18 +1892,8 @@
 onse
-.getBody()
 , Gr
@@ -1979,13 +1979,11 @@
 %0A    %7D%0D%0A
-%0D%0A
 %7D%0D%0A

Add more tests (including one on db)