import { it, expect, describe, inject, async, beforeEach, beforeEachProviders } from '@angular/core/testing'; import { OwsSearchService, ISearchFilter } from '../../dist/app/core'; import { ISearchConfig, SEARCH_CONFIG } from '../../dist/app/config/config.module'; import {provide} from '@angular/core'; import {AUTH_CONFIG, IAuthConfig} from 'atla-login/core'; import {AuthHttp} from 'angular2-jwt'; import {Observable} from 'rxjs/Observable'; import {Subscriber} from 'rxjs/Subscriber'; import { Response, ResponseOptions, URLSearchParams, } from '@angular/http'; import { IFeatureCollection } from '../../src/app/utility/feature-collection.interface'; class MockSearchConfig implements ISearchConfig { searchUrl: string = 'someUrl'; } class MockAuthConfig implements IAuthConfig { headerPrefix: string; noJwtError: boolean; authHeaderName: string; appName: string = 'some app'; tokenEndpoint: string = 'token endpoint'; signOutEndpoint: string; authPath: string; tokenKey: string = 'token key'; pcName: string; } const docType = 'drilling'; const mockSource = 'OCC'; const mockResult: any = { columns: [ {'name': 'source', 'label': 'Source', 'type': 'text', 'visible': true}, {'name': 'state', 'label': 'State', 'type': 'text', 'visible': true}, { 'name': 'kml_centroid', 'label': 'kml_centroid', 'type': 'text', 'visible': false }, { 'name': 'kml_polygon', 'label': 'kml_polygon', 'type': 'text', 'visible': false }, {'name': 'docType', 'label': 'Doc Type', 'type': 'text', 'visible': true}, ], data: [ [ // feature with geojson mockSource, 'OK', '' + '' + '-97.575521330698128,35.142475536542156' + '' + '', '' + '' + '' + '' + '-97.410459213375504,35.1533634094558 ' + '-97.40594985,35.1533634094558 ' + '-97.40594985,35.149721124727896 ' + '-97.410459213375504,35.149721124727896 ' + '-97.410459213375504,35.1533634094558' + '' + '' + '' + '', docType ], [ // unmappable feature mockSource, 'OK', '', '', docType ] ] }; const aoi = 'some aoi'; const timePeriod = '1-year'; const featureId = 'random-id'; const expectedGeoJson: IFeatureCollection = { type: 'FeatureCollection', features: [{ id: featureId, type: 'Feature', geometry: { type: 'Point', coordinates: [-97.575521330698128, 35.142475536542156] }, properties: { 'id': featureId, 'source': 'OCC', 'state': 'OK', 'record-type': docType, 'kml_centroid': mockResult.data[0][2], 'kml_polygon': mockResult.data[0][3] } }, { id: featureId, type: 'Feature', geometry: { type: 'Polygon', coordinates: [ [ [-97.410459213375504, 35.1533634094558], [-97.40594985, 35.1533634094558], [-97.40594985, 35.149721124727896], [-97.410459213375504, 35.149721124727896], [-97.410459213375504, 35.1533634094558] ] ] }, properties: { 'id': featureId, 'source': 'OCC', 'state': 'OK', 'record-type': docType, 'kml_centroid': mockResult.data[0][2], 'kml_polygon': mockResult.data[0][3] } }] }; const mockSearchFilter: ISearchFilter = { aoi: aoi, timePeriod: timePeriod }; const searchResultResponse: Observable = new Observable( (o: Subscriber) => o.next(new Response(new ResponseOptions({ status: 200, statusText: 'FOUND', body: mockResult }))) ); const emptySearchResultResponse: Observable = new Observable( (o: Subscriber) => o.next(new Response(new ResponseOptions({ status: 200, statusText: 'FOUND', body: {} }))) ); class MockAuthHttp { get( url: string, options: {search: URLSearchParams} = {search: new URLSearchParams()} ): Observable { if (options.search) { const searchAoi = options.search.get('aoi') || ''; if (searchAoi.match(aoi)) { return searchResultResponse; } } return emptySearchResultResponse; } } describe('OWS Search Query Service', () => { const mockSearchConfig = new MockSearchConfig(); const mockAuthConfig = new MockAuthConfig(); const mockAuthHttp = new MockAuthHttp(); let search: OwsSearchService; beforeEachProviders(() => { return [ OwsSearchService, provide(SEARCH_CONFIG, {useValue: mockSearchConfig}), provide(AUTH_CONFIG, {useValue: mockAuthConfig}), provide(AuthHttp, {useValue: mockAuthHttp}) ]; }); beforeEach(async( inject( [OwsSearchService], searchService => { search = searchService; } ) )); it('should return search result as a record collection', done => { search.get(docType, mockSearchFilter) .subscribe(collection => { expect(collection.records.length).toEqual(mockResult.data.length); expect(collection.records[0]['source']).toEqual(mockSource); expect(collection.fields.length).toEqual(mockResult.columns.length); done(); }); }); it('should return record collections with valid geojson', done => { search.get(docType, mockSearchFilter) .subscribe(collection => { const geoJson = collection.getRecordGeoJson(collection.records[0]); const featureOne = geoJson.features[0]; const featureTwo = geoJson.features[1]; expect(featureOne.geometry) .toEqual(expectedGeoJson.features[0].geometry); expect(featureTwo.geometry) .toEqual(expectedGeoJson.features[1].geometry); expect(featureOne.properties.source) .toEqual(featureOne.properties.source); expect(featureOne.properties['record-type']) .toEqual(featureOne.properties['record-type']); done(); }); }); it('should not create features for records without geometries', done => { search.get(docType, mockSearchFilter) .subscribe(collection => { const geoJson = collection.getRecordGeoJson(collection.records[1]); expect(geoJson.features).toEqual([]); done(); }); }); });