Skip to content

Commit

Permalink
removed dublicated style while updating default (#3769) (#3772)
Browse files Browse the repository at this point in the history
  • Loading branch information
allyoucanmap authored and Tobia Di Pisa committed May 20, 2019
1 parent 7576e94 commit 5c5c24d
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 11 deletions.
16 changes: 9 additions & 7 deletions web/client/api/geoserver/Layers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* LICENSE file in the root directory of this source tree.
*/
const axios = require('../../libs/ajax');
const { uniqBy } = require('lodash');
const { getNameParts } = require('../../utils/StyleEditorUtils');

/**
Expand Down Expand Up @@ -97,9 +98,13 @@ const Api = {
const layer = data.layer || {};
const currentAvailableStyle = layer.styles && layer.styles.style || {};
const defaultStyle = layer.defaultStyle || {};
const newAvailableStyle = currentAvailableStyle.filter(({ name: sName }) => {
return sName !== styleName;
});

// add old default to available styles to ensure to display it in the style list
const style = uniqBy([
defaultStyle,
...currentAvailableStyle
], 'name');

const layerObj = {
'layer': {
...layer,
Expand All @@ -108,10 +113,7 @@ const Api = {
},
'styles': {
'@class': 'linked-hash-set',
'style': [
defaultStyle,
...newAvailableStyle
]
style
}
}
};
Expand Down
152 changes: 148 additions & 4 deletions web/client/api/geoserver/__tests__/Layers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
* LICENSE file in the root directory of this source tree.
*/

var expect = require('expect');
var API = require('../Layers');
import expect from 'expect';
import API from '../Layers';
import MockAdapter from 'axios-mock-adapter';
import axios from '../../../libs/ajax';

let mockAxios;

describe('Test layers rest API', () => {
it('get layer', (done) => {
Expand Down Expand Up @@ -57,12 +61,152 @@ describe('Test layers rest API', () => {
}).then((layerObj)=> {
expect(layerObj).toExist();
expect(layerObj.layer.defaultStyle.name).toBe(newDefaultStyle);
expect(layerObj.layer.styles.style.length).toBe(2);
expect(layerObj.layer.styles.style.length).toBe(3);
expect(layerObj.layer.styles.style[0].name).toBe('test_TEST_LAYER_1');
expect(layerObj.layer.styles.style[1].name).toBe('generic');
expect(layerObj.layer.styles.style[1].name).toBe('point');
expect(layerObj.layer.styles.style[2].name).toBe('generic');
done();
}).catch(e => {
done(e);
});
});
});

describe('Test default style update with layers rest API', () => {
beforeEach(done => {
mockAxios = new MockAdapter(axios);
setTimeout(done);
});

afterEach(done => {
mockAxios.restore();
setTimeout(done);
});

it('test updateDefaultStyle, move old default style to available style', (done) => {

const OLD_DEFAULT_STYLE = {
name: 'workspace001:old_default_style',
workspace: 'workspace001',
href: '/geoserver/rest/workspaces/workspace001/styles/old_default_style.json'
};

mockAxios.onGet(/\/layers/).reply((config) => {
expect(config.url).toBe('/geoserver/rest/workspaces/workspace001/layers/layer001.json');
return [ 200, {
"layer": {
"name": "layer001",
"defaultStyle": OLD_DEFAULT_STYLE,
"styles": {
"@class": "linked-hash-set",
"style": [{
"name": "workspace001:new_default_style",
"workspace": "workspace001",
"href": "\/geoserver\/rest\/workspaces\/workspace001\/styles\/new_default_style.json"
}]
}
}
}];
});

mockAxios.onPut(/\/layers/).reply((config) => {
try {
const layer = JSON.parse(config.data).layer;
expect(layer.defaultStyle).toEqual({ "name": "workspace001:new_default_style" });
expect(layer.styles.style).toEqual([
OLD_DEFAULT_STYLE,
{
"name": "workspace001:new_default_style",
"workspace": "workspace001",
"href": "/geoserver/rest/workspaces/workspace001/styles/new_default_style.json"
}
]);
} catch(e) {
done(e);
}
done();
return [ 200, {}];
});

API.updateDefaultStyle({
baseUrl: '/geoserver/',
layerName: 'workspace001:layer001',
styleName: 'workspace001:new_default_style'
});
});

it('test updateDefaultStyle, available style must have unique name', (done) => {

const OLD_DEFAULT_STYLE = {
name: 'workspace001:old_default_style',
workspace: 'workspace001',
href: '/geoserver/rest/workspaces/workspace001/styles/old_default_style.json'
};

mockAxios.onGet(/\/layers/).reply((config) => {
expect(config.url).toBe('/geoserver/rest/workspaces/workspace001/layers/layer001.json');
return [ 200, {
layer: {
name: 'layer001',
defaultStyle: OLD_DEFAULT_STYLE,
styles: {
'@class': 'linked-hash-set',
style: [
OLD_DEFAULT_STYLE,
{
name: 'workspace001:new_default_style',
workspace: 'workspace001',
href: '/geoserver/rest/workspaces/workspace001/styles/new_default_style.json'
},
{
name: 'workspace001:other_style',
workspace: 'workspace001',
href: '/geoserver/rest/workspaces/workspace001/styles/other_style.json'
},
{
name: 'workspace001:other_style',
workspace: 'workspace001',
href: '/geoserver/rest/workspaces/workspace001/styles/other_style.json'
},
{
name: 'workspace001:other_style',
workspace: 'workspace001',
href: '/geoserver/rest/workspaces/workspace001/styles/other_style.json'
}
]
}
}
}];
});

mockAxios.onPut(/\/layers/).reply((config) => {
try {
const layer = JSON.parse(config.data).layer;
expect(layer.defaultStyle).toEqual({ "name": "workspace001:new_default_style" });
expect(layer.styles.style).toEqual([
OLD_DEFAULT_STYLE,
{
"name": "workspace001:new_default_style",
"workspace": "workspace001",
"href": "/geoserver/rest/workspaces/workspace001/styles/new_default_style.json"
},
{
"name": "workspace001:other_style",
"workspace": "workspace001",
"href": "/geoserver/rest/workspaces/workspace001/styles/other_style.json"
}
]);
} catch(e) {
done(e);
}
done();
return [ 200, {}];
});

API.updateDefaultStyle({
baseUrl: '/geoserver/',
layerName: 'workspace001:layer001',
styleName: 'workspace001:new_default_style'
});
});
});

0 comments on commit 5c5c24d

Please sign in to comment.