package io.teiler.api.endpoint;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;

import com.google.gson.Gson;

import io.teiler.api.service.GroupService;
import io.teiler.server.Tylr;
import io.teiler.server.dto.Group;
import io.teiler.server.util.exceptions.NotAuthorizedException;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Tylr.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@TestPropertySource(properties = {"local.server.port=4567"})
@AutoConfigureTestDatabase
public class GroupEndpointTest {

    Gson gson = new Gson();
    
    @Autowired
    private GroupService groupService;

    @Test(expected = NotAuthorizedException.class)
    public void shouldReturnNotAuthorizedWhenViewingGroupWithoutValidId() {
        groupService.viewGroup("");
        
    }

    @Test(expected = NotAuthorizedException.class)
    public void shouldReturnNotAuthorizedWhenViewingGroupWithInvalidId() {
        groupService.viewGroup("abcdef");
    }

    @Test
    public void shouldReturnGroupNameWhenCreating() {
        String testString = "Group Name";
        String response = groupService.createGroup(testString);
        Group responseGroup = gson.fromJson(response, Group.class);
        Assert.assertEquals(testString, responseGroup.getName());
    }

}

@@ -1642,10 +1642,877 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void testNewIdOnEachCreation() %7B%0A        String firstGroupResponse = groupService.createGroup(%22Hello%22);%0A        String secondGroupResponse = groupService.createGroup(%22World%22);%0A        Group firstGroup = gson.fromJson(firstGroupResponse, Group.class);%0A        Group secondGroup = gson.fromJson(secondGroupResponse, Group.class);%0A        Assert.assertNotEquals(firstGroup.getUuid(), secondGroup.getUuid());%0A    %7D%0A%0A    @Test%0A    public void testNoConflictInGroupNames() %7B%0A        String firstGroupResponse = groupService.createGroup(%22Test%22);%0A        String secondGroupResponse = groupService.createGroup(%22Test%22);%0A        Group firstGroup = gson.fromJson(firstGroupResponse, Group.class);%0A        Group secondGroup = gson.fromJson(secondGroupResponse, Group.class);%0A        Assert.assertNotEquals(firstGroup.getUuid(), secondGroup.getUuid());%0A    %7D%0A%0A
 %7D%0A

Add more unit tests