<commit_before>package se.su.it.svc.server.log

import com.sun.security.auth.UserPrincipal
import org.eclipse.jetty.server.Authentication
import org.eclipse.jetty.server.Request
import org.eclipse.jetty.server.UserIdentity
import org.junit.Test
import org.junit.runner.RunWith
import org.powermock.modules.junit4.PowerMockRunner

import static org.easymock.EasyMock.createMock
import static org.easymock.EasyMock.expect
import static org.powermock.api.easymock.PowerMock.replayAll

@RunWith(PowerMockRunner)
class CommonRequestLogTest {

  @Test
  void "getUserPrincipal returns - for no auth"() {
    def request = createMock(Request)
<commit_msg>DEVIDM-94: Test the getStatus method.<commit_after>package se.su.it.svc.server.log

import com.sun.security.auth.UserPrincipal
import org.eclipse.jetty.server.*
import org.junit.Test
import org.junit.runner.RunWith
import org.powermock.modules.junit4.PowerMockRunner

import static org.easymock.EasyMock.createMock
import static org.easymock.EasyMock.expect
import static org.powermock.api.easymock.PowerMock.replayAll

@RunWith(PowerMockRunner)
class CommonRequestLogTest {

  @Test
  void "getStatus returns Async for async requests"() {
    def request = createMock(Request)
    def async = createMock(AsyncContinuation)

    expect(request.getAsyncContinuation()).andReturn(async)
    expect(async.isInitial()).andReturn(false)

    replayAll(request, async)

    def ret = new CommonRequestLog().getStatus(request, null)

    assert ret == 'Async'
  }

  @Test
  void "getStatus returns 404 for status < 0"() {
    def request = createMock(Request)
    def async = createMock(AsyncContinuation)
    def response = createMock(Response)

    expect(request.getAsyncContinuation()).andReturn(async)
    expect(async.isInitial()).andReturn(true)
    expect(response.getStatus()).andReturn(-1)

    replayAll(request, async, response)

    def ret = new CommonRequestLog().getStatus(request, response)

    assert ret == '404'
  }

  @Test
  void "getStatus returns 404 for status == 0"() {
    def request = createMock(Request)
    def async = createMock(AsyncContinuation)
    def response = createMock(Response)

    expect(request.getAsyncContinuation()).andReturn(async)
    expect(async.isInitial()).andReturn(true)
    expect(response.getStatus()).andReturn(0)

    replayAll(request, async, response)

    def ret = new CommonRequestLog().getStatus(request, response)

    assert ret == '404'
  }

  @Test
  void "getStatus returns status"() {
    def request = createMock(Request)
    def async = createMock(AsyncContinuation)
    def response = createMock(Response)

    expect(request.getAsyncContinuation()).andReturn(async)
    expect(async.isInitial()).andReturn(true)
    expect(response.getStatus()).andReturn(123)

    replayAll(request, async, response)

    def ret = new CommonRequestLog().getStatus(request, response)

    assert ret == '123'
  }

  @Test
  void "getStatus prepends zeros"() {
    def request = createMock(Request)
    def async = createMock(AsyncContinuation)
    def response = createMock(Response)

    expect(request.getAsyncContinuation()).andReturn(async)
    expect(async.isInitial()).andReturn(true)
    expect(response.getStatus()).andReturn(1)

    replayAll(request, async, response)

    def ret = new CommonRequestLog().getStatus(request, response)

    assert ret == '001'
  }

  @Test
  void "getUserPrincipal returns - for no auth"() {
    def request = createMock(Request)

{'fraction_non_alphanumeric': 0.10903337169159955, 'fraction_numerical': 0.00863060989643268, 'mean_word_length': 4.536624203821656, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2404967', 'n_tokens_mistral': 1088, 'n_tokens_neox': 1025, 'n_words': 220}