AppNamespaceServiceTest.java 10.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright 2021 Apollo Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
L
lepdou 已提交
17 18 19
package com.ctrip.framework.apollo.portal.service;

import com.ctrip.framework.apollo.common.entity.AppNamespace;
20
import com.ctrip.framework.apollo.common.exception.BadRequestException;
L
lepdou 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
import com.ctrip.framework.apollo.core.ConfigConsts;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import com.ctrip.framework.apollo.portal.AbstractIntegrationTest;

import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.jdbc.Sql;

import java.util.List;

public class AppNamespaceServiceTest extends AbstractIntegrationTest {

  @Autowired
  private AppNamespaceService appNamespaceService;

  private final String APP = "app-test";

  @Test
  @Sql(scripts = "/sql/appnamespaceservice/init-appnamespace.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
  @Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
  public void testFindPublicAppNamespace() {

    List<AppNamespace> appNamespaceList = appNamespaceService.findPublicAppNamespaces();

    Assert.assertNotNull(appNamespaceList);
    Assert.assertEquals(5, appNamespaceList.size());

  }

  @Test
  @Sql(scripts = "/sql/appnamespaceservice/init-appnamespace.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
  @Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
  public void testFindPublicAppNamespaceByName() {

    Assert.assertNotNull(appNamespaceService.findPublicAppNamespace("datasourcexml"));

    Assert.assertNull(appNamespaceService.findPublicAppNamespace("TFF.song0711-02"));
  }

  @Test
  @Sql(scripts = "/sql/appnamespaceservice/init-appnamespace.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
  @Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
  public void testFindPublicAppNamespaceByAppAndName() {

    Assert.assertNotNull(appNamespaceService.findByAppIdAndName("100003173", "datasourcexml"));

    Assert.assertNull(appNamespaceService.findByAppIdAndName("100003173", "TFF.song0711-02"));
  }


  @Test
  @Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
  public void testCreateDefaultAppNamespace() {
    appNamespaceService.createDefaultAppNamespace(APP);

    AppNamespace appNamespace = appNamespaceService.findByAppIdAndName(APP, ConfigConsts.NAMESPACE_APPLICATION);

    Assert.assertNotNull(appNamespace);
    Assert.assertEquals(ConfigFileFormat.Properties.getValue(), appNamespace.getFormat());

  }

  @Test(expected = BadRequestException.class)
  @Sql(scripts = "/sql/appnamespaceservice/init-appnamespace.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
  @Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
  public void testCreatePublicAppNamespaceExisted() {
88
    AppNamespace appNamespace = assembleBaseAppNamespace();
L
lepdou 已提交
89
    appNamespace.setPublic(true);
L
lepdou 已提交
90
    appNamespace.setName("old");
91
    appNamespace.setFormat(ConfigFileFormat.Properties.getValue());
L
lepdou 已提交
92 93 94 95

    appNamespaceService.createAppNamespaceInLocal(appNamespace);
  }

96 97 98 99
  @Test(expected = BadRequestException.class)
  @Sql(scripts = "/sql/appnamespaceservice/init-appnamespace.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
  @Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
  public void testCreatePublicAppNamespaceExistedAsPrivateAppNamespace() {
100
    AppNamespace appNamespace = assembleBaseAppNamespace();
101 102 103 104 105 106 107
    appNamespace.setPublic(true);
    appNamespace.setName("private-01");
    appNamespace.setFormat(ConfigFileFormat.Properties.getValue());

    appNamespaceService.createAppNamespaceInLocal(appNamespace);
  }

L
lepdou 已提交
108 109 110
  @Test
  @Sql(scripts = "/sql/appnamespaceservice/init-appnamespace.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
  @Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
111
  public void testCreatePublicAppNamespaceNotExistedWithNoAppendnamespacePrefix() {
112
    AppNamespace appNamespace = assembleBaseAppNamespace();
113 114
    appNamespace.setPublic(true);
    appNamespace.setName("old");
115
    appNamespace.setFormat(ConfigFileFormat.Properties.getValue());
116 117 118 119 120 121 122 123 124 125 126

    AppNamespace createdAppNamespace = appNamespaceService.createAppNamespaceInLocal(appNamespace, false);

    Assert.assertNotNull(createdAppNamespace);
    Assert.assertEquals(appNamespace.getName(), createdAppNamespace.getName());
  }

  @Test(expected = BadRequestException.class)
  @Sql(scripts = "/sql/appnamespaceservice/init-appnamespace.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
  @Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
  public void testCreatePublicAppNamespaceExistedWithNoAppendnamespacePrefix() {
127
    AppNamespace appNamespace = assembleBaseAppNamespace();
128 129
    appNamespace.setPublic(true);
    appNamespace.setName("datasource");
130
    appNamespace.setFormat(ConfigFileFormat.Properties.getValue());
131 132 133 134 135 136 137

    appNamespaceService.createAppNamespaceInLocal(appNamespace, false);
  }

  @Test
  @Sql(scripts = "/sql/appnamespaceservice/init-appnamespace.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
  @Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
L
lepdou 已提交
138
  public void testCreatePublicAppNamespaceNotExisted() {
139
    AppNamespace appNamespace = assembleBaseAppNamespace();
L
lepdou 已提交
140 141 142 143 144 145 146 147 148 149
    appNamespace.setPublic(true);

    appNamespaceService.createAppNamespaceInLocal(appNamespace);

    AppNamespace createdAppNamespace = appNamespaceService.findPublicAppNamespace(appNamespace.getName());

    Assert.assertNotNull(createdAppNamespace);
    Assert.assertEquals(appNamespace.getName(), createdAppNamespace.getName());
  }

150 151 152 153 154 155 156 157 158 159 160 161 162 163
  @Test
  @Sql(scripts = "/sql/appnamespaceservice/init-appnamespace.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
  @Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
  public void testCreatePublicAppNamespaceWithWrongFormatNotExisted() {
    AppNamespace appNamespace = assembleBaseAppNamespace();
    appNamespace.setPublic(true);
    appNamespace.setFormat(ConfigFileFormat.YAML.getValue());

    appNamespaceService.createAppNamespaceInLocal(appNamespace);

    AppNamespace createdAppNamespace = appNamespaceService.findPublicAppNamespace(appNamespace.getName());

    Assert.assertNotNull(createdAppNamespace);
    Assert.assertEquals(appNamespace.getName(), createdAppNamespace.getName());
张乐 已提交
164
    Assert.assertEquals(ConfigFileFormat.YAML.getValue(), createdAppNamespace.getFormat());
165 166 167
  }


L
lepdou 已提交
168 169 170 171
  @Test(expected = BadRequestException.class)
  @Sql(scripts = "/sql/appnamespaceservice/init-appnamespace.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
  @Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
  public void testCreatePrivateAppNamespaceExisted() {
172
    AppNamespace appNamespace = assembleBaseAppNamespace();
L
lepdou 已提交
173
    appNamespace.setPublic(false);
174 175 176 177 178 179 180 181 182 183
    appNamespace.setName("datasource");
    appNamespace.setAppId("100003173");

    appNamespaceService.createAppNamespaceInLocal(appNamespace);
  }

  @Test
  @Sql(scripts = "/sql/appnamespaceservice/init-appnamespace.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
  @Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
  public void testCreatePrivateAppNamespaceExistedInAnotherAppId() {
184
    AppNamespace appNamespace = assembleBaseAppNamespace();
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
    appNamespace.setPublic(false);
    appNamespace.setName("datasource");
    appNamespace.setAppId("song0711-01");

    appNamespaceService.createAppNamespaceInLocal(appNamespace);

    AppNamespace createdAppNamespace =
        appNamespaceService.findByAppIdAndName(appNamespace.getAppId(), appNamespace.getName());

    Assert.assertNotNull(createdAppNamespace);
    Assert.assertEquals(appNamespace.getName(), createdAppNamespace.getName());
  }

  @Test(expected = BadRequestException.class)
  @Sql(scripts = "/sql/appnamespaceservice/init-appnamespace.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
  @Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
  public void testCreatePrivateAppNamespaceExistedInAnotherAppIdAsPublic() {
202
    AppNamespace appNamespace = assembleBaseAppNamespace();
203 204
    appNamespace.setPublic(false);
    appNamespace.setName("SCC.song0711-03");
L
lepdou 已提交
205
    appNamespace.setAppId("100003173");
206
    appNamespace.setFormat(ConfigFileFormat.Properties.getValue());
L
lepdou 已提交
207 208 209 210 211 212 213 214

    appNamespaceService.createAppNamespaceInLocal(appNamespace);
  }

  @Test
  @Sql(scripts = "/sql/appnamespaceservice/init-appnamespace.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
  @Sql(scripts = "/sql/cleanup.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
  public void testCreatePrivateAppNamespaceNotExisted() {
215
    AppNamespace appNamespace = assembleBaseAppNamespace();
L
lepdou 已提交
216 217 218 219 220 221 222 223 224 225 226
    appNamespace.setPublic(false);

    appNamespaceService.createAppNamespaceInLocal(appNamespace);

    AppNamespace createdAppNamespace =
        appNamespaceService.findByAppIdAndName(appNamespace.getAppId(), appNamespace.getName());

    Assert.assertNotNull(createdAppNamespace);
    Assert.assertEquals(appNamespace.getName(), createdAppNamespace.getName());
  }

227
  private AppNamespace assembleBaseAppNamespace() {
L
lepdou 已提交
228 229 230 231 232 233 234 235
    AppNamespace appNamespace = new AppNamespace();
    appNamespace.setName("appNamespace");
    appNamespace.setAppId("1000");
    appNamespace.setFormat(ConfigFileFormat.XML.getValue());
    return appNamespace;
  }

}