mardi 31 juillet 2018

Zuul not throwing expected exception

I am throwing a ZuulRuntimeException and written a test case to validate it but it is throwing a different one.

Code:

public String extractCdsid(String accessToken) {

        ObjectMapper objectMapper = new ObjectMapper();
        String cdsid = "";

        try {

            String payload = JwtHelper.decode(accessToken).getClaims();
            cdsid = objectMapper.readTree(payload).get("upn").textValue();

        } catch (NullPointerException e) {      
            ZuulException ex = new ZuulException(
                    "Token does not contain upn. Please send a valid that contains upn(cdsid)", 401, "");

            throw new ZuulRuntimeException(ex);     

        } }

Testcase:

@Test(expected = ZuulRuntimeException.class)
    public void getPrincipalHeader_withAuthorizationHeaderNotContainUpn_returnsException(){

        // mock
        MockHttpServletRequest req = new MockHttpServletRequest();
        req.addHeader("Authorization", "sendingtokenthatdoesnotcontainupn");

        // run
        tokenParser.getPrincipalHeader(req);        

    }

I am getting

java.lang.Exception: Unexpected exception, expected<com.netflix.zuul.exception.ZuulException> but was<java.lang.IllegalStateException>
    at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:28)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.IllegalStateException: CounterFactory not initialized
    at com.netflix.zuul.monitoring.CounterFactory.instance(CounterFactory.java:42)
    at com.netflix.zuul.exception.ZuulException.incrementCounter(ZuulException.java:73)
    at com.netflix.zuul.exception.ZuulException.<init>(ZuulException.java:54)
    at com.ford.cv.vadr.vadrgateway.util.TokenParser.extractCdsid(TokenParser.java:51)
    at com.ford.cv.vadr.vadrgateway.util.TokenParser.getPrincipalHeader(TokenParser.java:32)
    at com.ford.cv.vadr.vadrgateway.util.TokenParserTest.getPrincipalHeader_withAuthorizationHeaderNotContainUpn_returnsException(TokenParserTest.java:67)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:19)
    ... 16 more

I see the correct exception being thrown with message when I hit the service throw postman

Aucun commentaire:

Enregistrer un commentaire