ScopesFilter extends ZuulFilter { @Override public Object run() { long startTime = System.currentTimeMillis(); try { process(); detail.setStatus(Constants.SUCCESS); } catch (Throwable e) { detail.setStatus(Constants.FAILED); throw e; } finally { long endTime = System.currentTimeMillis(); long duration = (endTime - startTime); detail.setTimeInMillisRun(duration); TraceContextHolder.getInstance().getActualTrace().addFilter(this.getClass().getSimpleName(), detail); } return null; }  @Override String filterType(); @Override int filterOrder(); @Override boolean shouldFilter(); @Override Object run();  }
@Test public void clientIdNotInScope() { this.request.addHeader(CLIENT_ID, clientId1); Api api = new Api(); api.setId(1L); App app = new App(); app.setId(10L); app.setClientId(clientId1); Plan plan = new Plan(); plan.setId(20L); plan.setApi(api); Operation operation = new Operation(1111L, null, null,null, null); Scope scope = new Scope(1000L, null, null, null, null); app.setPlans(Collections.singletonList(plan)); plan.setScopes(new HashSet<>(Collections.singletonList(scope))); scope.setOperations(new HashSet<>(Collections.singletonList(operation))); context.set(OPERATION_ID, operation.getId() + 1); Mockito.when(appRepository.findByClientId(Mockito.anyString())).thenReturn(app); this.filter.run(); final FilterDetail filterDetail = TraceContextHolder.getInstance().getActualTrace().getFilters().get(SCOPES_FILTER); assertEquals(HttpStatus.OK.value(), context.getResponseStatusCode()); assertEquals(Constants.SUCCESS, filterDetail.getStatus()); }
@Test public void clientIdAllowedInScope() { this.request.addHeader(CLIENT_ID, clientId1); Api api = new Api(); api.setId(1L); App app = new App(); app.setId(10L); app.setClientId(clientId1); Plan plan = new Plan(); plan.setId(20L); plan.setApi(api); Operation operation = new Operation(1111L, null, null,null, null); Scope scope = new Scope(1000L, null, null, null, null); app.setPlans(Collections.singletonList(plan)); plan.setScopes(new HashSet<>(Collections.singletonList(scope))); scope.setOperations(new HashSet<>(Collections.singletonList(operation))); context.set(OPERATION_ID, operation.getId()); Mockito.when(appRepository.findByClientId(Mockito.anyString())).thenReturn(app); this.filter.run(); final FilterDetail filterDetail = TraceContextHolder.getInstance().getActualTrace().getFilters().get(SCOPES_FILTER); assertEquals(HttpStatus.OK.value(), context.getResponseStatusCode()); assertEquals(Constants.SUCCESS, filterDetail.getStatus()); }
@Test public void multiplePlansSuccess() { this.request.addHeader(CLIENT_ID, clientId1); Api api1 = new Api(); api1.setId(1L); Api api2 = new Api(); api2.setId(2L); Plan plan1 = new Plan(); plan1.setId(100L); plan1.setApi(api1); Plan plan2 = new Plan(); plan2.setId(200L); plan2.setApi(api2); App app1 = new App(); app1.setId(10L); app1.setClientId(clientId1); app1.setPlans(Collections.singletonList(plan1)); App app2 = new App(); app2.setId(20L); app2.setClientId(clientId2); app2.setPlans(Collections.singletonList(plan2)); Operation operation1 = new Operation(1111L, null, null,null, null); Operation operation2 = new Operation(2222L, null, null,null, null); Scope scope1 = new Scope(); scope1.setId(123L); scope1.setOperations(new HashSet<>(Collections.singletonList(operation1))); Scope scope2 = new Scope(); scope2.setId(321L); scope2.setOperations(new HashSet<>(Collections.singletonList(operation2))); plan1.setScopes(new HashSet<>(Collections.singletonList(scope1))); plan2.setScopes(new HashSet<>(Collections.singletonList(scope2))); context.set(OPERATION_ID, operation1.getId()); Mockito.when(appRepository.findByClientId(Mockito.anyString())).thenReturn(app1); this.filter.run(); final FilterDetail filterDetail = TraceContextHolder.getInstance().getActualTrace().getFilters().get(SCOPES_FILTER); assertEquals(HttpStatus.OK.value(), context.getResponseStatusCode()); assertEquals(Constants.SUCCESS, filterDetail.getStatus()); }
@Test public void multiplePlansFailure() { this.request.addHeader(CLIENT_ID, clientId2); Api api1 = new Api(); api1.setId(1L); Api api2 = new Api(); api2.setId(2L); Plan plan1 = new Plan(); plan1.setId(100L); plan1.setApi(api1); Plan plan2 = new Plan(); plan2.setId(200L); plan2.setApi(api2); App app1 = new App(); app1.setId(10L); app1.setClientId(clientId1); app1.setPlans(Collections.singletonList(plan1)); App app2 = new App(); app2.setId(20L); app2.setClientId(clientId2); app2.setPlans(Collections.singletonList(plan2)); Operation operation1 = new Operation(1111L, null, null,null, null); Operation operation2 = new Operation(2222L, null, null,null, null); Scope scope1 = new Scope(123L, null, null, null, new HashSet<>(Collections.singletonList(operation1))); Scope scope2 = new Scope(321L, null, null, null, new HashSet<>(Collections.singletonList(operation2))); plan1.setScopes(new HashSet<>(Collections.singletonList(scope1))); plan2.setScopes(new HashSet<>(Collections.singletonList(scope2))); context.set(OPERATION_ID, operation1.getId()); context.set(API_ID, api2.getId()); Mockito.when(appRepository.findByClientId(Mockito.anyString())).thenReturn(app2); this.filter.run(); final FilterDetail filterDetail = TraceContextHolder.getInstance().getActualTrace().getFilters().get(SCOPES_FILTER); assertEquals(HttpStatus.FORBIDDEN.value(), context.getResponseStatusCode()); assertEquals(Constants.SUCCESS, filterDetail.getStatus()); }
@Test public void overlappingPlansApp1() { this.request.addHeader(CLIENT_ID, clientId1); Api api1 = new Api(); api1.setId(1L); Api api2 = new Api(); api2.setId(2L); Plan plan1 = new Plan(); plan1.setId(100L); plan1.setApi(api1); Plan plan2 = new Plan(); plan2.setId(200L); plan2.setApi(api2); App app1 = new App(); app1.setId(10L); app1.setClientId(clientId1); app1.setPlans(Collections.singletonList(plan1)); App app2 = new App(); app2.setId(20L); app2.setClientId(clientId2); app2.setPlans(Collections.singletonList(plan2)); Operation operation1 = new Operation(1111L, null, null,null, null); Operation operation2 = new Operation(2222L, null, null,null, null); Operation operation3 = new Operation(3333L, null, null,null, null); List<Operation> operations1 = new ArrayList<>(); List<Operation> operations2 = new ArrayList<>(); operations1.add(operation1); operations1.add(operation3); operations2.add(operation2); operations2.add(operation3); Scope scope1 = new Scope(123L, null, null, null, new HashSet<>(operations1)); Scope scope2 = new Scope(321L, null, null, null, new HashSet<>(operations2)); plan1.setScopes(new HashSet<>(Collections.singletonList(scope1))); plan2.setScopes(new HashSet<>(Collections.singletonList(scope2))); context.set(OPERATION_ID, operation3.getId()); context.set(API_ID, api1.getId()); Mockito.when(appRepository.findByClientId(Mockito.anyString())).thenReturn(app1); this.filter.run(); final FilterDetail filterDetail = TraceContextHolder.getInstance().getActualTrace().getFilters().get(SCOPES_FILTER); assertEquals(HttpStatus.OK.value(), context.getResponseStatusCode()); assertEquals(Constants.SUCCESS, filterDetail.getStatus()); }
@Test public void overlappingPlansApp2() { this.request.addHeader(CLIENT_ID, clientId1); Api api1 = new Api(); api1.setId(1L); Api api2 = new Api(); api2.setId(2L); Plan plan1 = new Plan(); plan1.setId(100L); plan1.setApi(api1); Plan plan2 = new Plan(); plan2.setId(200L); plan2.setApi(api2); App app1 = new App(); app1.setId(10L); app1.setClientId(clientId1); app1.setPlans(Collections.singletonList(plan1)); Operation operation1 = new Operation(1111L, null, null,null, null); Operation operation2 = new Operation(2222L, null, null,null, null); Operation operation3 = new Operation(3333L, null, null,null, null); List<Operation> operations1 = new ArrayList<>(); List<Operation> operations2 = new ArrayList<>(); operations1.add(operation1); operations1.add(operation3); operations2.add(operation2); operations2.add(operation3); Scope scope1 = new Scope(123L, null, null, null, new HashSet<>(operations1)); Scope scope2 = new Scope(321L, null, null, null, new HashSet<>(operations2)); plan1.setScopes(new HashSet<>(Collections.singletonList(scope1))); plan2.setScopes(new HashSet<>(Collections.singletonList(scope2))); context.set(OPERATION_ID, operation2.getId()); context.set(API_ID, api1.getId()); Mockito.when(appRepository.findByClientId(Mockito.anyString())).thenReturn(app1); this.filter.run(); final FilterDetail filterDetail = TraceContextHolder.getInstance().getActualTrace().getFilters().get(SCOPES_FILTER); assertEquals(HttpStatus.FORBIDDEN.value(), context.getResponseStatusCode()); assertEquals(Constants.SUCCESS, filterDetail.getStatus()); }
@Test public void noClientIdHeader() { Api api = new Api(); api.setId(1L); App app = new App(); app.setId(100L); app.setClientId(clientId1); Plan plan = new Plan(); plan.setId(10L); plan.setApi(api); Operation operation = new Operation(1111L, null, null,null, null); Scope scope = new Scope(1000L, null, null, null, null); app.setPlans(Collections.singletonList(plan)); plan.setScopes(new HashSet<>(Collections.singletonList(scope))); scope.setOperations(new HashSet<>(Collections.singletonList(operation))); context.set(OPERATION_ID, operation.getId()); context.set(API_ID, api.getId()); this.filter.run(); final FilterDetail filterDetail = TraceContextHolder.getInstance().getActualTrace().getFilters().get(SCOPES_FILTER); assertEquals(HttpStatus.OK.value(), context.getResponseStatusCode()); assertEquals(Constants.SUCCESS, filterDetail.getStatus()); }
@Test public void noApp() { this.request.addHeader(CLIENT_ID, clientId1); Api api = new Api(); api.setId(1L); Operation operation = new Operation(1111L, null, null,null, null); Scope scope = new Scope(1000L, null, null, null, null); scope.setOperations(new HashSet<>(Collections.singletonList(operation))); context.set(OPERATION_ID, operation.getId()); context.set(API_ID, api.getId()); Mockito.when(appRepository.findByClientId(Mockito.anyString())).thenReturn(null); this.filter.run(); final FilterDetail filterDetail = TraceContextHolder.getInstance().getActualTrace().getFilters().get(SCOPES_FILTER); assertEquals(HttpStatus.OK.value(), context.getResponseStatusCode()); assertEquals(Constants.SUCCESS, filterDetail.getStatus()); }
@Test public void noPlan() { this.request.addHeader(CLIENT_ID, clientId1); App app = new App(); app.setId(1L); app.setClientId(clientId1); Operation operation = new Operation(1111L, null, null,null, null); Scope scope = new Scope(1000L, null, null, null, null); scope.setOperations(new HashSet<>(Collections.singletonList(operation))); context.set(OPERATION_ID, operation.getId()); Mockito.when(appRepository.findByClientId(Mockito.anyString())).thenReturn(app); this.filter.run(); final FilterDetail filterDetail = TraceContextHolder.getInstance().getActualTrace().getFilters().get(SCOPES_FILTER); assertEquals(HttpStatus.OK.value(), context.getResponseStatusCode()); assertEquals(Constants.SUCCESS, filterDetail.getStatus()); }
@Test public void noScopes() { this.request.addHeader(CLIENT_ID, clientId1); App app = new App(); app.setId(1L); app.setClientId(clientId1); Plan plan = new Plan(); plan.setId(1L); Operation operation = new Operation(1111L, null, null,null, null); context.set(OPERATION_ID, operation.getId()); Mockito.when(appRepository.findByClientId(Mockito.anyString())).thenReturn(app); this.filter.run(); final FilterDetail filterDetail = TraceContextHolder.getInstance().getActualTrace().getFilters().get(SCOPES_FILTER); assertEquals(HttpStatus.OK.value(), context.getResponseStatusCode()); assertEquals(Constants.SUCCESS, filterDetail.getStatus()); }
@Test public void noOperationIdContext() { this.request.addHeader(CLIENT_ID, clientId1); Api api = new Api(); api.setId(1L); App app = new App(); app.setId(100L); app.setClientId(clientId1); Plan plan = new Plan(); plan.setId(10L); plan.setApi(api); Operation operation = new Operation(1111L, null, null,null, null); Scope scope = new Scope(1000L, null, null, null, null); app.setPlans(Collections.singletonList(plan)); plan.setScopes(new HashSet<>(Collections.singletonList(scope))); scope.setOperations(new HashSet<>(Collections.singletonList(operation))); Mockito.when(appRepository.findByClientId(Mockito.anyString())).thenReturn(app); this.filter.run(); final FilterDetail filterDetail = TraceContextHolder.getInstance().getActualTrace().getFilters().get(SCOPES_FILTER); assertEquals(HttpStatus.OK.value(), context.getResponseStatusCode()); assertEquals(Constants.SUCCESS, filterDetail.getStatus()); }