package io.teiler.api.endpoint;

import com.google.gson.Gson;
import io.teiler.api.endpoint.util.Util;
import io.teiler.server.dto.Group;
import org.junit.Assert;
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.web.client.TestRestTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

//@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 Util util;
    
//    @Test
    public void shouldReturn401WhenViewingGroupWithoutValidUUID() {

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

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

}

@@ -121,50 +121,248 @@
 ler.
-server.dto.Group;%0D%0Aimport org.junit.Assert
+api.service.GroupService;%0D%0Aimport io.teiler.server.Tylr;%0D%0Aimport io.teiler.server.dto.Group;%0D%0Aimport io.teiler.server.util.exceptions.NotAuthorizedException;%0D%0Aimport org.junit.Assert;%0D%0Aimport org.junit.Test;%0D%0Aimport org.junit.runner.RunWith
 ;%0D%0Ai
@@ -547,24 +547,170 @@
 erverPort;%0D%0A
+import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;%0D%0Aimport org.springframework.boot.test.context.SpringBootTest;%0D%0A
 import org.s
@@ -948,20 +948,141 @@
 ntity;%0D%0A
+import org.springframework.test.context.TestPropertySource;%0D%0Aimport org.springframework.test.context.junit4.SpringRunner;%0D%0A
 %0D%0A
-//
 @RunWith
@@ -1103,18 +1103,16 @@
 class)%0D%0A
-//
 @SpringB
@@ -1203,18 +1203,16 @@
 _PORT)%0D%0A
-//
 @TestPro
@@ -1265,18 +1265,16 @@
 567%22%7D)%0D%0A
-//
 @AutoCon
@@ -1506,20 +1506,16 @@
 plate;%0D%0A
-    
 %0D%0A    @A
@@ -1540,17 +1540,33 @@
 ate 
-Util util
+GroupService groupService
 ;%0D%0A 
@@ -1574,328 +1574,104 @@
   %0D%0A
-//
     @
-Test%0D%0A    public void shouldReturn401WhenViewingGroupWithoutValidUUID() %7B%0D%0A%0D%0A        ResponseEntity%3CString%3E response = testRestTemplate.getForEntity(%22http://%22 + ip + %22:%22 + port + %22/v1/group%22, String.class)
+Autowired%0D%0A    private Util util
 ;%0D%0A    
+%0D%0A
     
-Assert.assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode());%0D%0A    %7D%0D%0A    %0D%0A//    @Test
+@Test(expected = NotAuthorizedException.class)
 %0D%0A  
@@ -1688,35 +1688,45 @@
 oid shouldReturn
-401
+NotAuthorized
 WhenViewingGroup
@@ -1733,21 +1733,22 @@
 With
-Empty
+outValid
 UUID()
- %7B
 %0D%0A  
@@ -1757,328 +1757,74 @@
     
-String url = %22http://%22 + ip + %22:%22 + util.getPort() + %22/v1/group%22;%0D%0A        HttpEntity entity = Util.getHttpEntityWithHeader(%22%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(%22%22
 );%0D%0A

Add unit tests